/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Collections.Generic; using System.IO; using Microsoft.Extensions.Logging; namespace Opc.Ua.PubSub { /// /// Abstract class for an UA network message /// public abstract class UaNetworkMessage { private ushort m_dataSetWriterId; /// /// The Default event for an error encountered during decoding the dataset messages /// public event EventHandler? DataSetDecodeErrorOccurred; /// /// The DataSetMetaData /// protected DataSetMetaDataType? m_metadata; /// /// List of DataSet messages /// protected List m_uaDataSetMessages; /// /// A logger /// protected ILogger m_logger; /// /// Create instance of . /// /// The configuration object that produced this message. /// The containing data set messages. /// A contextual logger to log to protected UaNetworkMessage( WriterGroupDataType writerGroupConfiguration, List uaDataSetMessages, ILogger? logger = null) { WriterGroupConfiguration = writerGroupConfiguration; m_uaDataSetMessages = uaDataSetMessages; m_metadata = null; m_logger = logger ?? Utils.Fallback.Logger; } /// /// Create instance of . /// protected UaNetworkMessage( WriterGroupDataType writerGroupConfiguration, DataSetMetaDataType metadata, ILogger? logger = null) { WriterGroupConfiguration = writerGroupConfiguration; m_uaDataSetMessages = []; m_metadata = metadata; m_logger = logger ?? Utils.Fallback.Logger; } /// /// Get and Set WriterGroupId /// public ushort WriterGroupId { get; set; } /// /// Get and Set DataSetWriterId if a single value exists for the message. /// public ushort? DataSetWriterId { get { if (m_dataSetWriterId == 0) { if (m_uaDataSetMessages != null && m_uaDataSetMessages.Count == 1) { return m_uaDataSetMessages[0].DataSetWriterId; } return null; } return m_dataSetWriterId != 0 ? m_dataSetWriterId : null; } set => m_dataSetWriterId = value ?? 0; } /// /// DataSet messages /// public List DataSetMessages => m_uaDataSetMessages; /// /// DataSetMetaData messages /// public DataSetMetaDataType? DataSetMetaData => m_metadata; /// /// TRUE if it is a metadata message. /// public bool IsMetaDataMessage => m_metadata != null; /// /// Get the writer group configuration for this network message /// internal WriterGroupDataType WriterGroupConfiguration { get; set; } /// /// Encodes the object and returns the resulting byte array. /// /// The context. public abstract byte[] Encode(IServiceMessageContext messageContext); /// /// Encodes the object in the specified stream. /// /// The context. /// The stream to use. public abstract void Encode(IServiceMessageContext messageContext, Stream stream); /// /// Decodes the message /// public abstract void Decode( IServiceMessageContext messageContext, byte[] message, IList dataSetReaders); /// /// The DataSetDecodeErrorOccurred event handler /// protected virtual void OnDataSetDecodeErrorOccurred(DataSetDecodeErrorEventArgs e) { DataSetDecodeErrorOccurred?.Invoke(this, e); } } }