private void AuthenticationCallback(IAsyncResult asyncResult) { if (asyncResult == null) { return; } TcpHost host = asyncResult.AsyncState as TcpHost; if (host == null) { return; } try { host.EndAuthenticationAsServer(asyncResult); ReadFirstPacket(host); } catch (IOException) { // Somehow, the authentication failed... Close the connection host.Close(); } }
private void TcpClientAcceptCallback(IAsyncResult asyncResult) { if (_closing) { return; } DoBeginListenForClients(); try { TcpClient client = _tcpListener.EndAcceptTcpClient(asyncResult); TcpHost host = new TcpHost(client); if (_certificate != null) { host.BeginAuthenticationAsServer(_certificate, _configuration.SslProtocols, AuthenticationCallback, host); } else { ReadFirstPacket(host); } } catch (IOException) { // TCP Listener may not be valid anymore } }
private void FirstPacketReadCallback(IAsyncResult asyncResult) { if (asyncResult == null) { return; } TcpStateContainer asyncState = asyncResult.AsyncState as TcpStateContainer; if (asyncState == null) { return; } TcpHost clientMachine = asyncState.Client; byte[] data = asyncState.DataBuffer; try { int numBytes = clientMachine.Stream.EndRead(asyncResult); string stringData = Encoding.UTF8.GetString(data, 0, numBytes); HttpPacket packet = HttpPacketBuilder.BuildPacket(stringData); if (packet == null) { clientMachine.Close(); return; } Host serverMachineHost = packet.IsWebSocketPacket ? _configuration.WebSocketHost : _configuration.HttpHost; if (serverMachineHost != null && serverMachineHost.IsSpecified) { TcpHost serverMachine = TcpHost.ManufactureDefault(serverMachineHost.IpAddress, serverMachineHost.Port); serverMachine.Send(data, numBytes); TcpRoute route = new TcpRoute(clientMachine, serverMachine); _tcpConnectionManager.AddRoute(route); } else { clientMachine.Close(); } } catch (IOException) { } }
private void ReadFirstPacket(TcpHost clientMachine) { byte[] data = new byte[4 * 1024]; try { TcpStateContainer state = new TcpStateContainer(clientMachine, data); clientMachine.Stream.BeginRead(data, 0, data.Length, FirstPacketReadCallback, state); } catch (IOException) { clientMachine.Close(); } }
void _clientMachine_Disconnected(TcpHost host) { Stop(); }
void _serverMachine_Disconnected(TcpHost host) { Stop(); }
void _clientMachine_DataAvailable(TcpHost host, byte[] data, int length) { _serverMachine.Send(data, length); }
public TcpRoute(TcpHost clientMachine, TcpHost serverMachine) { _clientMachine = clientMachine; _serverMachine = serverMachine; }
private void TcpClientAcceptCallback(IAsyncResult asyncResult) { if (_closing) return; DoBeginListenForClients(); try { TcpClient client = _tcpListener.EndAcceptTcpClient(asyncResult); TcpHost host = new TcpHost(client); if (_certificate != null) { host.BeginAuthenticationAsServer(_certificate, _configuration.SslProtocols, AuthenticationCallback, host); } else { ReadFirstPacket(host); } } catch (IOException) { // TCP Listener may not be valid anymore } }
void _serverMachine_DataAvailable(TcpHost host, byte[] data, int length) { _clientMachine.Send(data, length); }