Ejemplo n.º 1
0
        /// <summary>
        /// Process the bytes received from UADP channel
        /// </summary>
        /// <param name="message"></param>
        /// <param name="source"></param>
        private void ProcessReceivedMessage(byte[] message, IPEndPoint source)
        {
            Utils.Trace(Utils.TraceMasks.Information, "UadpPubSubConnection.ProcessReceivedMessage from source={0}", source);
            ServiceMessageContext messageContext = new ServiceMessageContext();

            using (BinaryDecoder decoder = new BinaryDecoder(message, messageContext))
            {
                UadpNetworkMessage uadpNetworkMessage = new UadpNetworkMessage();
                //decode bytes using dataset reader information
                var subscribedDataSets = uadpNetworkMessage.DecodeSubscribedDataSets(decoder, DataSetReaders);
                if (subscribedDataSets != null && subscribedDataSets.Count > 0)
                {
                    //trigger notification for received subscribed data set
                    Application.RaiseDataReceivedEvent(
                        new SubscribedDataEventArgs()
                    {
                        NetworkMessageSequenceNumber = uadpNetworkMessage.SequenceNumber,
                        DataSets       = subscribedDataSets,
                        SourceEndPoint = source
                    }
                        );
                    Utils.Trace(Utils.TraceMasks.Information,
                                "UadpPubSubConnection.RaiseDataReceivedEvent from source={0}, with {1} DataSets", source, subscribedDataSets.Count);
                }
                else
                {
                    Utils.Trace(Utils.TraceMasks.Information,
                                "Message from source={0} cannot be decoded.", source);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create the network message built from the provided writerGroupConfiguration
        /// </summary>
        /// <param name="writerGroupConfiguration"></param>
        /// <returns></returns>
        public override UaNetworkMessage CreateNetworkMessage(WriterGroupDataType writerGroupConfiguration)
        {
            UadpWriterGroupMessageDataType messageSettings = ExtensionObject.ToEncodeable(writerGroupConfiguration.MessageSettings)
                                                             as UadpWriterGroupMessageDataType;

            if (messageSettings == null)
            {
                //Wrong configuration of writer group MessageSettings
                return(null);
            }

            //Create list of dataSet messages to be sent
            List <UadpDataSetMessage> dataSetMessages = new List <UadpDataSetMessage>();

            foreach (DataSetWriterDataType dataSetWriter in writerGroupConfiguration.DataSetWriters)
            {
                //check if dataSetWriter enabled
                if (dataSetWriter.Enabled)
                {
                    DataSet dataSet = Application.DataCollector.CollectData(dataSetWriter.DataSetName);
                    if (dataSet != null)
                    {
                        UadpDataSetWriterMessageDataType dataSetMessageSettings =
                            ExtensionObject.ToEncodeable(dataSetWriter.MessageSettings) as
                            UadpDataSetWriterMessageDataType;
                        // check MessageSettings to see how to encode DataSet
                        if (dataSetMessageSettings != null)
                        {
                            UadpDataSetMessage uadpDataSetMessage = new UadpDataSetMessage(dataSet);
                            uadpDataSetMessage.DataSetWriterId = dataSetWriter.DataSetWriterId;
                            uadpDataSetMessage.SetMessageContentMask((UadpDataSetMessageContentMask)dataSetMessageSettings.DataSetMessageContentMask);
                            uadpDataSetMessage.SetFieldContentMask((DataSetFieldContentMask)dataSetWriter.DataSetFieldContentMask);
                            uadpDataSetMessage.SequenceNumber = (ushort)(Utils.IncrementIdentifier(ref m_dataSetSequenceNumber) % UInt16.MaxValue);
                            uadpDataSetMessage.ConfiguredSize = dataSetMessageSettings.ConfiguredSize;
                            uadpDataSetMessage.DataSetOffset  = dataSetMessageSettings.DataSetOffset;
                            uadpDataSetMessage.TimeStamp      = DateTime.UtcNow;
                            uadpDataSetMessage.Status         = (ushort)StatusCodes.Good;
                            dataSetMessages.Add(uadpDataSetMessage);
                        }
                    }
                }
            }

            //cancel send if no dataset message
            if (dataSetMessages.Count == 0)
            {
                return(null);
            }

            UadpNetworkMessage uadpNetworkMessage = new UadpNetworkMessage(dataSetMessages);

            uadpNetworkMessage.SetNetworkMessageContentMask((UadpNetworkMessageContentMask)messageSettings.NetworkMessageContentMask);
            uadpNetworkMessage.WriterGroupId = writerGroupConfiguration.WriterGroupId;
            // Network message header
            uadpNetworkMessage.PublisherId    = PubSubConnectionConfiguration.PublisherId.Value;
            uadpNetworkMessage.SequenceNumber = (ushort)(Utils.IncrementIdentifier(ref m_sequenceNumber) % UInt16.MaxValue);

            // Writer group header
            uadpNetworkMessage.GroupVersion         = messageSettings.GroupVersion;
            uadpNetworkMessage.NetworkMessageNumber = 1; //only one network message per publish

            return(uadpNetworkMessage);
        }