public IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state)
        {
            IAsyncResult asyncResult;

            this.ThrowIfNotOpen();
            try
            {
                asyncResult = this.webSocket.BeginSend(buffer, offset, size, ServiceBusClientWebSocket.WebSocketMessageType.Binary, timeout, callback, state);
            }
            catch (IOException oException1)
            {
                IOException oException = oException1;
                throw Fx.Exception.AsError(ClientWebSocketConnection.ConvertIOException(oException), this.Activity);
            }
            catch (ObjectDisposedException objectDisposedException1)
            {
                ObjectDisposedException objectDisposedException = objectDisposedException1;
                if (this.webSocket.State != ServiceBusClientWebSocket.WebSocketState.Faulted)
                {
                    throw Fx.Exception.AsError(new CommunicationObjectAbortedException(objectDisposedException.Message, objectDisposedException), this.Activity);
                }
                throw Fx.Exception.AsError(new CommunicationObjectFaultedException(objectDisposedException.Message, objectDisposedException), this.Activity);
            }
            return(asyncResult);
        }
        public int EndRead()
        {
            if (Thread.VolatileRead(ref this.asyncReadPending) != 0)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(Resources.ClientWebSocketConnectionPrematureEndRead), this.Activity);
            }
            IAsyncResult asyncResult = this.readResult;

            if (asyncResult != null)
            {
                this.readResult   = null;
                this.readCallback = null;
                this.readObject   = null;
                try
                {
                    this.bytesRead = this.webSocket.EndReceive(asyncResult);
                }
                catch (IOException oException1)
                {
                    IOException oException = oException1;
                    throw Fx.Exception.AsError(ClientWebSocketConnection.ConvertIOException(oException), this.Activity);
                }
                catch (ObjectDisposedException objectDisposedException1)
                {
                    ObjectDisposedException objectDisposedException = objectDisposedException1;
                    if (this.webSocket.State != ServiceBusClientWebSocket.WebSocketState.Faulted)
                    {
                        throw Fx.Exception.AsError(new CommunicationObjectAbortedException(objectDisposedException.Message, objectDisposedException), this.Activity);
                    }
                    throw Fx.Exception.AsError(new CommunicationObjectFaultedException(objectDisposedException.Message, objectDisposedException), this.Activity);
                }
            }
            return(this.bytesRead);
        }
        public AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state)
        {
            this.ThrowIfNotOpen();
            Microsoft.ServiceBus.Channels.ConnectionUtilities.ValidateBufferBounds(this.AsyncReadBufferSize, offset, size);
            this.readCallback = callback;
            this.readObject   = state;
            AsyncReadResult asyncReadResult = AsyncReadResult.Completed;
            bool            flag            = false;

            try
            {
                try
                {
                    if (Interlocked.CompareExchange(ref this.asyncReadPending, 1, 0) != 0)
                    {
                        throw Fx.Exception.AsError(new InvalidOperationException(Resources.ClientWebSocketConnectionReadPending), this.Activity);
                    }
                    IAsyncResult asyncResult = this.webSocket.BeginReceive(this.AsyncReadBuffer, offset, size, timeout, this.onRead, this);
                    if (asyncResult.CompletedSynchronously)
                    {
                        this.readCallback = null;
                        this.readObject   = null;
                        this.bytesRead    = this.webSocket.EndReceive(asyncResult);
                    }
                    else
                    {
                        asyncReadResult = AsyncReadResult.Queued;
                    }
                    flag = true;
                }
                catch (IOException oException1)
                {
                    IOException oException = oException1;
                    throw Fx.Exception.AsError(ClientWebSocketConnection.ConvertIOException(oException), this.Activity);
                }
                catch (ObjectDisposedException objectDisposedException1)
                {
                    ObjectDisposedException objectDisposedException = objectDisposedException1;
                    if (this.webSocket.State != ServiceBusClientWebSocket.WebSocketState.Faulted)
                    {
                        throw Fx.Exception.AsError(new CommunicationObjectAbortedException(objectDisposedException.Message, objectDisposedException), this.Activity);
                    }
                    throw Fx.Exception.AsError(new CommunicationObjectFaultedException(objectDisposedException.Message, objectDisposedException), this.Activity);
                }
            }
            finally
            {
                if (!flag || asyncReadResult == AsyncReadResult.Completed)
                {
                    Interlocked.Exchange(ref this.asyncReadPending, 0);
                }
            }
            return(asyncReadResult);
        }
 public void EndWrite(IAsyncResult result)
 {
     try
     {
         this.webSocket.EndSend(result);
     }
     catch (IOException oException1)
     {
         IOException oException = oException1;
         throw Fx.Exception.AsError(ClientWebSocketConnection.ConvertIOException(oException), this.Activity);
     }
     catch (ObjectDisposedException objectDisposedException1)
     {
         ObjectDisposedException objectDisposedException = objectDisposedException1;
         if (this.webSocket.State != ServiceBusClientWebSocket.WebSocketState.Faulted)
         {
             throw Fx.Exception.AsError(new CommunicationObjectAbortedException(objectDisposedException.Message, objectDisposedException), this.Activity);
         }
         throw Fx.Exception.AsError(new CommunicationObjectFaultedException(objectDisposedException.Message, objectDisposedException), this.Activity);
     }
 }
        public int Read(byte[] buffer, int offset, int size, TimeSpan timeout)
        {
            int num;

            this.ThrowIfNotOpen();
            Microsoft.ServiceBus.Channels.ConnectionUtilities.ValidateBufferBounds(buffer, offset, size);
            try
            {
                if (this.asyncReadBufferOffset <= 0)
                {
                    IAsyncResult asyncResult = this.webSocket.BeginReceive(this.AsyncReadBuffer, 0, size, timeout, null, null);
                    this.bytesRead = this.webSocket.EndReceive(asyncResult);
                    num            = this.TransferData(this.bytesRead, buffer, offset, size);
                }
                else
                {
                    Fx.AssertAndThrow(this.remainingBytes > 0, "Must have data in buffer to transfer");
                    num = this.TransferData(this.remainingBytes, buffer, offset, size);
                }
            }
            catch (IOException oException1)
            {
                IOException oException = oException1;
                throw Fx.Exception.AsError(ClientWebSocketConnection.ConvertIOException(oException), this.Activity);
            }
            catch (ObjectDisposedException objectDisposedException1)
            {
                ObjectDisposedException objectDisposedException = objectDisposedException1;
                if (this.webSocket.State != ServiceBusClientWebSocket.WebSocketState.Faulted)
                {
                    throw Fx.Exception.AsError(new CommunicationObjectAbortedException(objectDisposedException.Message, objectDisposedException), this.Activity);
                }
                throw Fx.Exception.AsError(new CommunicationObjectFaultedException(objectDisposedException.Message, objectDisposedException), this.Activity);
            }
            return(num);
        }