Beispiel #1
0
        void ClientAccepted(IAsyncResult asyncResult)
        {
            bool didReschedule = false;

            try
            {
                TcpClient tcpConnection = tcpListener.EndAcceptTcpClient(asyncResult);
                if (tcpConnection != null)
                {
                    tcpListener.BeginAcceptTcpClient(ClientAccepted, null);
                    didReschedule = true;

                    try
                    {
                        bool       endpointInPermittedRange = false;
                        IPEndPoint remoteIPEndpoint         = (IPEndPoint)tcpConnection.Client.RemoteEndPoint;
                        foreach (IPRange range in firewallRules)
                        {
                            if (range.IsInRange(remoteIPEndpoint.Address))
                            {
                                endpointInPermittedRange = true;
                                break;
                            }
                        }
                        if (!endpointInPermittedRange)
                        {
                            Trace.TraceWarning("No matching firewall rule. Dropping connection from {0}", remoteIPEndpoint.Address);
                            tcpConnection.Close();
                        }
                        else
                        {
                            tcpConnection.NoDelay             = true;
                            tcpConnection.LingerState.Enabled = false;
                            Stream socketStream = tcpConnection.GetStream();

                            EnsureConnection();

                            MultiplexedTcpConnection multiplexedConnection = new MultiplexedTcpConnection(tcpConnection, multiplexedOutputStream);
                            multiplexedConnection.Closed += MultiplexedConnectionClosed;
                            lock (connectionLock)
                            {
                                connections.Add(multiplexedConnection.Id, multiplexedConnection);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("Unable to establish connection: {0}", ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Failure accepting client: {0}", ex.Message);
                if (!didReschedule && this.open)
                {
                    tcpListener.BeginAcceptTcpClient(ClientAccepted, null);
                }
            }
        }
        MultiplexedConnection CorrelateConnection(int connectionId, object state)
        {
            MultiplexedTcpConnection connection = null;

            connections.TryGetValue(connectionId, out connection);
            return(connection);
        }
Beispiel #3
0
        void MultiplexedConnectionClosed(object sender, EventArgs e)
        {
            MultiplexedTcpConnection connection = (MultiplexedTcpConnection)sender;

            lock (connectionLock)
            {
                connections.Remove(connection.Id);
            }
        }