Exemplo n.º 1
0
 /// <summary>
 /// Adds the data to already serialized data.
 /// </summary>
 /// <remarks>
 /// It creates the <see cref="WrappedData"/> from the given data and serializes it with the provided serializer.<br/>
 /// </remarks>
 /// <param name="addedData">Added data. It must a basic .Net type. Otherwise the serialization will fail.</param>
 /// <param name="originalData">Already serialized data - it is type of string or byte[].</param>
 /// <param name="serializer">serializer</param>
 public static object Wrap(object addedData, object originalData, ISerializer serializer)
 {
     using (EneterTrace.Entering())
     {
         WrappedData aWrappedData = new WrappedData(addedData, originalData);
         return(serializer.Serialize <WrappedData>(aWrappedData));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// The method is called when a reponse message is received from the duplex output channel.
        /// The received response is unwrapped and sent as a response to the matching duplex input channel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnResponseMessageReceived(object sender, DuplexChannelMessageEventArgs e)
        {
            using (EneterTrace.Entering())
            {
                try
                {
                    ISerializer aSerializer  = mySerializer.ForResponseReceiver(e.ResponseReceiverId);
                    WrappedData aWrappedData = DataWrapper.Unwrap(e.Message, aSerializer);

                    // WrappedData.AddedData represents the channel id.
                    // Therefore if everything is ok then it must be string.
                    if (aWrappedData.AddedData is string)
                    {
                        // Get the output channel according to the channel id.
                        TDuplexInputChannel aDuplexInputChannel = null;

                        using (ThreadLock.Lock(myDuplexInputChannels))
                        {
                            myDuplexInputChannels.TryGetValue((string)aWrappedData.AddedData, out aDuplexInputChannel);
                        }

                        if (aDuplexInputChannel != null)
                        {
                            aDuplexInputChannel.DuplexInputChannel.SendResponseMessage(aDuplexInputChannel.ResponseReceiverId, aWrappedData.OriginalData);
                        }
                        else
                        {
                            EneterTrace.Warning(TracedObject + "could not send the response message to the duplex input channel '" + (string)aWrappedData.AddedData + "' because the channel is not attached to the unwrapper.");
                        }
                    }
                    else
                    {
                        EneterTrace.Error(TracedObject + "detected that the unwrapped message does not contain the channel id as the string type.");
                    }
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "failed to process the response message.", err);
                }
            }
        }
        protected override void OnRequestMessageReceived(object sender, DuplexChannelMessageEventArgs e)
        {
            using (EneterTrace.Entering())
            {
                WrappedData aWrappedData = null;

                try
                {
                    ISerializer aSerializer = mySerializer.ForResponseReceiver(e.ResponseReceiverId);

                    // Unwrap the incoming message.
                    aWrappedData = DataWrapper.Unwrap(e.Message, aSerializer);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "failed to unwrap the message.", err);
                    return;
                }

                // WrappedData.AddedData represents the channel id.
                // Therefore if everything is ok then it must be string.
                if (aWrappedData.AddedData is string)
                {
                    string aMessageReceiverId = (string)aWrappedData.AddedData;

                    TDuplexConnection aConectionToOutput = null;

                    // Try to find if the output channel with the required channel id and for the incoming response receiver
                    // already exists.
                    using (ThreadLock.Lock(myConnections))
                    {
                        aConectionToOutput = myConnections.FirstOrDefault(x => x.DuplexOutputChannel.ChannelId == aMessageReceiverId && x.ResponseReceiverId == e.ResponseReceiverId);

                        // If it does not exist then create the duplex output channel and open connection.
                        if (aConectionToOutput == null)
                        {
                            IDuplexOutputChannel aDuplexOutputChannel = null;

                            try
                            {
                                aDuplexOutputChannel = myOutputMessagingFactory.CreateDuplexOutputChannel(aMessageReceiverId);
                                aDuplexOutputChannel.ResponseMessageReceived += OnResponseMessageReceived;
                                aDuplexOutputChannel.OpenConnection();
                            }
                            catch (Exception err)
                            {
                                EneterTrace.Error(TracedObject + "failed to create and connect the duplex output channel '" + aMessageReceiverId + "'", err);

                                if (aDuplexOutputChannel != null)
                                {
                                    aDuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived;
                                    aDuplexOutputChannel.CloseConnection();
                                    aDuplexOutputChannel = null;
                                }
                            }

                            if (aDuplexOutputChannel != null)
                            {
                                aConectionToOutput = new TDuplexConnection(e.ResponseReceiverId, aDuplexOutputChannel);
                                myConnections.Add(aConectionToOutput);
                            }
                        }
                    }

                    if (aConectionToOutput != null)
                    {
                        try
                        {
                            // Send the unwrapped message.
                            aConectionToOutput.DuplexOutputChannel.SendMessage(aWrappedData.OriginalData);
                        }
                        catch (Exception err)
                        {
                            EneterTrace.Error(TracedObject + "failed to send the message to the output channel '" + aConectionToOutput.DuplexOutputChannel.ChannelId + "'.", err);
                        }
                    }
                }
                else
                {
                    EneterTrace.Error(TracedObject + "detected that the unwrapped message contian the channel id as the string type.");
                }
            }
        }