-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessingBuffer.cpp
More file actions
196 lines (156 loc) · 6.5 KB
/
ProcessingBuffer.cpp
File metadata and controls
196 lines (156 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
The Original Code is the Parlo Library.
The Initial Developer of the Original Code is
Mats 'Afr0' Vederhus. All Rights Reserved.
Contributor(s): ______________________________________.
*/
#include "pch.h"
#include "Parlo.h"
#include <memory>
namespace Parlo
{
/*The processingbuffer processes incoming data and turns them into packets.*/
class ProcessingBuffer::Impl {
public:
Impl();
~Impl();
using PacketProcessedCallback = std::function<void(const Packet&)>;
void setOnPacketProcessedHandler(PacketProcessedCallback callback);
void addData(const std::vector<uint8_t>& data);
uint8_t operator[](size_t index) const;
size_t bufferCount() const;
private:
std::queue<uint8_t> internalBuffer;
mutable std::mutex mutex;
std::condition_variable cv;
std::thread processingThread;
std::atomic<bool> stopProcessing{ false };
bool hasReadHeader = false;
uint8_t currentID = 0;
bool isCompressed = false;
uint16_t currentLength = 0;
PacketProcessedCallback onPacketProcessedHandler;
void processPackets();
void readHeader();
friend class ProcessingBuffer;
};
ProcessingBuffer::Impl::Impl() : stopProcessing(false) {
processingThread = std::thread(&Impl::processPackets, this);
}
ProcessingBuffer::Impl::~Impl() {
stopProcessing.store(true, std::memory_order_relaxed);
cv.notify_all();
if (processingThread.joinable())
processingThread.join();
}
/*Sets a handler for the OnPacketProcessed event.
@param PacketProcessedCallback A callback function with the signature: void(const Packet&)*/
void ProcessingBuffer::Impl::setOnPacketProcessedHandler(PacketProcessedCallback callback) {
onPacketProcessedHandler = callback;
}
/*
* Shovels shit (data) into the buffer.
* @param The data to add. Needs to be no bigger than MAX_PACKET_SIZE!
* @exception Throws std::overflow_error if data was larger than MAX_PACKET_SIZE.
*/
void ProcessingBuffer::Impl::addData(const std::vector<uint8_t>& data) {
if (data.size() > MAX_PACKET_SIZE)
throw std::overflow_error("ProcessingBuffer::addData(): Buffer overflow exception!");
{
std::lock_guard<std::mutex> lock(mutex);
for (auto byte : data)
internalBuffer.push(byte);
}
cv.notify_one();
}
uint8_t ProcessingBuffer::Impl::operator[](size_t index) const {
std::lock_guard<std::mutex> lock(mutex);
if (index >= internalBuffer.size())
throw std::out_of_range("ProcessingBuffer: Index out of range!");
auto tempQueue = internalBuffer;
for (size_t i = 0; i < index; ++i)
tempQueue.pop();
return tempQueue.front();
}
/*The length of the internal buffer.*/
size_t ProcessingBuffer::Impl::bufferCount() const {
return internalBuffer.size();
}
/*Processes packets.*/
void ProcessingBuffer::Impl::processPackets() {
while (!stopProcessing)
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this] { return !internalBuffer.empty() || stopProcessing; });
if (stopProcessing) break;
if (internalBuffer.size() >= static_cast<size_t>(PacketHeaders::STANDARD)) {
if (!hasReadHeader)
readHeader();
}
if (hasReadHeader) {
if (internalBuffer.size() >= static_cast<size_t>(currentLength - PacketHeaders::STANDARD)) {
std::vector<uint8_t> packetData(currentLength - PacketHeaders::STANDARD);
for (uint16_t i = 0; i < packetData.size(); ++i) {
packetData[i] = internalBuffer.front();
internalBuffer.pop();
}
hasReadHeader = false;
Packet packet(currentID, packetData, isCompressed);
if (onPacketProcessedHandler)
onPacketProcessedHandler(packet);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(10)); //To prevent hogging the processor
}
}
/*Reads a packet's header.*/
void ProcessingBuffer::Impl::readHeader() {
if (internalBuffer.size() < 4)
return; //Not enough data to read the header
currentID = internalBuffer.front();
internalBuffer.pop();
isCompressed = internalBuffer.front();
internalBuffer.pop();
//Read two bytes for the length and combine them
uint8_t lengthLow = internalBuffer.front();
internalBuffer.pop();
uint8_t lengthHigh = internalBuffer.front();
internalBuffer.pop();
currentLength = static_cast<uint16_t>(lengthHigh << 8 | lengthLow);
std::cout << "lengthLow: " << static_cast<int>(lengthLow) << ", lengthHigh: " << static_cast<int>(lengthHigh) << ", currentLength: " << currentLength << std::endl;
hasReadHeader = true;
}
ProcessingBuffer::ProcessingBuffer()
: pImpl(std::make_unique<Impl>())
{}
ProcessingBuffer::~ProcessingBuffer() {
if (pImpl) {
pImpl->stopProcessing.store(true, std::memory_order_relaxed);
pImpl->cv.notify_all();
if (pImpl->processingThread.joinable())
pImpl->processingThread.join();
}
}
/*Sets a handler for the OnPacketProcessed event.
@param PacketProcessedCallback A callback function with the signature: void(const Packet&)*/
void ProcessingBuffer::setOnPacketProcessedHandler(PacketProcessedCallback callback) {
pImpl->setOnPacketProcessedHandler(callback);
}
/*
* Shovels shit (data) into the buffer.
* @param The data to add. Needs to be no bigger than MAX_PACKET_SIZE!
* @exception Throws std::overflow_error if data was larger than MAX_PACKET_SIZE.
*/
void ProcessingBuffer::addData(const std::vector<uint8_t>& data) {
pImpl->addData(data);
}
uint8_t ProcessingBuffer::operator[](size_t index) const {
return pImpl->operator[](index);
}
/*The length of the internal buffer.*/
size_t ProcessingBuffer::bufferCount() const {
return pImpl->bufferCount();
}
}