private void ForwardMessageToClient(string clientResponseReceiverId, string serviceResponseReceiverId, object serializedMessage, object originalMessage) { using (EneterTrace.Entering()) { // Check if the requested client id has a connection with the service session which forwards the message. // Note: this is to prevent that a sevice sends a message to a client which is not connected to it. TClientContext aClientContext; using (ThreadLock.Lock(myConnectionLock)) { aClientContext = myConnectedClients.FirstOrDefault(x => x.ClientResponseReceiverId == clientResponseReceiverId && x.ServiceResponseReceiverId == serviceResponseReceiverId); } if (aClientContext == null) { // The associated client does not exist and the message canno be sent. EneterTrace.Warning(TracedObject + "failed to forward the message to client because the client was not found."); return; } IDuplexInputChannel anInputChannel = myClientConnector.AttachedDuplexInputChannel; if (anInputChannel != null) { // Invoke sending of the message in the client particular thread. // So that e.g. if there are communication problems sending to other clients // is not affected. aClientContext.ForwardToClientThreadDispatcher.Invoke( () => { using (EneterTrace.Entering()) { try { anInputChannel.SendResponseMessage(clientResponseReceiverId, serializedMessage); // If originalMessage is null then service forwards just confirmation the connection was open. if (originalMessage == null && ClientConnected != null) { MessageBusClientEventArgs anEvent = new MessageBusClientEventArgs(aClientContext.ServiceId, aClientContext.ServiceResponseReceiverId, clientResponseReceiverId); try { ClientConnected(this, anEvent); } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err); } } // If original message is not null then the service sends a response message to the client. else if (originalMessage != null && MessageToClientSent != null) { MessageBusMessageEventArgs anEventArgs = new MessageBusMessageEventArgs(aClientContext.ServiceId, serviceResponseReceiverId, clientResponseReceiverId, originalMessage); try { MessageToClientSent(this, anEventArgs); } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err); } } } catch (Exception err) { string anErrorMessage = TracedObject + "failed to send message to the client."; EneterTrace.Error(anErrorMessage, err); UnregisterClient(aClientContext.ClientResponseReceiverId, true, true); } } }); } } }
private void ForwardMessageToService(string clientResponseReceiverId, MessageBusMessage messageFromClient) { using (EneterTrace.Entering()) { TClientContext aClientContext = null; using (ThreadLock.Lock(myConnectionLock)) { aClientContext = myConnectedClients.FirstOrDefault(x => x.ClientResponseReceiverId == clientResponseReceiverId); } if (aClientContext != null) { // Forward the incoming message to the service. IDuplexInputChannel anInputChannel = myServiceConnector.AttachedDuplexInputChannel; if (anInputChannel != null) { aClientContext.ForwardToServiceThreadDispatcher.Invoke( () => { using (EneterTrace.Entering()) { try { // Add the client id into the message. // Note: Because of security reasons we do not expect Ids from the client but using Ids associated with the connection session. // Otherwise it would be possible that some client could use id of another client to pretend a different client. messageFromClient.Id = clientResponseReceiverId; object aSerializedMessage = mySerializer.Serialize <MessageBusMessage>(messageFromClient); anInputChannel.SendResponseMessage(aClientContext.ServiceResponseReceiverId, aSerializedMessage); if (MessageToServiceSent != null) { MessageBusMessageEventArgs anEventArgs = new MessageBusMessageEventArgs(aClientContext.ServiceId, aClientContext.ServiceResponseReceiverId, clientResponseReceiverId, messageFromClient.MessageData); try { MessageToServiceSent(this, anEventArgs); } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err); } } } catch (Exception err) { string anErrorMessage = TracedObject + "failed to send message to the service '" + aClientContext.ServiceId + "'."; EneterTrace.Error(anErrorMessage, err); UnregisterService(aClientContext.ServiceResponseReceiverId); } } }); } } else { string anErrorMessage = TracedObject + "failed to send message to the service because the client was not found."; EneterTrace.Warning(anErrorMessage); } } }