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

Read() public method

public Read ( [ buffer, int offset, int size ) : int
buffer [
offset int
size int
return int
        public override int Read([In, Out] byte[] buffer, int offset, int size) {
#if DEBUG
            using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Sync)) {
#endif
            if (Logging.On) Logging.Enter(Logging.Web, this, "Read", "");
            if (WriteStream) {
                throw new NotSupportedException(SR.GetString(SR.net_writeonlystream));
            }
            if (buffer==null) {
                throw new ArgumentNullException("buffer");
            }
            if (offset<0 || offset>buffer.Length) {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size<0 || size>buffer.Length-offset) {
                throw new ArgumentOutOfRangeException("size");
            }
            if (ErrorInStream) {
                throw m_ErrorException;
            }

            if (IsClosed) {
                throw new WebException(
                            NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.ConnectionClosed),
                            WebExceptionStatus.ConnectionClosed);
            }

            if (m_Request.Aborted) {
                GlobalLog.Print("ConnectStream#" + ValidationHelper.HashString(this) + "::InternalWrite() throwing");
                throw new WebException(
                            NetRes.GetWebStatusString("net_requestaborted", WebExceptionStatus.RequestCanceled),
                            WebExceptionStatus.RequestCanceled);
            }

            //
            // if we fail/hang this call for some reason,
            // this Nesting count we be non-0, so that when we
            // close this stream, we will abort the socket.
            //
            int nesting = Interlocked.CompareExchange(ref m_CallNesting, Nesting.IoInProgress, Nesting.Idle);
            GlobalLog.Print("Read() In: callNesting : " + m_CallNesting.ToString());

            if (nesting != Nesting.Idle)
            {
                throw new NotSupportedException(SR.GetString(SR.net_no_concurrent_io_allowed));
            }

            int bytesRead = -1;
            try
            {
                SafeSetSocketTimeout(SocketShutdown.Receive);
            }
            catch (Exception exception)
            {
                IOError(exception);
                throw;
            }
            try {
                bytesRead = ReadWithoutValidation(buffer, offset, size);
            }
            catch (Exception exception)
            {
                Win32Exception win32Exception = exception.InnerException as Win32Exception;
                if (win32Exception != null && win32Exception.NativeErrorCode == (int)SocketError.TimedOut)
                    exception = new WebException(SR.GetString(SR.net_timeout), WebExceptionStatus.Timeout);
                throw exception;
            }

            Interlocked.CompareExchange(ref m_CallNesting, Nesting.Idle, Nesting.IoInProgress);
            GlobalLog.Print("Read() Out: callNesting: " + m_CallNesting.ToString());

            if(Logging.On && bytesRead>0)Logging.Dump(Logging.Web, this, "Read", buffer, offset, bytesRead);
            if(Logging.On)Logging.Exit(Logging.Web, this, "Read", bytesRead);
            return bytesRead;
#if DEBUG
            }
#endif
        }