MultiplexedConnection CorrelateConnection(int connectionId, object state) { MultiplexedTcpConnection connection = null; connections.TryGetValue(connectionId, out connection); return(connection); }
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.ToString()); } else { tcpConnection.NoDelay = true; tcpConnection.LingerState.Enabled = false; Stream socketStream = tcpConnection.GetStream(); EnsureConnection(); MultiplexedTcpConnection multiplexedConnection = new MultiplexedTcpConnection(tcpConnection, this.multiplexedOutputStream); multiplexedConnection.Closed += new EventHandler(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) { tcpListener.BeginAcceptTcpClient(ClientAccepted, null); } } }
void MultiplexedConnectionClosed(object sender, EventArgs e) { MultiplexedTcpConnection connection = (MultiplexedTcpConnection)sender; connections.Remove(connection.Id); }
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; Trace.TraceInformation("TCP Connection from {0}", remoteIPEndpoint.Address.ToString()); 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.ToString()); } else { tcpConnection.NoDelay = true; tcpConnection.LingerState.Enabled = false; Stream socketStream = tcpConnection.GetStream(); EnsureConnection(); MultiplexedTcpConnection multiplexedConnection = new MultiplexedTcpConnection(tcpConnection, this.multiplexedOutputStream); multiplexedConnection.Closed += new EventHandler(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) { tcpListener.BeginAcceptTcpClient(ClientAccepted, null); } } }