Esempio n. 1
0
        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();
            }
        }
Esempio n. 2
0
        private static CompositeTcpDataLogger BuildLogger(EnvironmentOptions options)
        {
            var logger = new CompositeTcpDataLogger();

            if (options.DetailedLoggingToConsole)
            {
                logger.AppendLogger(new ConsoleTcpDataLogger());
            }
            else
            {
                logger.AppendLogger(new SummaryConsoleTcpDataLogger());
            }

            if (!string.IsNullOrEmpty(options.LogFile))
            {
                logger.AppendLogger(new FileTcpDataLogger(options.LogFile));
            }

            return(logger);
        }
Esempio n. 3
0
        private static CompositeTcpDataLogger BuildLogger(EnvironmentOptions options)
        {
            var logger = new CompositeTcpDataLogger();

            if (options.DetailedLoggingToConsole)
            {
                logger.AppendLogger(new ConsoleTcpDataLogger());
            }
            else
            {
                logger.AppendLogger(new SummaryConsoleTcpDataLogger());
            }

            if (!string.IsNullOrEmpty(options.LogFile))
            {
                logger.AppendLogger(new FileTcpDataLogger(options.LogFile));
            }

            return logger;
        }