Exemplo n.º 1
0
        private static WebExceptionStatus GetStatusFromExceptionHelper(HttpRequestException ex)
        {
            SocketException?socketEx = ex.InnerException as SocketException;

            if (socketEx is null)
            {
                return(WebExceptionStatus.UnknownError);
            }

            WebExceptionStatus status;

            switch (socketEx.SocketErrorCode)
            {
            case SocketError.NoData:
            case SocketError.HostNotFound:
                status = WebExceptionStatus.NameResolutionFailure;
                break;

            default:
                status = WebExceptionStatus.UnknownError;
                break;
            }

            return(status);
        }
Exemplo n.º 2
0
 public static String Format(this SocketException?instance)
 {
     return(instance.Format(( StringBuilder buffer ) =>
     {
         if (instance is not null)
         {
             buffer.AppendLine($"HResult: { String.Format( "0x{0:X8}", instance.HResult ) }");
             buffer.AppendLine($"Error code: { instance.ErrorCode }");
             buffer.AppendLine($"Socket error code: { instance.SocketErrorCode }");
             buffer.AppendLine(instance.StackTrace);
         }
     }));
 }
Exemplo n.º 3
0
        private void SyncFail(Exception e)
        {
            OnFail(false);

            if (_internalArgs != null)
            {
                _internalArgs.Dispose();
            }

            SocketException?socketException = e as SocketException;

            if (socketException != null)
            {
                _userArgs !.FinishConnectByNameSyncFailure(socketException, 0, SocketFlags.None);
            }
            else
            {
                ExceptionDispatchInfo.Throw(e);
            }
        }
Exemplo n.º 4
0
        // Callback which fires when an internal connection attempt completes.
        // If it failed and there are more addresses to try, do it.
        // Returns true if the operation is pending, false if it completed synchronously.
        private bool DoConnectCallback(SocketAsyncEventArgs args, bool sync)
        {
            Exception?exception = null;

            Debug.Assert(!Monitor.IsEntered(_lockObject));
            lock (_lockObject)
            {
                if (_state == State.Canceled)
                {
                    // If Cancel was called before we got the lock, the Socket will be closed soon.  We need to report
                    // OperationAborted (even though the connection actually completed), or the user will try to use a
                    // closed Socket.
                    exception = new SocketException((int)SocketError.OperationAborted);
                }
                else
                {
                    while (true)
                    {
                        Debug.Assert(_state == State.ConnectAttempt);

                        if (args.SocketError == SocketError.Success)
                        {
                            // The connection attempt succeeded; go to the completed state.
                            // The callback will be called outside the lock.
                            _state = State.Completed;
                            break;
                        }
                        else if (args.SocketError == SocketError.OperationAborted)
                        {
                            // The socket was closed while the connect was in progress.  This can happen if the user
                            // closes the socket, and is equivalent to a call to CancelConnectAsync
                            exception = new SocketException((int)SocketError.OperationAborted);
                            _state    = State.Canceled;
                            break;
                        }
                        else
                        {
                            // Keep track of this because it will be overwritten by AttemptConnection
                            SocketError currentFailure = args.SocketError;

                            (Exception? connectException, bool pending) = AttemptConnection();

                            if (connectException == null)
                            {
                                if (pending)
                                {
                                    // don't call the callback, another connection attempt is successfully started
                                    return(true);
                                }

                                // We have a sync completion from AttemptConnection.
                                // Loop around and process its results.
                            }
                            else
                            {
                                SocketException?socketException = connectException as SocketException;
                                if (socketException != null && socketException.SocketErrorCode == SocketError.NoData)
                                {
                                    // If the error is NoData, that means there are no more IPAddresses to attempt
                                    // a connection to.  Return the last error from an actual connection instead.
                                    exception = new SocketException((int)currentFailure);
                                }
                                else
                                {
                                    exception = connectException;
                                }

                                _state = State.Completed;
                                break;
                            }
                        }
                    }
                }
            }

            if (exception != null)
            {
                return(Fail(sync, exception));
            }
            else
            {
                return(Succeed(sync));
            }
        }
Exemplo n.º 5
0
 public SocketOperationResult(SocketException exception)
 {
     SocketError      = exception;
     BytesTransferred = 0;
 }
Exemplo n.º 6
0
 public SocketOperationResult(int bytesTransferred)
 {
     SocketError      = null;
     BytesTransferred = bytesTransferred;
 }
Exemplo n.º 7
0
        // Callback which fires when an internal connection attempt completes.
        // If it failed and there are more addresses to try, do it.
        private void InternalConnectCallback(object?sender, SocketAsyncEventArgs args)
        {
            Exception?exception = null;

            lock (_lockObject)
            {
                if (_state == State.Canceled)
                {
                    // If Cancel was called before we got the lock, the Socket will be closed soon.  We need to report
                    // OperationAborted (even though the connection actually completed), or the user will try to use a
                    // closed Socket.
                    exception = new SocketException((int)SocketError.OperationAborted);
                }
                else
                {
                    Debug.Assert(_state == State.ConnectAttempt);

                    if (args.SocketError == SocketError.Success)
                    {
                        // The connection attempt succeeded; go to the completed state.
                        // The callback will be called outside the lock.
                        _state = State.Completed;
                    }
                    else if (args.SocketError == SocketError.OperationAborted)
                    {
                        // The socket was closed while the connect was in progress.  This can happen if the user
                        // closes the socket, and is equivalent to a call to CancelConnectAsync
                        exception = new SocketException((int)SocketError.OperationAborted);
                        _state    = State.Canceled;
                    }
                    else
                    {
                        // Keep track of this because it will be overwritten by AttemptConnection
                        SocketError currentFailure   = args.SocketError;
                        Exception?  connectException = AttemptConnection();

                        if (connectException == null)
                        {
                            // don't call the callback, another connection attempt is successfully started
                            return;
                        }
                        else
                        {
                            SocketException?socketException = connectException as SocketException;
                            if (socketException != null && socketException.SocketErrorCode == SocketError.NoData)
                            {
                                // If the error is NoData, that means there are no more IPAddresses to attempt
                                // a connection to.  Return the last error from an actual connection instead.
                                exception = new SocketException((int)currentFailure);
                            }
                            else
                            {
                                exception = connectException;
                            }

                            _state = State.Completed;
                        }
                    }
                }
            }

            if (exception == null)
            {
                Succeed();
            }
            else
            {
                AsyncFail(exception);
            }
        }