private void ClientLoop() { var buffer = new byte[2048]; var offset = 0; while (_tcpClient.Connected) { offset += _tcpClientStream.Read(buffer, offset, buffer.Length - offset); while (offset > 0) { var validationResult = _packetValidator.Validate(buffer); if (validationResult.Valid) { offset -= validationResult.Size; var packet = _packetsFactory.Create(buffer.Take(validationResult.Size).ToArray()); if (packet.IsChecksumValid()) { var response = _packetHandler[packet.Type].Handle(packet); if (response != null) { _tcpClientStream.Write(response, 0, response.Length); } } Array.Copy(buffer, validationResult.Size, buffer, 0, buffer.Length - validationResult.Size); } else { break; } } } }
private void ServerLoop() { _tcpListener = new TcpListener(IPAddress.Any, _port); _tcpListener.Start(); _initializationEnded.Set(); while (true) { var client = _tcpListener.AcceptTcpClient(); var clientStream = client.GetStream(); Console.WriteLine($"New debugger client connected: {client.Client.RemoteEndPoint}"); var buffer = new byte[2048]; var offset = 0; while (client.Connected) { offset += clientStream.Read(buffer, offset, buffer.Length - offset); while (offset > 0) { var validationResult = _packetValidator.Validate(buffer); if (validationResult.Valid) { offset -= validationResult.Size; var packet = _packetsFactory.Create(buffer.Take(validationResult.Size).ToArray()); if (packet.IsChecksumValid()) { var responsePacket = _packetHandler[packet.Type].Handle(packet); if (responsePacket != null) { clientStream.Write(responsePacket.Data, 0, responsePacket.Data.Length); } } else { Console.WriteLine("Invalid checksum detected"); } Array.Copy(buffer, validationResult.Size, buffer, 0, buffer.Length - validationResult.Size); } else { break; } } } Console.Write("Debugger client disconnected"); } }