Example #1
0
        private IEnumerable <string> Send(string responseReceiverId, object serializedMessage)
        {
            using (EneterTrace.Entering())
            {
                if (AttachedDuplexInputChannel == null)
                {
                    string anErrorMessage = TracedObject + "failed to send the message because the it is not attached to duplex input channel.";
                    EneterTrace.Error(anErrorMessage);
                    throw new InvalidOperationException(anErrorMessage);
                }

                try
                {
                    AttachedDuplexInputChannel.SendResponseMessage(responseReceiverId, serializedMessage);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "failed to send the message. The client will be disconnected and unsubscribed from all messages.", err);

                    try
                    {
                        // Try to disconnect the client.
                        AttachedDuplexInputChannel.DisconnectResponseReceiver(responseReceiverId);
                    }
                    catch
                    {
                    }

                    // Unsubscribe the failed client.
                    return(Unsubscribe(responseReceiverId, null));
                }

                return(new string[0]);
            }
        }
Example #2
0
        protected override void OnRequestMessageReceived(object sender, DuplexChannelMessageEventArgs e)
        {
            using (EneterTrace.Entering())
            {
                // Try to deserialize the message.
                BrokerMessage aBrokerMessage;
                try
                {
                    aBrokerMessage = mySerializer.ForResponseReceiver(e.ResponseReceiverId).Deserialize <BrokerMessage>(e.Message);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "failed to deserialize the message.", err);
                    return;
                }

                if (myValidateBrokerRequestCallback != null)
                {
                    bool isValidated = false;
                    try
                    {
                        isValidated = myValidateBrokerRequestCallback(e.ResponseReceiverId, aBrokerMessage);
                    }
                    catch (Exception err)
                    {
                        EneterTrace.Error(TracedObject + ErrorHandler.DetectedException, err);
                    }

                    if (!isValidated)
                    {
                        IEnumerable <string> anUnsubscribedMessages = Unsubscribe(e.ResponseReceiverId, null);
                        RaiseClientUnsubscribed(e.ResponseReceiverId, anUnsubscribedMessages);

                        try
                        {
                            AttachedDuplexInputChannel.DisconnectResponseReceiver(e.ResponseReceiverId);
                        }
                        catch (Exception err)
                        {
                            EneterTrace.Warning(TracedObject + "failed to disconnect response receiver.", err);
                        }
                        return;
                    }
                }

                if (aBrokerMessage.Request == EBrokerRequest.Publish)
                {
                    if (mySerializer.IsSameForAllResponseReceivers())
                    {
                        // If only one serializer is used for communication with all clients then
                        // increase the performance by reusing already serialized message.
                        Publish(e.ResponseReceiverId, aBrokerMessage, e.Message);
                    }
                    else
                    {
                        // If there is a serializer per client then the message must be serialized
                        // individually for each subscribed client.
                        Publish(e.ResponseReceiverId, aBrokerMessage, null);
                    }
                }
                else if (aBrokerMessage.Request == EBrokerRequest.Subscribe)
                {
                    Subscribe(e.ResponseReceiverId, aBrokerMessage.MessageTypes);
                }
                else if (aBrokerMessage.Request == EBrokerRequest.Unsubscribe)
                {
                    IEnumerable <string> anUnsubscribedMessages = Unsubscribe(e.ResponseReceiverId, aBrokerMessage.MessageTypes);
                    RaiseClientUnsubscribed(e.ResponseReceiverId, anUnsubscribedMessages);
                }
                else if (aBrokerMessage.Request == EBrokerRequest.UnsubscribeAll)
                {
                    IEnumerable <string> anUnsubscribedMessages = Unsubscribe(e.ResponseReceiverId, null);
                    RaiseClientUnsubscribed(e.ResponseReceiverId, anUnsubscribedMessages);
                }
            }
        }