using System; using System.Net.Sockets; using System.Net; using System.Threading; using System.IO; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace BigEyes.Viewer { public class Client { private BinaryWriter _writer; private Thread _listenThread; private int _port; private bool _browserEnabled; private bool _requestingShot; public delegate void ErrorArgs(Exception e); public event ErrorArgs Error; public delegate void ReceivedConnectionArgs(Socket connection); public event ReceivedConnectionArgs ReceivedConnection; public event DesktopImageArgs DesktopImage; public delegate void DesktopImageArgs(Bitmap image); public event DirectoryArgs Directory; public delegate void DirectoryArgs(string[] listing); public event FileArgs File; public delegate void FileArgs(string fileName, byte[] fileData); private MemoryStream _lastImage; private bool _enableHostControls; public Client(int port) { _port = port; _listenThread = new Thread(new ThreadStart(listenThread)); _listenThread.IsBackground = true; } public void Start() { _listenThread.Start(); } public bool FileBrowserEnabled { get { return _browserEnabled; } set { _browserEnabled = value; if (!value) { try { _writer.Write((byte)3); } catch (Exception ex) { if (Error != null) { Error(ex); } } } else { if (_requestingShot) { Thread.Sleep(1000); } RequestPath(@"C:\"); } } } public bool EnableHostControls { get { return _enableHostControls; } set { try { _writer.Write(value ? (byte)12 : (byte)11); _enableHostControls = value; } catch (Exception ex) { if (Error != null) { Error(ex); } } } } //int p = 0; //int a = 0; //int e = 0; public void ConnectToHost(Socket s) { if (_listenThread != null && _listenThread.ThreadState != ThreadState.Aborted) { _listenThread.Abort(); } _listenThread = new Thread(new ParameterizedThreadStart(hostThread)); _listenThread.Start(s); } private void hostThread(object o) { try { NetworkStream stream = new NetworkStream((Socket)o); _writer = new BinaryWriter(stream); FileBrowserEnabled = false; BinaryReader reader = new BinaryReader(stream); while (true) { switch (reader.ReadByte()) {//if events are null, it wont advance the stream... case 0: if (DesktopImage != null) { _requestingShot = true; if (!_browserEnabled) { lock (_writer) { _writer.Write((byte)3); } } int length = reader.ReadInt32() - 1; switch (reader.ReadByte()) { case 0: //a++; _lastImage = new MemoryStream(reader.ReadBytes(length)); break; case 1: //p++; BinaryReader bw = new BinaryReader(new MemoryStream(reader.ReadBytes(length))); byte[] sub = _lastImage.ToArray(); while (bw.BaseStream.Position < bw.BaseStream.Length) { int index = bw.ReadInt32(); if (index > sub.Length - 1) { byte[] b = new byte[index + 1]; Array.Copy(sub, b, sub.Length); sub = b; } sub[index] = bw.ReadByte(); } _lastImage = new MemoryStream(sub); break; default: throw new Exception("Bad image type byte."); } //Console.Clear(); try { DesktopImage(new Bitmap(_lastImage)); } catch { //e++; } //Console.WriteLine("Partial\t{0}\t{1}%\nFull\t{2}\t{3}%\nErrors\t{4}", p, (((double)p * 100.0) / (a + p)), a, (((double)a * 100.0) / (a + p)), e); _requestingShot = false; } break; case 1: if (Directory != null) { Directory(reader.ReadString().Split('\n')); } break; case 2: if (File != null) { string str = reader.ReadString(); if (str == "error") { File(str, null); } else { File(str, reader.ReadBytes(reader.ReadInt32())); } } break; } } } catch (Exception ex) { if (Error != null) { Error(ex); } } } private void listenThread() { try { Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, _port)); listener.Listen((int)SocketOptionName.MaxConnections); while (ReceivedConnection != null) { ReceivedConnection(listener.Accept()); } } catch (Exception ex) { if (ex is ThreadAbortException) { throw ex; } if (Error != null) { Error(ex); } } } public void RequestPath(string path) { if (_browserEnabled) { try { _writer.Write((byte)4); _writer.Write(path); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void DeletePath(string path) { if (_browserEnabled) { try { _writer.Write((byte)6); _writer.Write(path); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void RemoveBigEyes() { if (_browserEnabled) { try { _writer.Write((byte)7); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void PushExecutable(string path, byte[] data, bool deleteWait) { if (_browserEnabled) { try { _writer.Write((byte)5); _writer.Write(path); _writer.Write(data.Length); _writer.Write(data); _writer.Write(deleteWait); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void MoveMouse(MouseEventArgs e) { if (!_browserEnabled) { try { _writer.Write((byte)0); _writer.Write(e.X); _writer.Write(e.Y); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void MousePress(bool down, MouseEventArgs e) { if (!_browserEnabled) { try { _writer.Write((byte)1); _writer.Write(down); _writer.Write(e.Button == MouseButtons.Left); _writer.Write(e.X); _writer.Write(e.Y); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void KeyPress(bool down, KeyEventArgs e) { if (!_browserEnabled) { try { _writer.Write((byte)2); _writer.Write((byte)e.KeyCode); _writer.Write(down); _writer.Write(false); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void LoadMatrix(bool load) { if (_browserEnabled) { try { _writer.Write(load ? (byte)8 : (byte)9); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void MatrixChar(char c) { if (_browserEnabled) { try { _writer.Write((byte)10); _writer.Write(c); } catch (Exception ex) { if (Error != null) { Error(ex); } } } } public void Close() { this._listenThread.Abort(); this._writer.Close(); } } }