Example #1
0
        // When this returns zero, the entire entity has been read.
        public override int EndRead(IAsyncResult asyncResult)
        {
            HttpWorkerRequest wr = _context.WorkerRequest;

            if (wr != null && wr.SupportsAsyncRead && !_context.IsInCancellablePeriod)
            {
                int totalBytesRead = _preloadedBytesRead;
                if (_preloadedBytesRead > 0)
                {
                    _preloadedBytesRead = 0;
                }
                int bytesRead = 0;
                try {
                    bytesRead = wr.EndRead(asyncResult);
                }
                catch (HttpException) {
                    if (_persistEntityBody)
                    {
                        SetRawContentOnce();
                    }
                    throw;
                }
                totalBytesRead += bytesRead;
                if (bytesRead > 0)
                {
                    if (_persistEntityBody)
                    {
                        if (_rawContent != null)
                        {
                            _rawContent.AddBytes(_buffer, _offset, bytesRead);
                        }
                        _buffer = null;
                        _offset = 0;
                        _count  = 0;
                    }
                    int dummy1 = 0, dummy2 = 0, dummy3 = 0;
                    UpdateCounters(bytesRead, ref dummy1, ref dummy2, ref dummy3);
                }
                if (_persistEntityBody
                    // we might attempt a read with count == 0, in which case bytesRead will
                    // be zero but we may not be done reading the entity body. Don't set raw
                    // content until bytesRead is 0 and count is not 0 or _remainingBytes is 0
                    && ((bytesRead == 0 && _count != 0) || _remainingBytes == 0))
                {
                    SetRawContentOnce();
                }
                return(totalBytesRead);
            }
            else
            {
                return(base.EndRead(asyncResult));
            }
        }