System.Net.ConnectStream.EndRead C# (CSharp) Method

EndRead() public method

public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
return int
        public override int EndRead(IAsyncResult asyncResult) {
#if DEBUG
            using (GlobalLog.SetThreadKind(ThreadKinds.User)) {
#endif
            if (Logging.On) Logging.Enter(Logging.Web, this, "EndRead", "");

            //
            // parameter validation
            //
            if (asyncResult==null) {
                throw new ArgumentNullException("asyncResult");
            }

            int bytesTransferred;
            bool zeroLengthRead = false;
            if (asyncResult.GetType() == typeof(NestedSingleAsyncResult))
            {
                NestedSingleAsyncResult castedAsyncResult = (NestedSingleAsyncResult) asyncResult;
                if (castedAsyncResult.AsyncObject != this)
                {
                    throw new ArgumentException(SR.GetString(SR.net_io_invalidasyncresult), "asyncResult");
                }
                if (castedAsyncResult.EndCalled)
                {
                    throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndRead"));
                }
                castedAsyncResult.EndCalled = true;

                if (ErrorInStream)
                {
                    GlobalLog.LeaveException("ConnectStream::EndRead", m_ErrorException);
                    throw m_ErrorException;
                }

                object result = castedAsyncResult.InternalWaitForCompletion();

                Exception errorException = result as Exception;
                if (errorException != null)
                {
                    IOError(errorException, false);
                    bytesTransferred = -1;
                }
                else
                {
                    // If it's a NestedSingleAsyncResult, we completed it ourselves with our own result.
                    if (result == null)
                    {
                        bytesTransferred = 0;
                    }
                    else if (result == ZeroLengthRead)
                    {
                        bytesTransferred = 0;
                        zeroLengthRead = true;
                    }
                    else
                    {
                        try
                        {
                            bytesTransferred = (int) result;
                        }
                        catch (InvalidCastException)
                        {
                            bytesTransferred = -1;
                        }
                    }
                }
            }
            else
            {
                // If it's not a NestedSingleAsyncResult, we forwarded directly to the Connection and need to call EndRead.
                try
                {
                    bytesTransferred = m_Connection.EndRead(asyncResult);
                }
                catch (Exception exception)
                {
                    if (NclUtilities.IsFatal(exception)) throw;

                    IOError(exception, false);
                    bytesTransferred = -1;
                }
            }

            bytesTransferred = EndReadWithoutValidation(bytesTransferred, zeroLengthRead);

            Interlocked.CompareExchange(ref m_CallNesting, Nesting.Idle, Nesting.IoInProgress);
            GlobalLog.Print("EndRead() callNesting: " + m_CallNesting.ToString());

            if(Logging.On)Logging.Exit(Logging.Web, this, "EndRead", bytesTransferred);
            return bytesTransferred;
#if DEBUG
            }
#endif
        }