Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions AdsLib/AdsNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "AdsDef.h"
#include "Log.h"
#include "RingBuffer.h"

#include <vector>
Expand All @@ -21,30 +22,36 @@ struct Notification {
, callback(__func)
, buffer(sizeof(AdsNotificationHeader) + length)
, hUser(__hUser)
, bufferLength(length)
{
auto header = reinterpret_cast<AdsNotificationHeader *>(
buffer.data());
header->hNotification = 0;
header->cbSampleSize = length;
}

void Notify(uint64_t timestamp, RingBuffer &ring)
void Notify(uint64_t timestamp, RingBuffer &ring, uint32_t size)
{
if (size > bufferLength)
{
LOG_ERROR("Notification size exceeds buffer. "
<< size << " > " << bufferLength);
return;
}
auto header = reinterpret_cast<AdsNotificationHeader *>(
buffer.data());
uint8_t *data = reinterpret_cast<uint8_t *>(header + 1);
for (size_t i = 0; i < header->cbSampleSize; ++i) {
for (size_t i = 0; i < size; ++i) {
data[i] = ring.ReadFromLittleEndian<uint8_t>();
}
header->nTimeStamp = timestamp;
header->cbSampleSize = size;
callback(&connection.second, header, hUser);
}

uint32_t Size() const
uint32_t Capacity() const
{
auto header = reinterpret_cast<const AdsNotificationHeader *>(
buffer.data());
return header->cbSampleSize;
return bufferLength;
}

void hNotify(uint32_t value)
Expand All @@ -58,4 +65,5 @@ struct Notification {
const PAdsNotificationFuncEx callback;
std::vector<uint8_t> buffer;
const uint32_t hUser;
const uint32_t bufferLength;
};
8 changes: 4 additions & 4 deletions AdsLib/standalone/NotificationDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ void NotificationDispatcher::Run()
fullLength -= sizeof(hNotify) + sizeof(size);
const auto notification = Find(hNotify);
if (notification) {
if (size != notification->Size()) {
if (size > notification->Capacity()) {
LOG_WARN(
"Notification sample size: "
<< size
<< " doesn't match: "
<< notification->Size());
<< " exceeds buffer: "
<< notification->Capacity());
goto cleanup;
}
notification->Notify(timestamp, ring);
notification->Notify(timestamp, ring, size);
} else {
ring.Read(size);
}
Expand Down