private static void Main(string[] args) { EnvironmentOptions options = BuildEnvironmentOptions(args); if (options == null) { return; } IPAddress ipAddress = IPAddress.Any; var listeningSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.IP); listeningSocket.Bind(new IPEndPoint(ipAddress, options.ListenOnPort)); listeningSocket.Listen(0); var logger = BuildLogger(options); AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => logger.ShuttingDown(); while (true) { Socket clientSocket = null; Socket relaySocket = null; Task<Socket>.Factory.FromAsync(listeningSocket.BeginAccept, listeningSocket.EndAccept, null) .ContinueWith(task => { clientSocket = task.Result; return Task<IPHostEntry>.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, options.ForwardToHostAddress, null); }) .Unwrap() .ContinueWith(task => { IPHostEntry relayHostEntry = task.Result; IPAddress relayHostIpAddress = relayHostEntry.AddressList.First(); relaySocket = new Socket(relayHostIpAddress.AddressFamily, SocketType.Stream, ProtocolType.IP); return Task.Factory.FromAsync(relaySocket.BeginConnect, relaySocket.EndConnect, relayHostEntry.AddressList, options.ForwardToPort, null); }) .Unwrap() .ContinueWith(task => { var exchange = new TcpExchangeHub(logger); exchange.WireExchange(relaySocket, clientSocket); }) .Wait(); } }
public void ClientConnected(TcpExchangeHub hub) { Interlocked.Increment(ref this._connectedClients); }
public void Disconnected(TcpExchangeHub hub, TcpRelayDirection direction) { if (direction == TcpRelayDirection.ClientToRelay) { Interlocked.Decrement(ref this._connectedClients); } }
public void TransmitData(TcpExchangeHub hub, TcpRelayDirection direction, byte[] data, int count) { if (!this._transmissionSubject.HasObservers) { return; } var subject = new TransmissionSummary { Hub = hub, ByteCount = count, Direction = direction, }; this._transmissionSubject.OnNext(subject); }