Example #1
0
        public AsyncCompletionResult BeginRead(int offset, int size, TimeSpan timeout, Action <object> callback, object state)
        {
            ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size);
            _readCallback = callback;

            try
            {
                SetReadTimeout(timeout);
                Task <int> localTask = _stream.ReadAsync(AsyncReadBuffer, offset, size);
                //IAsyncResult localResult = stream.BeginRead(AsyncReadBuffer, offset, size, onRead, state);

                if (!localTask.IsCompleted)
                {
                    localTask.ContinueWith(_onRead, state);
                    return(AsyncCompletionResult.Queued);
                }

                _bytesRead = localTask.GetAwaiter().GetResult();
            }
            catch (IOException ioException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException));
            }

            return(AsyncCompletionResult.Completed);
        }
Example #2
0
        public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout)
        {
            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
            TimeoutHelper helper = new TimeoutHelper(timeout);

            try
            {
                this.SetImmediate(immediate);
                int num = size;
                while (num > 0)
                {
                    this.SetWriteTimeout(helper.RemainingTime(), true);
                    size = Math.Min(num, 0x10000);
                    this.socket.Send(buffer, offset, size, SocketFlags.None);
                    num    -= size;
                    offset += size;
                    timeout = helper.RemainingTime();
                }
            }
            catch (SocketException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertSendException(exception, helper.RemainingTime()), this.ExceptionEventType);
            }
            catch (ObjectDisposedException exception2)
            {
                Exception objA = this.ConvertObjectDisposedException(exception2, TransferOperation.Write);
                if (object.ReferenceEquals(objA, exception2))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelper(objA, this.ExceptionEventType);
            }
        }
Example #3
0
        public IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state)
        {
            IAsyncResult result2;

            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
            try
            {
                lock (this.ThisLock)
                {
                    this.SetImmediate(immediate);
                    this.SetWriteTimeout(timeout, false);
                    this.asyncWritePending = true;
                }
                result2 = this.socket.BeginSend(buffer, offset, size, SocketFlags.None, callback, state);
            }
            catch (SocketException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertSendException(exception, TimeSpan.MaxValue), this.ExceptionEventType);
            }
            catch (ObjectDisposedException exception2)
            {
                Exception objA = this.ConvertObjectDisposedException(exception2, TransferOperation.Write);
                if (object.ReferenceEquals(objA, exception2))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelper(objA, this.ExceptionEventType);
            }
            return(result2);
        }
Example #4
0
            private void CompleteReadFaultData()
            {
                int offset = 0;
                int size   = this.connection.EndRead();

                while (size > 0)
                {
                    int num3 = this.decoder.Decode(this.connection.AsyncReadBuffer, offset, size);
                    offset += num3;
                    size   -= num3;
                    if (this.decoder.CurrentState == ClientFramingDecoderState.Fault)
                    {
                        ConnectionUtilities.CloseNoThrow(this.connection, this.timeoutHelper.RemainingTime());
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(this.decoder.Fault, this.via.ToString(), this.contentType));
                    }
                    if (this.decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                    {
                        throw Fx.AssertAndThrow("invalid framing client state machine");
                    }
                    if (size == 0)
                    {
                        offset = 0;
                        if (this.connection.BeginRead(0, Math.Min(0x100, this.connection.AsyncReadBufferSize), this.timeoutHelper.RemainingTime(), onReadFaultData, this) == AsyncReadResult.Queued)
                        {
                            return;
                        }
                        size = this.connection.EndRead();
                    }
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
            }
Example #5
0
        public static void DecodeFramingFault(ClientFramingDecoder decoder, IConnection connection, Uri via, string contentType, ref TimeoutHelper timeoutHelper)
        {
            ValidateReadingFaultString(decoder);
            int offset = 0;

            byte[] buffer = DiagnosticUtility.Utility.AllocateByteArray(0x100);
            int    size   = connection.Read(buffer, offset, buffer.Length, timeoutHelper.RemainingTime());

            while (size > 0)
            {
                int num3 = decoder.Decode(buffer, offset, size);
                offset += num3;
                size   -= num3;
                if (decoder.CurrentState == ClientFramingDecoderState.Fault)
                {
                    ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType));
                }
                if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                {
                    throw Fx.AssertAndThrow("invalid framing client state machine");
                }
                if (size == 0)
                {
                    offset = 0;
                    size   = connection.Read(buffer, offset, buffer.Length, timeoutHelper.RemainingTime());
                }
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
        }
