System.Net.WebConnection.BeginRead C# (CSharp) Method

BeginRead() private method

private BeginRead ( HttpWebRequest request, byte buffer, int offset, int size, AsyncCallback cb, object state ) : IAsyncResult
request HttpWebRequest
buffer byte
offset int
size int
cb AsyncCallback
state object
return IAsyncResult
		internal IAsyncResult BeginRead (HttpWebRequest request, byte [] buffer, int offset, int size, AsyncCallback cb, object state)
		{
			lock (this) {
				if (Data.request != request)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				if (nstream == null)
					return null;
			}

			IAsyncResult result = null;
			if (!chunkedRead || chunkStream.WantMore) {
				try {
					result = nstream.BeginRead (buffer, offset, size, cb, state);
					cb = null;
				} catch (Exception) {
					HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
					throw;
				}
			}

			if (chunkedRead) {
				WebAsyncResult wr = new WebAsyncResult (cb, state, buffer, offset, size);
				wr.InnerAsyncResult = result;
				if (result == null) {
					// Will be completed from the data in ChunkStream
					wr.SetCompleted (true, (Exception) null);
					wr.DoCallback ();
				}
				return wr;
			}

			return result;
		}
		

Usage Example

Esempio n. 1
0
        public override IAsyncResult BeginRead(byte [] buffer, int offset, int size,
                                               AsyncCallback cb, object state)
        {
            if (!isRead)
            {
                throw new NotSupportedException("this stream does not allow reading");
            }

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

            int length = buffer.Length;

            if (offset < 0 || length < offset)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size < 0 || (length - offset) < size)
            {
                throw new ArgumentOutOfRangeException("size");
            }

            lock (locker) {
                pendingReads++;
                pending.Reset();
            }

            WebAsyncResult result = new WebAsyncResult(cb, state, buffer, offset, size);

            if (totalRead >= contentLength)
            {
                result.SetCompleted(true, -1);
                result.DoCallback();
                return(result);
            }

            int remaining = readBufferSize - readBufferOffset;

            if (remaining > 0)
            {
                int copy = (remaining > size) ? size : remaining;
                Buffer.BlockCopy(readBuffer, readBufferOffset, buffer, offset, copy);
                readBufferOffset += copy;
                offset           += copy;
                size             -= copy;
                totalRead        += copy;
                if (size == 0 || totalRead >= contentLength)
                {
                    result.SetCompleted(true, copy);
                    result.DoCallback();
                    return(result);
                }
                result.NBytes = copy;
            }

            if (cb != null)
            {
                cb = cb_wrapper;
            }

            if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
            {
                size = contentLength - totalRead;
            }

            if (!read_eof)
            {
                result.InnerAsyncResult = cnc.BeginRead(request, buffer, offset, size, cb, result);
            }
            else
            {
                result.SetCompleted(true, result.NBytes);
                result.DoCallback();
            }
            return(result);
        }
All Usage Examples Of System.Net.WebConnection::BeginRead