Exemple #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);
        }
        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);
        }