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 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 IConnection Connect(Uri uri, TimeSpan timeout)
        {
            WebSocketConnectAsyncResult webSocketConnectAsyncResult = (new WebSocketConnectAsyncResult(uri, timeout, this.webSocketRole, null, null)).RunSynchronously();
            IConnection clientWebSocketConnection = new ClientWebSocketConnection(webSocketConnectAsyncResult.ClientWebSocket, this.bufferSize, uri, new EventTraceActivity());

            MessagingClientEtwProvider.Provider.WebSocketConnectionEstablished(clientWebSocketConnection.Activity, webSocketConnectAsyncResult.Uri.AbsoluteUri);
            return(clientWebSocketConnection);
        }
        public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            WebSocketConnectAsyncResult webSocketConnectAsyncResult = (new WebSocketConnectAsyncResult(uri, timeout, "wsrelayedconnection", null, null)).RunSynchronously();
            ClientWebSocketConnection   clientWebSocketConnection   = new ClientWebSocketConnection(webSocketConnectAsyncResult.ClientWebSocket, this.bufferSize, uri, new EventTraceActivity());

            MessagingClientEtwProvider.Provider.WebSocketConnectionEstablished(clientWebSocketConnection.Activity, uri.AbsoluteUri);
            this.SendRelayedConnectAndReceiveResponse(clientWebSocketConnection, timeoutHelper);
            return(clientWebSocketConnection);
        }
        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 Microsoft.ServiceBus.Channels.IConnection EndConnect(IAsyncResult result)
        {
            WebSocketConnectAsyncResult webSocketConnectAsyncResult = AsyncResult <WebSocketConnectAsyncResult> .End(result);

            TimeoutHelper             timeoutHelper             = new TimeoutHelper(webSocketConnectAsyncResult.TimeRemaining());
            ClientWebSocketConnection clientWebSocketConnection = null;

            if (result.IsCompleted)
            {
                clientWebSocketConnection = new ClientWebSocketConnection(webSocketConnectAsyncResult.ClientWebSocket, this.bufferSize, webSocketConnectAsyncResult.Uri, new EventTraceActivity());
                MessagingClientEtwProvider.Provider.WebSocketConnectionEstablished(clientWebSocketConnection.Activity, webSocketConnectAsyncResult.Uri.AbsoluteUri);
                this.SendRelayedConnectAndReceiveResponse(clientWebSocketConnection, timeoutHelper);
            }
            return(clientWebSocketConnection);
        }
        private int TransferData(int numBytesRead, byte[] buffer, int offset, int size)
        {
            if (numBytesRead <= size)
            {
                Buffer.BlockCopy(this.AsyncReadBuffer, this.asyncReadBufferOffset, buffer, offset, numBytesRead);
                this.asyncReadBufferOffset = 0;
                this.remainingBytes        = 0;
                return(numBytesRead);
            }
            Buffer.BlockCopy(this.AsyncReadBuffer, this.asyncReadBufferOffset, buffer, offset, size);
            ClientWebSocketConnection clientWebSocketConnection = this;

            clientWebSocketConnection.asyncReadBufferOffset = clientWebSocketConnection.asyncReadBufferOffset + size;
            this.remainingBytes = numBytesRead - size;
            return(size);
        }
        private void SendRelayedConnectAndReceiveResponse(ClientWebSocketConnection connection, TimeoutHelper timeoutHelper)
        {
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(connection.Uri));

            TrackingIdHeader.TryAddOrUpdate(message.Headers, connection.Activity.ActivityId.ToString());
            if (this.tokenProvider != null)
            {
                string        absoluteUri = RelayedHttpUtility.ConvertToHttpUri(connection.Uri).AbsoluteUri;
                SecurityToken token       = this.tokenProvider.GetToken(absoluteUri, "Send", false, timeoutHelper.RemainingTime());
                message.Headers.Add(new RelayTokenHeader(token));
            }
            socketMessageHelper.SendMessage(connection, message, timeoutHelper.RemainingTime());
            Message message1 = socketMessageHelper.ReceiveMessage(connection, timeoutHelper.RemainingTime());

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
        }
 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);
        }