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();
            }
        }
示例#5
0
 void _clientMachine_Disconnected(TcpHost host)
 {
     Stop();
 }
示例#6
0
 void _serverMachine_Disconnected(TcpHost host)
 {
     Stop();
 }
示例#7
0
 void _clientMachine_DataAvailable(TcpHost host, byte[] data, int length)
 {
     _serverMachine.Send(data, length);
 }
示例#8
0
 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
            }
        }
        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();
            }
        }
示例#11
0
 void _serverMachine_Disconnected(TcpHost host)
 {
     Stop();
 }
示例#12
0
 void _serverMachine_DataAvailable(TcpHost host, byte[] data, int length)
 {
     _clientMachine.Send(data, length);
 }
示例#13
0
 void _clientMachine_Disconnected(TcpHost host)
 {
     Stop();
 }
示例#14
0
 public TcpRoute(TcpHost clientMachine, TcpHost serverMachine)
 {
     _clientMachine = clientMachine;
     _serverMachine = serverMachine;
 }