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

EndRead() private method

private EndRead ( HttpWebRequest request, IAsyncResult result ) : int
request HttpWebRequest
result IAsyncResult
return int
		internal int EndRead (HttpWebRequest request, IAsyncResult result)
		{
			lock (this) {
				if (Data.request != request)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
				if (nstream == null)
					throw new ObjectDisposedException (typeof (NetworkStream).FullName);
			}

			int nbytes = 0;
			WebAsyncResult wr = null;
			IAsyncResult nsAsync = ((WebAsyncResult) result).InnerAsyncResult;
			if (chunkedRead && (nsAsync is WebAsyncResult)) {
				wr = (WebAsyncResult) nsAsync;
				IAsyncResult inner = wr.InnerAsyncResult;
				if (inner != null && !(inner is WebAsyncResult))
					nbytes = nstream.EndRead (inner);
			} else if (!(nsAsync is WebAsyncResult)) {
				nbytes = nstream.EndRead (nsAsync);
				wr = (WebAsyncResult) result;
			}

			if (chunkedRead) {
				bool done = (nbytes == 0);
				try {
					chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
					if (!done && nbytes == 0 && chunkStream.WantMore)
						nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
				} catch (Exception e) {
					if (e is WebException)
						throw e;

					throw new WebException ("Invalid chunked data.", e,
								WebExceptionStatus.ServerProtocolViolation, null);
				}

				if ((done || nbytes == 0) && chunkStream.ChunkLeft != 0) {
					HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
					throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
				}
			}

			return (nbytes != 0) ? nbytes : -1;
		}

Usage Example

Esempio n. 1
0
        public override int EndRead(IAsyncResult r)
        {
            WebAsyncResult result = (WebAsyncResult)r;

            if (result.EndCalled)
            {
                int xx = result.NBytes;
                return((xx >= 0) ? xx : 0);
            }

            result.EndCalled = true;

            if (!result.IsCompleted)
            {
                int nbytes = -1;
                try {
                    nbytes = cnc.EndRead(request, result);
                } catch (Exception exc) {
                    lock (locker) {
                        pendingReads--;
                        if (pendingReads == 0)
                        {
                            pending.Set();
                        }
                    }

                    nextReadCalled = true;
                    cnc.Close(true);
                    result.SetCompleted(false, exc);
                    result.DoCallback();
                    throw;
                }

                if (nbytes < 0)
                {
                    nbytes   = 0;
                    read_eof = true;
                }

                totalRead += nbytes;
                result.SetCompleted(false, nbytes + result.NBytes);
                result.DoCallback();
                if (nbytes == 0)
                {
                    contentLength = totalRead;
                }
            }

            lock (locker) {
                pendingReads--;
                if (pendingReads == 0)
                {
                    pending.Set();
                }
            }

            if (totalRead >= contentLength && !nextReadCalled)
            {
                ReadAll();
            }

            int nb = result.NBytes;

            return((nb >= 0) ? nb : 0);
        }
All Usage Examples Of System.Net.WebConnection::EndRead