Exemplo n.º 1
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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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));
 }
Exemplo n.º 5
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();
                }
            }
        }
Exemplo n.º 6
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
            {
                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;
            }
        }
Exemplo n.º 7
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));
 }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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));
 }
Exemplo n.º 11
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));
        }
Exemplo n.º 12
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));
        }
Exemplo n.º 13
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);
                }
            }
        }
Exemplo n.º 14
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);
 }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
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();
                    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();
                }
            }
        }
Exemplo n.º 17
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));
 }