// SPDX-License-Identifier: MIT /** Copyright (c) 2015 - 2022 Beckhoff Automation GmbH & Co. KG */ #pragma once #include "AdsDef.h" #include "RingBuffer.h" #include namespace Beckhoff { namespace Ads { using VirtualConnection = std::pair; struct Notification { const VirtualConnection connection; Notification(PAdsNotificationFuncEx __func, uint32_t __hUser, uint32_t length, AmsAddr __amsAddr, uint16_t __port) : connection({__port, __amsAddr}), callback(__func), buffer(sizeof(AdsNotificationHeader) + length), hUser(__hUser) { auto header = reinterpret_cast(buffer.data()); header->hNotification = 0; header->cbSampleSize = length; } void Notify(uint64_t timestamp, RingBuffer& ring) { auto header = reinterpret_cast(buffer.data()); uint8_t* data = reinterpret_cast(header + 1); for (size_t i = 0; i < header->cbSampleSize; ++i) { data[i] = ring.ReadFromLittleEndian(); } header->nTimeStamp = timestamp; callback(&connection.second, header, hUser); } uint32_t Size() const { auto header = reinterpret_cast(buffer.data()); return header->cbSampleSize; } void hNotify(uint32_t value) { auto header = reinterpret_cast(buffer.data()); header->hNotification = value; } private: const PAdsNotificationFuncEx callback; std::vector buffer; const uint32_t hUser; }; } }