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

BeginReadWithoutValidation() private method

private BeginReadWithoutValidation ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
size int
callback AsyncCallback
state object
return IAsyncResult
        private IAsyncResult BeginReadWithoutValidation(byte[] buffer, int offset, int size, AsyncCallback callback, object state) {
            GlobalLog.Enter("ConnectStream#" + ValidationHelper.HashString(this) + "::BeginReadWithoutValidation", ValidationHelper.HashString(m_Connection) + ", " + offset.ToString() + ", " + size.ToString());
            GlobalLog.ThreadContract(ThreadKinds.Unknown, "ConnectStream#" + ValidationHelper.HashString(this) + "::BeginReadWithoutValidation");

            //
            // Figure out how much we should really read.
            //

            int bytesToRead = 0;

            if (m_Chunked) {

                if (!m_ChunkEofRecvd) {

                    // See if we have more left from a previous
                    // chunk.

                    if (m_ChunkSize != 0) {
                        bytesToRead = Math.Min(size, m_ChunkSize);
                    }
                    else {
                        NestedSingleAsyncResult castedAsyncResult = new NestedSingleAsyncResult(this, state, callback, buffer, offset, size);

                        ThreadPool.QueueUserWorkItem(m_ReadChunkedCallbackDelegate, castedAsyncResult);

                        GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::BeginReadWithoutValidation() called QueueUserWorkItem(m_ReadChunkedCallbackDelegate)");
                        return castedAsyncResult;
                    }

                }
            }
            else {

                //
                // Not doing chunked, so don't read more than is left.
                //

                if (m_ReadBytes != -1) {
                    bytesToRead = (int)Math.Min(m_ReadBytes, (long)size);
                }
                else {
                    bytesToRead = size;
                }
            }

            // If we're not going to read anything, either because they're
            // not asking for anything or there's nothing left, bail
            // out now.

            if (bytesToRead == 0 || this.Eof) {
                NestedSingleAsyncResult completedResult = new NestedSingleAsyncResult(this, state, callback, ZeroLengthRead);
                GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::BeginReadWithoutValidation() completed, bytesToRead: " + bytesToRead + " Eof: " + this.Eof.ToString());
                return completedResult;
            }

            try
            {
                int bytesAlreadyRead = 0;
                if (m_ReadBufferSize > 0)
                {
                    bytesAlreadyRead = FillFromBufferedData(buffer, ref offset, ref bytesToRead);
                    if (bytesToRead == 0)
                    {
                        NestedSingleAsyncResult completedResult = new NestedSingleAsyncResult(this, state, callback, bytesAlreadyRead);
                        GlobalLog.Leave("ConnectStream::BeginReadWithoutValidation");
                        return completedResult;
                    }
                }

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

                GlobalLog.Assert(m_DoneCalled == 0 || m_ReadBytes != -1, "BeginRead: Calling BeginRead after ReadDone.|m_DoneCalled > 0 && m_ReadBytes == -1");

                // Keep track of this during the read so it can be added back at the end.
                m_BytesAlreadyTransferred = bytesAlreadyRead;

                IAsyncResult asyncResult = m_Connection.BeginRead(buffer, offset, bytesToRead, callback, state);

                // a null return indicates that the connection was closed underneath us.
                if (asyncResult == null)
                {
                    m_BytesAlreadyTransferred = 0;
                    m_ErrorException = new WebException(
                        NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled),
                        WebExceptionStatus.RequestCanceled);

                    GlobalLog.LeaveException("ConnectStream::BeginReadWithoutValidation", m_ErrorException);
                    throw m_ErrorException;
                }

                GlobalLog.Leave("ConnectStream#" + ValidationHelper.HashString(this) + "::BeginReadWithoutValidation() called BeginRead");
                return asyncResult;
            }
            catch (Exception exception) {
                IOError(exception);
                GlobalLog.LeaveException("ConnectStream#" + ValidationHelper.HashString(this) + "::BeginReadWithoutValidation", exception);
                throw;
            }
        }