forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qospacketqueue.cpp
210 lines (159 loc) · 5.15 KB
/
qospacketqueue.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#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
{
// TODO: hmm, this is possibly very inaccurate with MQTT5 packets.
return publish.topic.length() + publish.payload.length();
}
void QoSPublishQueue::addToExpirationQueue(std::shared_ptr<QueuedPublish> &qp)
{
Publish &pub = qp->getPublish();
if (!pub.getHasExpireInfo())
return;
this->nextExpireAt = std::min(pub.expiresAt(), this->nextExpireAt);
this->queueExpirations[pub.expiresAt()] = qp->getPacketId();
}
bool QoSPublishQueue::erase(const uint16_t packet_id)
{
bool result = false;
auto pos = this->queue.find(packet_id);
if (pos != this->queue.end())
{
std::shared_ptr<QueuedPublish> &qp = pos->second;
const size_t mem = qp->getApproximateMemoryFootprint();
qosQueueBytes -= mem;
assert(qosQueueBytes >= 0);
if (qosQueueBytes < 0) // Should not happen, but correcting a hypothetical bug is fine for this purpose.
qosQueueBytes = 0;
result = true;
this->eraseFromMapAndRelinkList(pos);
}
return result;
}
/**
* @brief QoSPublishQueue::eraseFromMapAndRelinkList Removes a QueuedPublish from the unordered_map and relink previous andnext together.
* @param pos
*
* This is an internal helper, and therefore doesn't do anything with qosQueueBytes.
*/
void QoSPublishQueue::eraseFromMapAndRelinkList(std::unordered_map<uint16_t, std::shared_ptr<QueuedPublish>>::iterator pos)
{
std::shared_ptr<QueuedPublish> &qp = pos->second;
if (qp->prev)
qp->prev->next = qp->next;
if (this->head == qp)
this->head = qp->prev;
if (qp->next)
qp->next->prev = qp->prev;
if (this->tail == qp)
this->tail = qp->next;
this->queue.erase(pos);
}
const std::shared_ptr<QueuedPublish> &QoSPublishQueue::getTail() const
{
return tail;
}
std::shared_ptr<QueuedPublish> QoSPublishQueue::popNext()
{
std::shared_ptr<QueuedPublish> result = this->tail;
if (result)
erase(result->getPacketId());
return result;
}
size_t QoSPublishQueue::size() const
{
return queue.size();
}
size_t QoSPublishQueue::getByteSize() const
{
return qosQueueBytes;
}
void QoSPublishQueue::addToHeadOfLinkedList(std::shared_ptr<QueuedPublish> &qp)
{
qp->prev = this->head;
if (this->head)
this->head->next = qp;
this->head = qp;
if (!this->tail)
this->tail = qp;
}
/**
* @brief QoSPublishQueue::queuePublish
*
* Note that it may seem a bit weird to queue messages with retain flags, because retained messages can only happen on
* subscribe, which an offline client can't do. However, MQTT5 introduces 'retained as published', so it becomes valid. Bridge
* mode uses this as well.
*/
void QoSPublishQueue::queuePublish(PublishCopyFactory ©Factory, uint16_t id, uint8_t new_max_qos, bool retainAsPublished)
{
assert(new_max_qos > 0);
assert(id > 0);
Publish pub = copyFactory.getNewPublish(new_max_qos, retainAsPublished);
std::shared_ptr<QueuedPublish> qp = std::make_shared<QueuedPublish>(std::move(pub), id);
addToHeadOfLinkedList(qp);
qosQueueBytes += qp->getApproximateMemoryFootprint();
addToExpirationQueue(qp);
queue[id] = std::move(qp);
}
void QoSPublishQueue::queuePublish(Publish &&pub, uint16_t id)
{
assert(id > 0);
std::shared_ptr<QueuedPublish> qp = std::make_shared<QueuedPublish>(std::move(pub), id);
addToHeadOfLinkedList(qp);
qosQueueBytes += qp->getApproximateMemoryFootprint();
addToExpirationQueue(qp);
queue[id] = std::move(qp);
}
int QoSPublishQueue::clearExpiredMessages()
{
if (nextExpireAt > std::chrono::steady_clock::now() || this->queueExpirations.empty())
return 0;
this->nextExpireAt = std::chrono::time_point<std::chrono::steady_clock>::max();
int removed = 0;
auto it = queueExpirations.begin();
auto end = queueExpirations.end();
while (it != end)
{
auto cur_it = it;
it++;
const std::chrono::time_point<std::chrono::steady_clock> &when = cur_it->first;
if (when > std::chrono::steady_clock::now())
{
this->nextExpireAt = when;
break;
}
auto qpos = this->queue.find(cur_it->second);
if (qpos != this->queue.end())
{
std::shared_ptr<QueuedPublish> &p = qpos->second;
if (p->getPublish().hasExpired())
{
this->eraseFromMapAndRelinkList(qpos);
this->queueExpirations.erase(cur_it);
removed++;
}
}
}
return removed;
}