示例#1
0
        internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

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

                CompletionTypeHelper.RunWithCompletionType(
                    (cb) => socket.BeginConnect(endpoint, cb, Tuple.Create(socket, callback)),
                    (ar) => EndConnectImpl(ar),
                    CompletionType.Sync);
            }
            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);
        }
        private void OnAddRead(Socket socket, ISocketCallback callback)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            lock (socketLookup)
            {
                if (isDisposed)
                {
                    throw new ObjectDisposedException(name);
                }

                var handle = socket.Handle;
                if (handle == IntPtr.Zero)
                {
                    throw new ObjectDisposedException("socket");
                }
                socketLookup.Add(handle, new SocketPair(socket, callback));
                if (socketLookup.Count == 1)
                {
                    Monitor.PulseAll(socketLookup);
                    if (Interlocked.CompareExchange(ref readerCount, 0, 0) == 0)
                    {
                        StartReader();
                    }
                }
            }
        }
示例#3
0
        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 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);
        }
        internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            SetFastLoopbackOption(socket);
            socket.NoDelay = true;
            socket.BeginConnect(endpoint, EndConnect, Tuple.Create(socket, callback));
            return(new SocketToken(socket));
        }
        private void OnAddRead(Socket socket, ISocketCallback callback)
        {
            if (socket == null) throw new ArgumentNullException("socket");
            if (callback == null) throw new ArgumentNullException("callback");

            lock (socketLookup)
            {
                if (isDisposed) throw new ObjectDisposedException(name);

                var handle = socket.Handle;
                if (handle == IntPtr.Zero) throw new ObjectDisposedException("socket");
                socketLookup.Add(handle, new SocketPair(socket, callback));
                if (socketLookup.Count == 1)
                {
                    Monitor.PulseAll(socketLookup);
                    if (Interlocked.CompareExchange(ref readerCount, 0, 0) == 0)
                        StartReader();
                }
            }
        }
        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;
        }
        internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, ConnectionMultiplexer multiplexer, TextWriter log)
        {
            var addressFamily = endpoint.AddressFamily == AddressFamily.Unspecified ? AddressFamily.InterNetwork : endpoint.AddressFamily;
            var socket        = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);

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

                var formattedEndpoint = Format.ToString(endpoint);
                var tuple             = Tuple.Create(socket, callback);
                if (endpoint is DnsEndPoint dnsEndpoint)
                {
                    // A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state)
#if !FEATURE_THREADPOOL
                    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 !FEATURE_THREADPOOL
                    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);
        }
 public SocketPair(Socket socket, ISocketCallback callback)
 {
     this.Socket   = socket;
     this.Callback = callback;
 }
示例#10
0
 public SocketPair(Socket socket, ISocketCallback callback)
 {
     Socket   = socket;
     Callback = callback;
 }
 internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback)
 {
     var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket.NoDelay = true;
     socket.BeginConnect(endpoint, EndConnect, Tuple.Create(socket, callback));
     return new SocketToken(socket);
 }
 private void OnAddRead(System.Net.Sockets.Socket socket, ISocketCallback callback)
 {
     throw new System.NotSupportedException();
 }
 public SocketPair(Socket socket, ISocketCallback callback)
 {
     Socket = socket;
     Callback = callback;
 }
 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 SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, ConnectionMultiplexer multiplexer, TextWriter log)
        {
            void RunWithCompletionType(Func <AsyncCallback, IAsyncResult> beginAsync, AsyncCallback asyncCallback)
            {
                void proxyCallback(IAsyncResult ar)
                {
                    if (!ar.CompletedSynchronously)
                    {
                        asyncCallback(ar);
                    }
                }

                var result = beginAsync(proxyCallback);

                if (result.CompletedSynchronously)
                {
                    result.AsyncWaitHandle.WaitOne();
                    asyncCallback(result);
                }
            }

            var addressFamily = endpoint.AddressFamily == AddressFamily.Unspecified ? AddressFamily.InterNetwork : endpoint.AddressFamily;
            var socket        = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);

            SetFastLoopbackOption(socket);
            socket.NoDelay = true;
            try
            {
                var formattedEndpoint = Format.ToString(endpoint);
                var tuple             = Tuple.Create(socket, callback);
                multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                // A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state)
                if (endpoint is DnsEndPoint dnsEndpoint)
                {
                    RunWithCompletionType(
                        cb => 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);
                    });
                }
                else
                {
                    RunWithCompletionType(
                        cb => socket.BeginConnect(endpoint, cb, tuple),
                        ar => {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(ar, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    });
                }
            }
            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);
        }
 partial void ShouldIgnoreConnect(ISocketCallback callback, ref bool ignore);
 partial void ShouldIgnoreConnect(ISocketCallback callback, ref bool ignore)
 {
     ignore = callback.IgnoreConnect;
 }
 public SocketPair(Socket socket, ISocketCallback callback)
 {
     this.Socket = socket;
     this.Callback = callback;
 }