Example #6
0
 public virtual AsyncCompletionResult BeginRead(int offset, int size, TimeSpan timeout,
                                                Action <object> callback, object state)
 {
     ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size);
     this.ThrowIfNotOpen();
     return(this.BeginReadCore(offset, size, timeout, callback, state));
 }
Example #7
0
        public static async Task DecodeFramingFaultAsync(ClientFramingDecoder decoder, IConnection connection,
                                                         Uri via, string contentType, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            ValidateReadingFaultString(decoder);

            var tcs    = new TaskCompletionSource <bool>();
            var result = connection.BeginRead(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                              timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs);

            if (result == AsyncCompletionResult.Completed)
            {
                tcs.TrySetResult(true);
            }

            await tcs.Task;

            int offset = 0;
            int size   = connection.EndRead();

            while (size > 0)
            {
                int bytesDecoded = decoder.Decode(connection.AsyncReadBuffer, offset, size);
                offset += bytesDecoded;
                size   -= bytesDecoded;

                if (decoder.CurrentState == ClientFramingDecoderState.Fault)
                {
                    ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType));
                }
                else
                {
                    if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                    {
                        throw new Exception("invalid framing client state machine");
                    }
                    if (size == 0)
                    {
                        offset = 0;
                        tcs    = new TaskCompletionSource <bool>();
                        result = connection.BeginRead(0, Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                                      timeoutHelper.RemainingTime(), FramingDuplexSessionChannel.OnIoComplete, tcs);
                        if (result == AsyncCompletionResult.Completed)
                        {
                            tcs.TrySetResult(true);
                        }

                        await tcs.Task;
                        size = connection.EndRead();
                    }
                }
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
        }
Example #8
0
        protected override AsyncCompletionResult BeginWriteCore(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout,
                                                                Action <object> callback, object state)
        {
            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
            bool abortWrite = true;

            try
            {
                lock (ThisLock)
                {
                    Contract.Assert(!_asyncWritePending, "Called BeginWrite twice.");
                    this.ThrowIfClosed();
                    this.EnsureWriteEventArgs();
                    SetImmediate(immediate);
                    SetWriteTimeout(timeout, false);
                    this.SetUserToken(_asyncWriteEventArgs, this);
                    _asyncWritePending  = true;
                    _asyncWriteCallback = callback;
                    _asyncWriteState    = state;
                }

                _asyncWriteEventArgs.SetBuffer(buffer, offset, size);

                if (_socket.SendAsync(_asyncWriteEventArgs))
                {
                    abortWrite = false;
                    return(AsyncCompletionResult.Queued);
                }

                this.HandleSendAsyncCompleted();
                abortWrite = false;
                return(AsyncCompletionResult.Completed);
            }
            catch (SocketException socketException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          ConvertSendException(socketException, TimeSpan.MaxValue));
            }
            catch (ObjectDisposedException objectDisposedException)
            {
                Exception exceptionToThrow = ConvertObjectDisposedException(objectDisposedException, TransferOperation.Write);
                if (object.ReferenceEquals(exceptionToThrow, objectDisposedException))
                {
                    throw;
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exceptionToThrow);
                }
            }
            finally
            {
                if (abortWrite)
                {
                    this.AbortWrite();
                }
            }
        }
        protected override void WriteCore(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout)
        {
            // as per http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b201213
            // we shouldn't write more than 64K synchronously to a socket
            const int maxSocketWrite = 64 * 1024;

            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            try
            {
                int bytesToWrite = size;

                using (timeoutHelper.GetCancellationToken().Register(OnSendTimeout, this))
                {
                    while (bytesToWrite > 0)
                    {
                        size = Math.Min(bytesToWrite, maxSocketWrite);
                        _outputStream.Write(buffer, offset, size);
                        if (immediate)
                        {
                            _outputStream.Flush();
                        }

                        bytesToWrite -= size;
                        offset       += size;
                    }
                }
            }
            catch (ObjectDisposedException objectDisposedException)
            {
                Exception exceptionToThrow = ConvertObjectDisposedException(objectDisposedException, TransferOperation.Write);
                if (object.ReferenceEquals(exceptionToThrow, objectDisposedException))
                {
                    throw;
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exceptionToThrow);
                }
            }
            catch (Exception exception)
            {
                if (RTSocketError.GetStatus(exception.HResult) != SocketErrorStatus.Unknown)
                {
                    SocketException socketException = new SocketException(exception.HResult & 0x0000FFFF);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              ConvertSendException(socketException, timeoutHelper.RemainingTime()));
                }
                throw;
            }
        }
