示例#1
0
        public static async Task <MyTcpProxyServer> CreateInstance(
            MyTcpProxyServerSettings settings
            )
        {
            if (settings == null)
            {
                throw new ArgumentNullException(
                          MyNameof.GetLocalVarName(() => settings));
            }
            settings.CheckMe();
            var result = new MyTcpProxyServer();

            result._clientToDestination = new TcpClient();
            await result._clientToDestination.ConnectAsync(
                settings.DestinationHostname,
                settings.DestinationPort
                ).ConfigureAwait(false);

            try
            {
                result._serverTcpListener = new TcpListener(
                    IPAddress.Parse(settings.ServerHostname),
                    settings.ServerPort
                    );
                result._serverTcpListener.Start();
                try
                {
                    result._stateHelper.SetInitializedState();
                    result.WaitClientConnectAction();
                    return(result);
                }
                catch
                {
                    result._serverTcpListener.Stop();
                    throw;
                }
            }
            catch
            {
                result._clientToDestination.Close();
                throw;
            }
        }
示例#2
0
 public static async Task<MyTcpProxyServer> CreateInstance(
     MyTcpProxyServerSettings settings
 )
 {
     if(settings == null)
         throw new ArgumentNullException(
             MyNameof.GetLocalVarName(() => settings));
     settings.CheckMe();
     var result = new MyTcpProxyServer();
     result._clientToDestination = new TcpClient();
     await result._clientToDestination.ConnectAsync(
         settings.DestinationHostname,
         settings.DestinationPort
     ).ConfigureAwait(false);
     try
     {
         result._serverTcpListener = new TcpListener(
             IPAddress.Parse(settings.ServerHostname),
             settings.ServerPort
         );
         result._serverTcpListener.Start();
         try
         {
             result._stateHelper.SetInitializedState();
             result.WaitClientConnectAction();
             return result;
         }
         catch
         {
             result._serverTcpListener.Stop();
             throw;
         }
     }
     catch
     {
         result._clientToDestination.Close();
         throw;
     }
 }