Beispiel #1
0
        void RawInputPumpCompleted(IAsyncResult a)
        {
            try
            {
                try
                {
                    Pump.EndRunPump(a);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Multiplex pump failed: {0}", ex.Message);
                }

                lock (connectLock)
                {
                    dataChannel?.Shutdown();
                    DataChannelClose();
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error closing data channel: {0}", ex.Message);
                lock (connectLock)
                {
                    DataChannelClose();
                }
            }
        }
Beispiel #2
0
        State DoDisconnect()
        {
            logger.Site().Debug("{0} Shutting down.", this);

            networkStream.Shutdown();

            if (connectionType == ConnectionType.Server)
            {
                var args = new DisconnectedEventArgs(this, errorDetails);
                parentListener.InvokeOnDisconnected(args);
            }

            responseMap.Shutdown();

            return(State.Disconnected);
        }
        public async Task HandleConnectionAsync(HybridConnectionStream hybridConnectionStream)
        {
            using (TcpClient client = new TcpClient())
            {
                client.NoDelay        = true;
                client.SendBufferSize = client.ReceiveBufferSize = 65536;
                client.SendTimeout    = 60000;
                if (config.BindAddress != null)
                {
                    var computerProperties        = IPGlobalProperties.GetIPGlobalProperties();
                    var unicastAddresses          = computerProperties.GetUnicastAddresses();
                    IList <IPAddress> ipAddresses = null;

                    ipAddresses = IPAddress.TryParse(config.BindAddress, out var ipAddress)
                        ? new[] { ipAddress }
                        : Dns.GetHostEntry(config.BindAddress).AddressList;

                    List <IPAddress> eligibleAddresses = new List <IPAddress>();
                    eligibleAddresses.AddRange(from hostAddress in ipAddresses
                                               where IPAddress.IsLoopback(hostAddress)
                                               select hostAddress);
                    eligibleAddresses.AddRange(from unicastAddress in unicastAddresses
                                               join hostAddress in ipAddresses on unicastAddress.Address equals hostAddress
                                               where !IPAddress.IsLoopback(hostAddress)
                                               select hostAddress);
                    // pick one of those eligible endpoints
                    client.Client.Bind(new IPEndPoint(eligibleAddresses[rnd.Next(eligibleAddresses.Count)], 0));
                }
                await client.ConnectAsync(targetServer, targetPort);

                var tcpstream = client.GetStream();

                CancellationTokenSource socketAbort = new CancellationTokenSource();
                await Task.WhenAll(
                    StreamPump.RunAsync(hybridConnectionStream, tcpstream,
                                        () => client.Client.Shutdown(SocketShutdown.Send), socketAbort.Token)
                    .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted),
                    StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream.Shutdown(), socketAbort.Token))
                .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
            }
        }
 void ShutdownSocketSafe(HybridConnectionStream relayEpoxyStream)
 {
     relayEpoxyStream?.Shutdown();
 }