Example #10
0
        public int Read(byte[] buffer, int offset, int size, TimeSpan timeout)
        {
            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
            ThrowIfNotOpen();
            int bytesRead = ReadCore(buffer, offset, size, timeout, false);

            if (WcfEventSource.Instance.SocketReadStopIsEnabled())
            {
                TraceSocketReadStop(bytesRead, false);
            }

            return(bytesRead);
        }
Example #11
0
 public override int Read(byte[] buffer, int offset, int size, TimeSpan timeout)
 {
     ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
     if (this.preReadCount > 0)
     {
         int count = Math.Min(size, this.preReadCount);
         Buffer.BlockCopy(this.preReadData, this.preReadOffset, buffer, offset, count);
         this.preReadOffset += count;
         this.preReadCount  -= count;
         return(count);
     }
     return(base.Read(buffer, offset, size, timeout));
 }
Example #12
0
 public override AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state)
 {
     ConnectionUtilities.ValidateBufferBounds(this.AsyncReadBufferSize, offset, size);
     if (this.preReadCount > 0)
     {
         int count = Math.Min(size, this.preReadCount);
         Buffer.BlockCopy(this.preReadData, this.preReadOffset, this.AsyncReadBuffer, offset, count);
         this.preReadOffset += count;
         this.preReadCount  -= count;
         this.asyncBytesRead = count;
         return(AsyncReadResult.Completed);
     }
     return(base.BeginRead(offset, size, timeout, callback, state));
 }
Example #13
0
        public virtual AsyncCompletionResult BeginRead(int offset, int size, TimeSpan timeout,
                                                       Action <object> callback, object state)
        {
            ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size);
            this.ThrowIfNotOpen();
            var completionResult = this.BeginReadCore(offset, size, timeout, callback, state);

            if (completionResult == AsyncCompletionResult.Completed && WcfEventSource.Instance.SocketReadStopIsEnabled())
            {
                TraceSocketReadStop(_asyncReadSize, true);
            }

            return(completionResult);
        }
Example #14
0
        public override int Read(byte[] buffer, int offset, int size, TimeSpan timeout)
        {
            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);

            if (_preReadCount > 0)
            {
                int bytesToCopy = Math.Min(size, _preReadCount);
                Buffer.BlockCopy(_preReadData, _preReadOffset, buffer, offset, bytesToCopy);
                _preReadOffset += bytesToCopy;
                _preReadCount  -= bytesToCopy;
                return(bytesToCopy);
            }

            return(base.Read(buffer, offset, size, timeout));
        }
Example #15
0
        public override AsyncCompletionResult BeginRead(int offset, int size, TimeSpan timeout, Action <object> callback, object state)
        {
            ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size);

            if (_preReadCount > 0)
            {
                int bytesToCopy = Math.Min(size, _preReadCount);
                Buffer.BlockCopy(_preReadData, _preReadOffset, AsyncReadBuffer, offset, bytesToCopy);
                _preReadOffset += bytesToCopy;
                _preReadCount  -= bytesToCopy;
                _asyncBytesRead = bytesToCopy;
                return(AsyncCompletionResult.Completed);
            }

            return(base.BeginRead(offset, size, timeout, callback, state));
        }
