コード例 #1
0
        async Task Connect(WebOperation operation, CancellationToken cancellationToken)
        {
            IPHostEntry hostEntry = ServicePoint.HostEntry;

            if (hostEntry == null || hostEntry.AddressList.Length == 0)
            {
#if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
                xamarin_start_wwan(ServicePoint.Address.ToString());
                hostEntry = ServicePoint.HostEntry;
                if (hostEntry == null)
                {
#endif
                throw GetException(ServicePoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
                                   WebExceptionStatus.NameResolutionFailure, null);
#if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
            }
#endif
            }

            Exception connectException = null;

            foreach (IPAddress address in hostEntry.AddressList)
            {
                operation.ThrowIfDisposed(cancellationToken);

                try {
                    socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                }
                catch (Exception se) {
                    // The Socket ctor can throw if we run out of FD's
                    throw GetException(WebExceptionStatus.ConnectFailure, se);
                }
                IPEndPoint remote = new IPEndPoint(address, ServicePoint.Address.Port);
                socket.NoDelay = !ServicePoint.UseNagleAlgorithm;
                try {
                    ServicePoint.KeepAliveSetup(socket);
                } catch {
                    // Ignore. Not supported in all platforms.
                }

                if (!ServicePoint.CallEndPointDelegate(socket, remote))
                {
                    Interlocked.Exchange(ref socket, null)?.Close();
                    continue;
                }
                else
                {
                    try {
                        operation.ThrowIfDisposed(cancellationToken);
                        await socket.ConnectAsync(remote).ConfigureAwait(false);
                    } catch (ObjectDisposedException) {
                        throw;
                    } catch (Exception exc) {
                        Interlocked.Exchange(ref socket, null)?.Close();
                        // Something went wrong, but we might have multiple IP Addresses
                        // and need to probe them all.
                        connectException = GetException(WebExceptionStatus.ConnectFailure, exc);
                        continue;
                    }
                }

                if (socket != null)
                {
                    return;
                }
            }

            if (connectException == null)
            {
                connectException = GetException(WebExceptionStatus.ConnectFailure, null);
            }

            throw connectException;
        }
コード例 #2
0
        void Connect(HttpWebRequest request)
        {
            lock (socketLock) {
                if (socket != null && socket.Connected && status == WebExceptionStatus.Success)
                {
                    // Take the chunked stream to the expected state (State.None)
                    if (CanReuse() && CompleteChunkedRead())
                    {
                        reused = true;
                        return;
                    }
                }

                reused = false;
                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }

                chunkStream = null;
                IPHostEntry hostEntry = sPoint.HostEntry;

                if (hostEntry == null)
                {
#if MONOTOUCH
                    if (start_wwan != null)
                    {
                        start_wwan.Invoke(null, new object [1] {
                            sPoint.Address
                        });
                        hostEntry = sPoint.HostEntry;
                    }
                    if (hostEntry == null)
                    {
#endif
                    status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
                             WebExceptionStatus.NameResolutionFailure;
                    return;

#if MONOTOUCH
                }
#endif
                }

                //WebConnectionData data = Data;
                foreach (IPAddress address in hostEntry.AddressList)
                {
                    try {
                        socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    }
                    catch (Exception se) {
                        // The Socket ctor can throw if we run out of FD's
                        if (!request.Aborted)
                        {
                            status = WebExceptionStatus.ConnectFailure;
                        }
                        connect_exception = se;
                        return;
                    }
                    IPEndPoint remote = new IPEndPoint(address, sPoint.Address.Port);
                    socket.NoDelay = !sPoint.UseNagleAlgorithm;
                    try {
                        sPoint.KeepAliveSetup(socket);
                    } catch {
                        // Ignore. Not supported in all platforms.
                    }

                    if (!sPoint.CallEndPointDelegate(socket, remote))
                    {
                        socket.Close();
                        socket = null;
                        status = WebExceptionStatus.ConnectFailure;
                    }
                    else
                    {
                        try {
                            if (request.Aborted)
                            {
                                return;
                            }
                            socket.Connect(remote);
                            status = WebExceptionStatus.Success;
                            break;
                        } catch (ThreadAbortException) {
                            // program exiting...
                            Socket s = socket;
                            socket = null;
                            if (s != null)
                            {
                                s.Close();
                            }
                            return;
                        } catch (ObjectDisposedException) {
                            // socket closed from another thread
                            return;
                        } catch (Exception exc) {
                            Socket s = socket;
                            socket = null;
                            if (s != null)
                            {
                                s.Close();
                            }
                            if (!request.Aborted)
                            {
                                status = WebExceptionStatus.ConnectFailure;
                            }
                            connect_exception = exc;
                        }
                    }
                }
            }
        }
コード例 #3
0
        async Task Connect(WebOperation operation, CancellationToken cancellationToken)
        {
            IPHostEntry hostEntry = ServicePoint.HostEntry;

            if (hostEntry == null || hostEntry.AddressList.Length == 0)
            {
#if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
                xamarin_start_wwan(ServicePoint.Address.ToString());
                hostEntry = ServicePoint.HostEntry;
                if (hostEntry == null)
                {
#endif
                throw GetException(ServicePoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
                                   WebExceptionStatus.NameResolutionFailure, null);
#if MONOTOUCH && !MONOTOUCH_TV && !MONOTOUCH_WATCH
            }
#endif
            }

            Exception connectException = null;

            foreach (IPAddress address in hostEntry.AddressList)
            {
                operation.ThrowIfDisposed(cancellationToken);

                try {
                    socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                }
                catch (Exception se) {
                    // The Socket ctor can throw if we run out of FD's
                    throw GetException(WebExceptionStatus.ConnectFailure, se);
                }
                IPEndPoint remote = new IPEndPoint(address, ServicePoint.Address.Port);
                socket.NoDelay = !ServicePoint.UseNagleAlgorithm;
                try {
                    ServicePoint.KeepAliveSetup(socket);
                } catch {
                    // Ignore. Not supported in all platforms.
                }

                if (!ServicePoint.CallEndPointDelegate(socket, remote))
                {
                    Interlocked.Exchange(ref socket, null)?.Close();
                    continue;
                }
                else
                {
                    try {
                        operation.ThrowIfDisposed(cancellationToken);

                        /*
                         * Socket.Tasks.cs from CoreFX introduces a new internal
                         * BeginConnect(EndPoint) overload, which will replace
                         * the one we're using from SocketTaskExtensions.cs.
                         *
                         * Our implementation of Socket.BeginConnect() does not
                         * invoke the callback when the request failed synchronously.
                         *
                         * Explicitly use our implementation from SocketTaskExtensions.cs here.
                         */
                        await Task.Factory.FromAsync(
                            (targetEndPoint, callback, state) => ((Socket)state).BeginConnect (targetEndPoint, callback, state),
                            asyncResult => ((Socket)asyncResult.AsyncState).EndConnect(asyncResult),
                            remote, socket).ConfigureAwait(false);
                    } catch (ObjectDisposedException) {
                        throw;
                    } catch (Exception exc) {
                        Interlocked.Exchange(ref socket, null)?.Close();
                        // Something went wrong, but we might have multiple IP Addresses
                        // and need to probe them all.
                        connectException = GetException(WebExceptionStatus.ConnectFailure, exc);
                        continue;
                    }
                }

                if (socket != null)
                {
                    return;
                }
            }

            if (connectException == null)
            {
                connectException = GetException(WebExceptionStatus.ConnectFailure, null);
            }

            throw connectException;
        }