public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
        {
            EventTraceActivity eventTraceActivity = new EventTraceActivity();
            TimeoutHelper      timeoutHelper      = new TimeoutHelper(timeout);
            WebStream          webStream          = (new WebStream(uri, "connection", this.useHttpsMode, eventTraceActivity, uri)).Open();

            Microsoft.ServiceBus.Channels.IConnection webStreamConnection = new WebStreamConnection(uri, this.bufferSize, eventTraceActivity, webStream, uri);
            webStreamConnection = SecureSocketUtil.InitiateSecureClientUpgradeIfNeeded(webStreamConnection, null, this.socketSecurityRole, uri.Host, timeoutHelper.RemainingTime());
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(uri));

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

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
            return(webStreamConnection);
        }
		public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
		{
			TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
			Uri uri1 = uri;
			for (int i = 0; i < 3; i++)
			{
				EventTraceActivity eventTraceActivity = new EventTraceActivity();
				Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				try
				{
					socket.Connect(uri1.Host, 9352);
					Microsoft.ServiceBus.Channels.IConnection socketConnection = new Microsoft.ServiceBus.Channels.SocketConnection(socket, this.bufferSize, eventTraceActivity);
					byte[] bytes = Encoding.UTF8.GetBytes(ConnectConstants.ConnectType);
					byte[] numArray = BitConverter.GetBytes((int)bytes.Length);
					socketConnection.Write(numArray, 0, (int)numArray.Length, true, timeoutHelper.RemainingTime());
					socketConnection.Write(bytes, 0, (int)bytes.Length, true, timeoutHelper.RemainingTime());
					byte[] bytes1 = Encoding.UTF8.GetBytes(uri1.ToString());
					byte[] numArray1 = BitConverter.GetBytes((int)bytes1.Length);
					socketConnection.Write(numArray1, 0, (int)numArray1.Length, true, timeoutHelper.RemainingTime());
					socketConnection.Write(bytes1, 0, (int)bytes1.Length, true, timeoutHelper.RemainingTime());
					socketConnection = SecureSocketUtil.InitiateSecureClientUpgradeIfNeeded(socketConnection, null, this.socketSecurityMode, uri.Host, timeoutHelper.RemainingTime());
					Message message = Message.CreateMessage(this.messageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(uri));
					TrackingIdHeader.TryAddOrUpdate(message.Headers, eventTraceActivity.ActivityId.ToString());
					if (this.tokenProvider != null)
					{
						SecurityToken token = this.tokenProvider.GetToken(RelayedHttpUtility.ConvertToHttpUri(uri).ToString(), "Send", false, timeoutHelper.RemainingTime());
						message.Headers.Add(new RelayTokenHeader(token));
					}
					this.messageHelper.SendMessage(socketConnection, message, timeoutHelper.RemainingTime());
					Message message1 = this.messageHelper.ReceiveMessage(socketConnection, timeoutHelper.RemainingTime());
					using (message1)
					{
						if (message1.Headers.Action == "Redirect")
						{
							uri1 = message1.GetBody<RedirectMessage>().Uri;
							socket.Close();
							goto Label1;
						}
						else if (message1.IsFault)
						{
							MessageFault messageFault = MessageFault.CreateFault(message1, 65536);
							throw Fx.Exception.AsError(ErrorUtility.ConvertToError(messageFault), eventTraceActivity);
						}
					}
					return socketConnection;
				}
				catch
				{
					socket.Close();
					throw;
				}
			Label1:
			}
			throw Fx.Exception.AsError(new CommunicationException(SRClient.MaxRedirectsExceeded(3)), null);
		}
        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));
            }
        }
示例#4
0
 private void EndConnectCallback(IAsyncResult ar)
 {
     try
     {
         Microsoft.ServiceBus.Channels.IConnection asyncState = (Microsoft.ServiceBus.Channels.IConnection)ar.AsyncState;
         Message message = this.messageHelper.EndReceiveMessage(ar);
         if (message.IsFault)
         {
             MessageFault messageFault = MessageFault.CreateFault(message, 65536);
             throw Fx.Exception.AsError(ErrorUtility.ConvertToError(messageFault), null);
         }
         base.EnqueueConnection(asyncState, false);
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         Fx.Exception.TraceHandled(exception, "WebSocketRelayedConnectionListener.EndConnectCallback", null);
     }
 }
示例#5
0
 private void EndConnectCallback(IAsyncResult ar)
 {
     Microsoft.ServiceBus.Channels.IConnection asyncState = (Microsoft.ServiceBus.Channels.IConnection)ar.AsyncState;
     try
     {
         Message message = this.messageHelper.EndReceiveMessage(ar);
         if (message.IsFault)
         {
             throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message, 65536));
         }
         this.listener.Success(this, asyncState);
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         asyncState.Abort();
         this.listener.Failure(this, exception);
     }
 }