コード例 #1
0
ファイル: RequestStream.cs プロジェクト: sam-zhou/MaJiang
        public override IAsyncResult BeginRead(
            byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            var nread = fillFromBuffer(buffer, offset, count);

            if (nread > 0 || nread == -1)
            {
                var ares = new HttpStreamAsyncResult(callback, state);
                ares.Buffer   = buffer;
                ares.Offset   = offset;
                ares.Count    = count;
                ares.SyncRead = nread > 0 ? nread : 0;
                ares.Complete();

                return(ares);
            }

            // Avoid reading past the end of the request to allow for HTTP pipelining.
            if (_bodyLeft >= 0 && count > _bodyLeft)
            {
                count = (int)_bodyLeft;
            }

            return(_stream.BeginRead(buffer, offset, count, callback, state));
        }
コード例 #2
0
        public override IAsyncResult BeginRead(
            byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "A negative value.");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "A negative value.");
            }

            var len = buffer.Length;

            if (offset + count > len)
            {
                throw new ArgumentException(
                          "The sum of 'offset' and 'count' is greater than 'buffer' length.");
            }

            var ares = new HttpStreamAsyncResult(callback, state);

            if (_noMoreData)
            {
                ares.Complete();
                return(ares);
            }

            var nread = _decoder.Read(buffer, offset, count);

            offset += nread;
            count  -= nread;
            if (count == 0)
            {
                // Got all we wanted, no need to bother the decoder yet.
                ares.Count = nread;
                ares.Complete();

                return(ares);
            }

            if (!_decoder.WantMore)
            {
                _noMoreData = nread == 0;
                ares.Count  = nread;
                ares.Complete();

                return(ares);
            }

            ares.Buffer = new byte[_bufferLength];
            ares.Offset = 0;
            ares.Count  = _bufferLength;

            var rstate = new ReadBufferState(buffer, offset, count, ares);

            rstate.InitialCount += nread;
            base.BeginRead(ares.Buffer, ares.Offset, ares.Count, onRead, rstate);

            return(ares);
        }