Example #16
0
        public static async Task DecodeFramingFaultAsync(ClientFramingDecoder decoder, IConnection connection,
                                                         Uri via, string contentType, TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);

            ValidateReadingFaultString(decoder);

            int size = await connection.ReadAsync(0,
                                                  Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                                  timeoutHelper.RemainingTime());

            int offset = 0;

            while (size > 0)
            {
                int bytesDecoded = decoder.Decode(connection.AsyncReadBuffer, offset, size);
                offset += bytesDecoded;
                size   -= bytesDecoded;

                if (decoder.CurrentState == ClientFramingDecoderState.Fault)
                {
                    ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              FaultStringDecoder.GetFaultException(decoder.Fault, via.ToString(), contentType));
                }
                else
                {
                    if (decoder.CurrentState != ClientFramingDecoderState.ReadingFaultString)
                    {
                        throw new Exception("invalid framing client state machine");
                    }
                    if (size == 0)
                    {
                        offset = 0;
                        size   = await connection.ReadAsync(0,
                                                            Math.Min(FaultStringDecoder.FaultSizeQuota, connection.AsyncReadBufferSize),
                                                            timeoutHelper.RemainingTime());
                    }
                }
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
        }
Example #17
0
        protected override void WriteCore(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout)
        {
            // as per http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b201213
            // we shouldn't write more than 64K synchronously to a socket
            const int maxSocketWrite = 64 * 1024;

            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            try
            {
                SetImmediate(immediate);
                int bytesToWrite = size;

                while (bytesToWrite > 0)
                {
                    SetWriteTimeout(timeoutHelper.RemainingTime(), true);
                    size = Math.Min(bytesToWrite, maxSocketWrite);
                    _socket.Send(buffer, offset, size, SocketFlags.None);
                    bytesToWrite -= size;
                    offset       += size;
                    timeout       = timeoutHelper.RemainingTime();
                }
            }
            catch (SocketException socketException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          ConvertSendException(socketException, timeoutHelper.RemainingTime()));
            }
            catch (ObjectDisposedException objectDisposedException)
            {
                Exception exceptionToThrow = ConvertObjectDisposedException(objectDisposedException, TransferOperation.Write);
                if (object.ReferenceEquals(exceptionToThrow, objectDisposedException))
                {
                    throw;
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exceptionToThrow);
                }
            }
        }
Example #18
0
 public AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state)
 {
     ConnectionUtilities.ValidateBufferBounds(this.AsyncReadBufferSize, offset, size);
     this.readCallback = callback;
     try
     {
         this.SetReadTimeout(timeout);
         IAsyncResult asyncResult = this.stream.BeginRead(this.AsyncReadBuffer, offset, size, this.onRead, state);
         if (!asyncResult.CompletedSynchronously)
         {
             return(AsyncReadResult.Queued);
         }
         this.bytesRead = this.stream.EndRead(asyncResult);
     }
     catch (IOException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.ConvertIOException(exception));
     }
     return(AsyncReadResult.Completed);
 }
