public PhysicalConnection(PhysicalBridge bridge)
        {
            lastWriteTickCount = lastReadTickCount = Environment.TickCount;
            lastBeatTickCount = 0;
            this.connectionType = bridge.ConnectionType;
            this.multiplexer = bridge.Multiplexer;
            this.ChannelPrefix = multiplexer.RawConfig.ChannelPrefix;
            if (this.ChannelPrefix != null && this.ChannelPrefix.Length == 0) this.ChannelPrefix = null; // null tests are easier than null+empty
            var endpoint = bridge.ServerEndPoint.EndPoint;
            physicalName = connectionType + "#" + Interlocked.Increment(ref totalCount) + "@" + Format.ToString(endpoint);
            this.bridge = bridge;
            multiplexer.Trace("Connecting...", physicalName);

            this.socketToken = multiplexer.SocketManager.BeginConnect(endpoint, this);
            //socket.SendTimeout = socket.ReceiveTimeout = multiplexer.TimeoutMilliseconds;
            OnCreateEcho();
        }
 internal void Shutdown(SocketToken token)
 {
     Shutdown(token.Socket);
 }
        internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, ConnectionMultiplexer multiplexer, TextWriter log)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SetFastLoopbackOption(socket);
            socket.NoDelay = true;
            try
            {
                CompletionType connectCompletionType = CompletionType.Any;
                this.ShouldForceConnectCompletionType(ref connectCompletionType);

                var formattedEndpoint = Format.ToString(endpoint);
                if (endpoint is DnsEndPoint)
                {
                    // A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state)
                    DnsEndPoint dnsEndpoint = (DnsEndPoint)endpoint;
                    CompletionTypeHelper.RunWithCompletionType(
                        (cb) =>
                        {
                            multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                            return socket.BeginConnect(dnsEndpoint.Host, dnsEndpoint.Port, cb, Tuple.Create(socket, callback));
                        },
                        (ar) =>
                        {
                            multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                            EndConnectImpl(ar, multiplexer, log);
                            multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                        },
                        connectCompletionType);
                }
                else
                {
                    CompletionTypeHelper.RunWithCompletionType(
                        (cb) => {
                            multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                            return socket.BeginConnect(endpoint, cb, Tuple.Create(socket, callback));
                        },
                        (ar) => {
                            multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                            EndConnectImpl(ar, multiplexer, log);
                            multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                        },
                        connectCompletionType);
                }
            } 
            catch (NotImplementedException ex)
            {
                if (!(endpoint is IPEndPoint))
                {
                    throw new InvalidOperationException("BeginConnect failed with NotImplementedException; consider using IP endpoints, or enable ResolveDns in the configuration", ex);
                }
                throw;
            }
            var token = new SocketToken(socket);
            return token;
        }
        public void BeginConnect(TextWriter log)
        {
            Thread.VolatileWrite(ref firstUnansweredWriteTickCount, 0);
            var endpoint = this.bridge.ServerEndPoint.EndPoint;

            multiplexer.Trace("Connecting...", physicalName);
            this.socketToken = multiplexer.SocketManager.BeginConnect(endpoint, this, multiplexer, log);
        }
 public void Dispose()
 {
     if (outStream != null)
     {
         multiplexer.Trace("Disconnecting...", physicalName);
         try { outStream.Close(); } catch { }
         try { outStream.Dispose(); } catch { }
         outStream = null;
     }
     if (netStream != null)
     {
         try { netStream.Close(); } catch { }
         try { netStream.Dispose(); } catch { }
         netStream = null;
     }
     if (socketToken.HasValue)
     {
         var socketManager = multiplexer.SocketManager;
         if (socketManager != null) socketManager.Shutdown(socketToken);
         socketToken = default(SocketToken);
         multiplexer.Trace("Disconnected", physicalName);
         RecordConnectionFailed(ConnectionFailureType.ConnectionDisposed);
     }
     OnCloseEcho();
 }
 internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback)
 {
     var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     SetFastLoopbackOption(socket);
     socket.NoDelay = true;
     try
     {
         var ar = socket.BeginConnect(endpoint, EndConnect, Tuple.Create(socket, callback));
         if (ar.CompletedSynchronously)
         {
             ConnectionMultiplexer.TraceWithoutContext("EndConnect (sync)");
             EndConnectImpl(ar);
         }
     } catch (NotImplementedException ex)
     {
         if (!(endpoint is IPEndPoint))
         {
             throw new InvalidOperationException("BeginConnect failed with NotImplementedException; consider using IP endpoints, or enable ResolveDns in the configuration", ex);
         }
         throw;
     }
     var token = new SocketToken(socket);
     return token;
 }
 internal void Shutdown(SocketToken token)
 {
     Shutdown(token.Socket);
 }
        internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, ConnectionMultiplexer multiplexer, TextWriter log)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            SetFastLoopbackOption(socket);
            socket.NoDelay = true;
            try
            {
                CompletionType connectCompletionType = CompletionType.Any;
                this.ShouldForceConnectCompletionType(ref connectCompletionType);

                var formattedEndpoint = Format.ToString(endpoint);
                var tuple             = Tuple.Create(socket, callback);
                if (endpoint is DnsEndPoint)
                {
                    // A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state)
                    DnsEndPoint dnsEndpoint = (DnsEndPoint)endpoint;

#if CORE_CLR
                    multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                    socket.ConnectAsync(dnsEndpoint.Host, dnsEndpoint.Port).ContinueWith(t =>
                    {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(t, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    });
#else
                    CompletionTypeHelper.RunWithCompletionType(
                        cb => {
                        multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                        return(socket.BeginConnect(dnsEndpoint.Host, dnsEndpoint.Port, cb, tuple));
                    },
                        ar => {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(ar, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    },
                        connectCompletionType);
#endif
                }
                else
                {
#if CORE_CLR
                    multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                    socket.ConnectAsync(endpoint).ContinueWith(t =>
                    {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(t, multiplexer, log, tuple);
                    });
#else
                    CompletionTypeHelper.RunWithCompletionType(
                        cb => {
                        multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                        return(socket.BeginConnect(endpoint, cb, tuple));
                    },
                        ar => {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(ar, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    },
                        connectCompletionType);
#endif
                }
            }
            catch (NotImplementedException ex)
            {
                if (!(endpoint is IPEndPoint))
                {
                    throw new InvalidOperationException("BeginConnect failed with NotImplementedException; consider using IP endpoints, or enable ResolveDns in the configuration", ex);
                }
                throw;
            }
            var token = new SocketToken(socket);
            return(token);
        }