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)
            {
            }
        }
Exemple #2
0
        public void Stop()
        {
            _clientMachine.Close();
            _serverMachine.Close();

            OnDisconnected();
        }
        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 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();
            }
        }
        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();
            }
        }