Example #19
0
        public AsyncCompletionResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state)
        {
            ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size);
            readCallback = callback;

            try
            {
                SetReadTimeout(timeout);
                IAsyncResult localResult = stream.BeginRead(AsyncReadBuffer, offset, size, onRead, state);

                if (!localResult.CompletedSynchronously)
                {
                    return(AsyncCompletionResult.Queued);
                }

                bytesRead = stream.EndRead(localResult);
            }
            catch (IOException ioException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException));
            }

            return(AsyncCompletionResult.Completed);
        }
        protected override AsyncCompletionResult BeginWriteCore(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout,
                                                                Action <object> callback, object state)
        {
            ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
            bool abortWrite = true;

            try
            {
                lock (ThisLock)
                {
                    Contract.Assert(!_asyncWritePending, "Called BeginWrite twice.");
                    this.ThrowIfClosed();
                    SetWriteTimeout(timeout, false);
                    _asyncWritePending  = true;
                    _asyncWriteCallback = callback;
                    _asyncWriteState    = state;
                }

                Task writeTask = _outputStream.WriteAsync(buffer, offset, size, _sendCts.Token);
                if (immediate)
                {
                    writeTask = writeTask.ContinueWith(s_flushWriteImmedaite, this, CancellationToken.None).Unwrap();
                }

                if (!writeTask.IsCompleted)
                {
                    writeTask.ContinueWith(s_onSendAsyncCompleted, this, CancellationToken.None);
                    abortWrite = false;
                    return(AsyncCompletionResult.Queued);
                }

                writeTask.GetAwaiter().GetResult();
                abortWrite = false;
                return(AsyncCompletionResult.Completed);
            }
            catch (ObjectDisposedException objectDisposedException)
            {
                Exception exceptionToThrow = ConvertObjectDisposedException(objectDisposedException, TransferOperation.Write);
                if (object.ReferenceEquals(exceptionToThrow, objectDisposedException))
                {
                    throw;
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exceptionToThrow);
                }
            }
            catch (Exception exception)
            {
                if (RTSocketError.GetStatus(exception.HResult) != SocketErrorStatus.Unknown)
                {
                    SocketException socketException = new SocketException(exception.HResult & 0x0000FFFF);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              ConvertSendException(socketException, TimeSpan.MaxValue));
                }
                throw;
            }
            finally
            {
                if (abortWrite)
                {
                    this.AbortWrite();
                }
            }
        }
        internal static void SendFault(IConnection connection, string faultString, byte[] drainBuffer, TimeSpan sendTimeout, int maxRead)
        {
            if (TD.ConnectionReaderSendFaultIsEnabled())
            {
                TD.ConnectionReaderSendFault(faultString);
            }

            EncodedFault  encodedFault  = new EncodedFault(faultString);
            TimeoutHelper timeoutHelper = new TimeoutHelper(sendTimeout);

            try
            {
                connection.Write(encodedFault.EncodedBytes, 0, encodedFault.EncodedBytes.Length, true, timeoutHelper.RemainingTime());
                connection.Shutdown(timeoutHelper.RemainingTime());
            }
            catch (CommunicationException e)
            {
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                connection.Abort();
                return;
            }
            catch (TimeoutException e)
            {
                if (TD.SendTimeoutIsEnabled())
                {
                    TD.SendTimeout(e.Message);
                }
                DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                connection.Abort();
                return;
            }

            // make sure we read until EOF or a quota is hit
            int read      = 0;
            int readTotal = 0;

            for (;;)
            {
                try
                {
                    read = connection.Read(drainBuffer, 0, drainBuffer.Length, timeoutHelper.RemainingTime());
                }
                catch (CommunicationException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                    connection.Abort();
                    return;
                }
                catch (TimeoutException e)
                {
                    if (TD.SendTimeoutIsEnabled())
                    {
                        TD.SendTimeout(e.Message);
                    }
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                    connection.Abort();
                    return;
                }

                if (read == 0)
                {
                    break;
                }

                readTotal += read;
                if (readTotal > maxRead || timeoutHelper.RemainingTime() <= TimeSpan.Zero)
                {
                    connection.Abort();
                    return;
                }
            }

            ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime());
        }
Example #22
0
 public int Read(byte[] buffer, int offset, int size, TimeSpan timeout)
 {
     ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
     this.ThrowIfNotOpen();
     return(this.ReadCore(buffer, offset, size, timeout, false));
 }
        internal static void SendFault(IConnection connection, string faultString, byte[] drainBuffer, TimeSpan sendTimeout, int maxRead)
        {
            EncodedFault  fault  = new EncodedFault(faultString);
            TimeoutHelper helper = new TimeoutHelper(sendTimeout);

            try
            {
                connection.Write(fault.EncodedBytes, 0, fault.EncodedBytes.Length, true, helper.RemainingTime());
                connection.Shutdown(helper.RemainingTime());
            }
            catch (CommunicationException exception)
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
                }
                connection.Abort();
                return;
            }
            catch (TimeoutException exception2)
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information);
                }
                connection.Abort();
                return;
            }
            int num  = 0;
            int num2 = 0;

            do
            {
                try
                {
                    num = connection.Read(drainBuffer, 0, drainBuffer.Length, helper.RemainingTime());
                }
                catch (CommunicationException exception3)
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Information);
                    }
                    connection.Abort();
                    return;
                }
                catch (TimeoutException exception4)
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        DiagnosticUtility.ExceptionUtility.TraceHandledException(exception4, TraceEventType.Information);
                    }
                    connection.Abort();
                    return;
                }
                if (num == 0)
                {
                    ConnectionUtilities.CloseNoThrow(connection, helper.RemainingTime());
                    return;
                }
                num2 += num;
            }while ((num2 <= maxRead) && (helper.RemainingTime() > TimeSpan.Zero));
            connection.Abort();
        }