/// <summary> /// Try to begin receive messages /// </summary> /// <param name="ccs">indicating the channel and the client</param> /// <returns>if the operation completed synchronously</returns> protected override bool TryToBeginReceive(ChannelClientState state) { T channel = (T)((ChannelClientState)state).Channel; IAsyncResult ar = null; try { ar = channel.BeginReceiveRequest(this.receiveRequest, state); } catch (Exception e) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[RequestReplyFrontEnd] Exception throwed while begin receive messages: {0}", e); // Channel must be in falted state lock (channel) { if (channel.State == CommunicationState.Faulted) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[RequestReplyFrontEnd] About the channel."); // About the falted channel channel.Abort(); return(false); } } } return(ar.CompletedSynchronously && channel.State == CommunicationState.Opened); }
/// <summary> /// Try to begin receive messages /// </summary> /// <param name="ccs">indicating the channel and the client</param> /// <returns>if the operation completed synchronously</returns> protected override bool TryToBeginReceive(ChannelClientState ccs) { IDuplexSessionChannel channel = (IDuplexSessionChannel)ccs.Channel; BrokerClient client = ccs.Client; IAsyncResult ar = null; try { ar = channel.BeginReceive(this.receiveRequest, ccs); } catch (Exception e) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[DuplexFrontEnd] Exception throwed while begin receive messages: {0}", e); // Channel must be in falted state lock (channel) { if (channel.State == CommunicationState.Faulted) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[DuplexFrontEnd] About the channel."); this.FrontendDisconnect(channel, client); // About the falted channel channel.Abort(); } } return(false); } return(ar.CompletedSynchronously && channel.State == CommunicationState.Opened); }
/// <summary> /// Wrapper to call TryToBeginReceiveMessagesWithThrottling /// Repeat to call this method if it has been completed synchronously /// </summary> /// <param name="state">indicating the channel client state</param> protected void TryToBeginReceiveMessagesWithThrottling(ChannelClientState state) { bool completedSynchronously = false; do { completedSynchronously = this.TryToBeginReceiveMessagesWithThrottlingInternal(state); }while (completedSynchronously); }
/// <summary> /// Try to receive messages with throttling /// </summary> /// <param name="state">indicating the state</param> /// <returns>indicating whether the operation completed synchronously</returns> private bool TryToBeginReceiveMessagesWithThrottlingInternal(ChannelClientState state) { if (this.throttling == 0) { return(this.TryToBeginReceive(state)); } else { lock (this.callbackStateQueueLock) { this.callbackStateQueue.Enqueue(state); } return(false); } }
/// <summary> /// Try to begin receive messages /// </summary> /// <param name="ccs">indicating the channel and the client</param> /// <returns>if the operation completed synchronously</returns> protected override bool TryToBeginReceive(ChannelClientState state) { // IAsyncResult ar = new AsyncResult(state); IAsyncResult ar = null; try { ar = this.azureQueueProxy.BeginReceiveRequest(this.receiveRequest, state); } catch (Exception e) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[AzureQueueFrontEnd] Exception throwed while begin receive messages: {0}", e); } return(ar.CompletedSynchronously); }
/// <summary> /// Override this method to implement real begin receive logic /// </summary> /// <param name="state">indicating the channel and the client</param> /// <returns>if the operation completed synchronously</returns> protected abstract bool TryToBeginReceive(ChannelClientState state);
/// <summary> /// AsyncCallback for ReceiveRequest /// </summary> /// <param name="ar">async result</param> private void ReceiveRequest(IAsyncResult ar) { ChannelClientState state = (ChannelClientState)ar.AsyncState; T channel = (T)state.Channel; BrokerClient client = state.Client; RequestContext request; try { request = channel.EndReceiveRequest(ar); } catch (TimeoutException) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[RequestReplyFrontEnd] Receive Request timed out"); if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } catch (CommunicationException ce) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[RequestReplyFrontEnd] Exception while receiving requests: {0}", ce.Message); // Retry receiving requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } #region Debug Failure Test SimulateFailure.FailOperation(1); #endregion // After channel timed out, the request will be null if you call channel.ReceiveRequest() // Need to end the channel at this time if (request == null) { // Indicate that the channel should be closed // Close the channel lock (channel) { if (channel.State == CommunicationState.Opened) { try { channel.Close(); } catch (CommunicationException ce) { BrokerTracing.TraceEvent(TraceEventType.Warning, 0, "[RequestReplyFrontEnd] Exception throwed while close the channel: {0}", ce); channel.Abort(); } } } return; } // Try to get the client id and the user name string callerSID; string userName = this.GetUserName(request.RequestMessage, out callerSID); string clientId = GetClientId(request.RequestMessage, callerSID); try { ParamCheckUtility.ThrowIfTooLong(clientId.Length, "clientId", Constant.MaxClientIdLength, SR.ClientIdTooLong); ParamCheckUtility.ThrowIfNotMatchRegex(ParamCheckUtility.ClientIdValid, clientId, "clientId", SR.InvalidClientId); } catch (ArgumentException) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedClientIdInvalid(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage)); RequestContextBase requestContextToReject = new RequestReplyRequestContext(request, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.GenerateFaultMessage(request.RequestMessage, request.RequestMessage.Headers.MessageVersion, SOAFaultCode.Broker_InvalidClientIdOrTooLong, SR.InvalidClientIdOrTooLong), this.ReplySentCallback, requestContextToReject); return; } if (client == null || client.State == BrokerClientState.Disconnected || String.Compare(clientId, client.ClientId, StringComparison.OrdinalIgnoreCase) != 0) { try { client = this.ClientManager.GetClient(clientId, userName); client.SingletonInstanceConnected(); } catch (FaultException <SessionFault> e) { if (e.Detail.Code == (int)SOAFaultCode.AccessDenied_BrokerQueue) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedAuthenticationError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), userName); RequestContextBase requestContextToReject = new RequestReplyRequestContext(request, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.GenerateFaultMessage(request.RequestMessage, request.RequestMessage.Headers.MessageVersion, SOAFaultCode.Broker_UserNameNotMatch, SR.UserNameNotMatch, userName, clientId), this.ReplySentCallback, requestContextToReject); return; } else { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), e.ToString()); throw; } } catch (BrokerQueueException e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), e.ToString()); RequestContextBase requestContextToReject = new RequestReplyRequestContext(request, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.TranslateBrokerQueueExceptionToFaultMessage(e, request.RequestMessage), this.ReplySentCallback, requestContextToReject); return; } catch (Exception e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), e.ToString()); throw; } state.Client = client; } // Receive new requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } // Try to get the user info header bool userDataFlag = GetUserInfoHeader(request.RequestMessage); // Create request context RequestContextBase requestContext; if (userDataFlag) { // Message that needs not to reply requestContext = DummyRequestContext.GetInstance(request.RequestMessage.Version); // Reply the message to the client immediately if user data is found for request/reply MEP (basic http binding) try { request.BeginReply(Message.CreateMessage(request.RequestMessage.Headers.MessageVersion, request.RequestMessage.Headers.Action + "Response"), this.ReplySent, request); } catch (Exception e) { BrokerTracing.TraceEvent(TraceEventType.Error, 0, "[RequestReplyFrontEnd] Exception throwed while trying to reply dummy message to client: {0}", e); } } else { requestContext = new RequestReplyRequestContext(request, this.Observer, client); } // Check auth if (!this.CheckAuth(requestContext, request.RequestMessage)) { return; } // Bug 15195: Remove security header for https TryRemoveSecurityHeaderForHttps(request.RequestMessage); // Send request to the broker client client.RequestReceived(requestContext, request.RequestMessage, null); }
/// <summary> /// AsyncCallback for ReceiveRequest /// </summary> /// <param name="ar">async result</param> private void ReceiveRequest(IAsyncResult ar) { ChannelClientState state = (ChannelClientState)ar.AsyncState; IDuplexSessionChannel channel = (IDuplexSessionChannel)state.Channel; BrokerClient client = state.Client; Message requestMessage; try { requestMessage = channel.EndReceive(ar); } catch (Exception ce) { BrokerTracing.TraceEvent(TraceEventType.Warning, 0, "[DuplexFrontEnd] Exception while receiving requests: {0}", ce); this.FrontendDisconnect(channel, client); lock (channel) { if (channel.State == CommunicationState.Faulted) { BrokerTracing.TraceEvent(TraceEventType.Warning, 0, "[DuplexFrontEnd] Abort faulted channel."); channel.Abort(); return; } } // Retry receiving requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } #region Debug Failure Test SimulateFailure.FailOperation(1); #endregion // After channel timeout, the request will be null if you call channel.Receive() // Need to end the channel at this time if (requestMessage == null) { // Indicate that the channel should be closed // Close the channel lock (channel) { if (channel.State == CommunicationState.Opened) { this.FrontendDisconnect(channel, client); try { channel.Close(); BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[DuplexFrontEnd] Channel closed"); } catch (Exception ce) { BrokerTracing.TraceEvent(TraceEventType.Warning, 0, "[DuplexFrontEnd] Exception throwed while close the channel: {0}", ce); channel.Abort(); } } } return; } // Try to get the client id string callerSID; string userName = this.GetUserName(requestMessage, out callerSID); string clientId = GetClientId(requestMessage, callerSID); try { ParamCheckUtility.ThrowIfTooLong(clientId.Length, "clientId", Constant.MaxClientIdLength, SR.ClientIdTooLong); ParamCheckUtility.ThrowIfNotMatchRegex(ParamCheckUtility.ClientIdValid, clientId, "clientId", SR.InvalidClientId); } catch (ArgumentException) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedClientIdInvalid(this.SessionId, clientId, Utility.GetMessageIdFromMessage(requestMessage)); RequestContextBase requestContextToReject = new DuplexRequestContext(requestMessage, this.binding, channel, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.GenerateFaultMessage(requestMessage, requestMessage.Headers.MessageVersion, SOAFaultCode.Broker_InvalidClientIdOrTooLong, SR.InvalidClientIdOrTooLong), this.ReplySentCallback, requestContextToReject); return; } if (client == null) { if (!this.TryGetClientByChannel(channel, clientId, userName, requestMessage, out client)) { return; } state.Client = client; } // Receive new requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } // Reject if client id does not match if (String.Compare(clientId, client.ClientId, StringComparison.OrdinalIgnoreCase) != 0) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedClientIdNotMatch(this.SessionId, clientId, Utility.GetMessageIdFromMessage(requestMessage)); RequestContextBase requestContextToReject = new DuplexRequestContext(requestMessage, this.binding, channel, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.GenerateFaultMessage(requestMessage, requestMessage.Headers.MessageVersion, SOAFaultCode.Broker_ClientIdNotMatch, SR.ClientIdNotMatch), this.ReplySentCallback, requestContextToReject); return; } // Try to get the user info header bool userDataFlag = GetUserInfoHeader(requestMessage); // Create request context RequestContextBase requestContext; if (userDataFlag) { // Message that needs not to reply requestContext = DummyRequestContext.GetInstance(requestMessage.Version); } else { requestContext = new DuplexRequestContext(requestMessage, this.binding, channel, this.Observer, client); } // Check auth if (!this.CheckAuth(requestContext, requestMessage)) { return; } // remove security header for websocket TryRemoveSecurityHeaderForHttps(requestMessage); // Send the request to the broker client Task.Run(() => client.RequestReceived(requestContext, requestMessage, null)); }
/// <summary> /// AsyncCallback for ReceiveRequest /// </summary> /// <param name="ar">async result</param> private void ReceiveRequest(IAsyncResult ar) { ChannelClientState state = (ChannelClientState)ar.AsyncState; BrokerClient client = state.Client; Message request; try { request = this.azureQueueProxy.EndReceiveRequest(ar); } catch (TimeoutException) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[AzureQueueFrontEnd] Receive Request timed out"); if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } catch (CommunicationException ce) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[AzureQueueFrontEnd] Exception while receiving requests: {0}", ce.Message); // Retry receiving requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } #region Debug Failure Test SimulateFailure.FailOperation(1); #endregion // Try to get the client id and the user name string userName = GetUserName(request); string clientId = GetClientId(request, string.Empty); try { ParamCheckUtility.ThrowIfTooLong(clientId.Length, "clientId", Constant.MaxClientIdLength, SR.ClientIdTooLong); ParamCheckUtility.ThrowIfNotMatchRegex(ParamCheckUtility.ClientIdValid, clientId, "clientId", SR.InvalidClientId); } catch (ArgumentException) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedClientIdInvalid(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request)); return; } if (client == null || client.State == BrokerClientState.Disconnected || !client.ClientId.Equals(clientId, StringComparison.InvariantCultureIgnoreCase)) { try { client = this.ClientManager.GetClient(clientId, userName); client.SingletonInstanceConnected(); } catch (FaultException <SessionFault> e) { if (e.Detail.Code == (int)SOAFaultCode.AccessDenied_BrokerQueue) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedAuthenticationError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), userName); return; } else { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), e.ToString()); throw; } } catch (BrokerQueueException e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), e.ToString()); return; } catch (Exception e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), e.ToString()); throw; } state.Client = client; } // Receive new requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } // Try to get the user info header bool userDataFlag = GetUserInfoHeader(request); // Create request context RequestContextBase requestContext; //Message that needs not to reply requestContext = DummyRequestContext.GetInstance(request.Version); // Bug 15195: Remove security header for https TryRemoveSecurityHeaderForHttps(request); // Send request to the broker client client.RequestReceived(requestContext, request, null); }