private NotificationMessage ConstructMessage(
Queue<EventFieldList> events,
Queue<MonitoredItemNotification> datachanges,
Queue<DiagnosticInfo> datachangeDiagnostics,
out int notificationCount)
{
notificationCount = 0;
NotificationMessage message = new NotificationMessage();
message.SequenceNumber = (uint)m_sequenceNumber;
message.PublishTime = DateTime.UtcNow;
Utils.IncrementIdentifier(ref m_sequenceNumber);
lock (m_diagnostics)
{
m_diagnostics.NextSequenceNumber = (uint)m_sequenceNumber;
}
// add events.
if (events.Count > 0 && notificationCount < m_maxNotificationsPerPublish)
{
EventNotificationList notification = new EventNotificationList();
while (events.Count > 0 && notificationCount < m_maxNotificationsPerPublish)
{
notification.Events.Add(events.Dequeue());
notificationCount++;
}
message.NotificationData.Add(new ExtensionObject(notification));
}
// add datachanges (space permitting).
if (datachanges.Count > 0 && notificationCount < m_maxNotificationsPerPublish)
{
bool diagnosticsExist = false;
DataChangeNotification notification = new DataChangeNotification();
notification.MonitoredItems = new MonitoredItemNotificationCollection(datachanges.Count);
notification.DiagnosticInfos = new DiagnosticInfoCollection(datachanges.Count);
while (datachanges.Count > 0 && notificationCount < m_maxNotificationsPerPublish)
{
MonitoredItemNotification datachange = datachanges.Dequeue();
notification.MonitoredItems.Add(datachange);
DiagnosticInfo diagnosticInfo = datachangeDiagnostics.Dequeue();
if (diagnosticInfo != null)
{
diagnosticsExist = true;
}
notification.DiagnosticInfos.Add(diagnosticInfo);
notificationCount++;
}
// clear diagnostics if not used.
if (!diagnosticsExist)
{
notification.DiagnosticInfos.Clear();
}
message.NotificationData.Add(new ExtensionObject(notification));
}
return message;
}