qospacketqueue.cpp
2.07 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
#include "qospacketqueue.h"
#include "cassert"
#include "mqttpacket.h"
QueuedPublish::QueuedPublish(Publish &&publish, uint16_t packet_id) :
publish(std::move(publish)),
packet_id(packet_id)
{
}
uint16_t QueuedPublish::getPacketId() const
{
return this->packet_id;
}
Publish &QueuedPublish::getPublish()
{
return publish;
}
size_t QueuedPublish::getApproximateMemoryFootprint() const
{
return publish.topic.length() + publish.payload.length();
}
bool QoSPublishQueue::erase(const uint16_t packet_id)
{
bool result = false;
auto it = queue.begin();
auto end = queue.end();
while (it != end)
{
QueuedPublish &p = *it;
if (p.getPacketId() == packet_id)
{
size_t mem = p.getApproximateMemoryFootprint();
qosQueueBytes -= mem;
assert(qosQueueBytes >= 0);
if (qosQueueBytes < 0) // Should not happen, but correcting a hypothetical bug is fine for this purpose.
qosQueueBytes = 0;
queue.erase(it);
result = true;
break;
}
it++;
}
return result;
}
std::list<QueuedPublish>::iterator QoSPublishQueue::erase(std::list<QueuedPublish>::iterator pos)
{
return this->queue.erase(pos);
}
size_t QoSPublishQueue::size() const
{
return queue.size();
}
size_t QoSPublishQueue::getByteSize() const
{
return qosQueueBytes;
}
void QoSPublishQueue::queuePublish(PublishCopyFactory ©Factory, uint16_t id, char new_max_qos)
{
assert(new_max_qos > 0);
assert(id > 0);
Publish pub = copyFactory.getNewPublish(new_max_qos);
queue.emplace_back(std::move(pub), id);
qosQueueBytes += queue.back().getApproximateMemoryFootprint();
}
void QoSPublishQueue::queuePublish(Publish &&pub, uint16_t id)
{
assert(id > 0);
queue.emplace_back(std::move(pub), id);
qosQueueBytes += queue.back().getApproximateMemoryFootprint();
}
std::list<QueuedPublish>::iterator QoSPublishQueue::begin()
{
return queue.begin();
}
std::list<QueuedPublish>::iterator QoSPublishQueue::end()
{
return queue.end();
}