qospacketqueue.cpp
1.87 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
#include "qospacketqueue.h"
#include "cassert"
#include "mqttpacket.h"
void QoSPacketQueue::erase(const uint16_t packet_id)
{
auto it = queue.begin();
auto end = queue.end();
while (it != end)
{
std::shared_ptr<MqttPacket> &p = *it;
if (p->getPacketId() == packet_id)
{
size_t mem = p->getTotalMemoryFootprint();
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);
break;
}
it++;
}
}
size_t QoSPacketQueue::size() const
{
return queue.size();
}
size_t QoSPacketQueue::getByteSize() const
{
return qosQueueBytes;
}
/**
* @brief QoSPacketQueue::queuePacket makes a copy of the packet because it has state for the receiver in question.
* @param p
* @param id
* @return the packet copy.
*/
std::shared_ptr<MqttPacket> QoSPacketQueue::queuePacket(const MqttPacket &p, uint16_t id, char new_max_qos)
{
assert(p.getQos() > 0);
std::shared_ptr<MqttPacket> copyPacket = p.getCopy(new_max_qos);
copyPacket->setPacketId(id);
queue.push_back(copyPacket);
qosQueueBytes += copyPacket->getTotalMemoryFootprint();
return copyPacket;
}
std::shared_ptr<MqttPacket> QoSPacketQueue::queuePacket(const Publish &pub, uint16_t id)
{
assert(pub.qos > 0);
std::shared_ptr<MqttPacket> copyPacket(new MqttPacket(pub));
copyPacket->setPacketId(id);
queue.push_back(copyPacket);
qosQueueBytes += copyPacket->getTotalMemoryFootprint();
return copyPacket;
}
std::list<std::shared_ptr<MqttPacket>>::const_iterator QoSPacketQueue::begin() const
{
return queue.cbegin();
}
std::list<std::shared_ptr<MqttPacket>>::const_iterator QoSPacketQueue::end() const
{
return queue.cend();
}