forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishcopyfactory.cpp
184 lines (148 loc) · 5.04 KB
/
publishcopyfactory.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
/*
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 <cassert>
#include <stdexcept>
#include "publishcopyfactory.h"
#include "mqttpacket.h"
PublishCopyFactory::PublishCopyFactory(MqttPacket *packet) :
packet(packet),
orgQos(packet->getQos()),
orgRetain(packet->getRetain())
{
}
PublishCopyFactory::PublishCopyFactory(Publish *publish) :
publish(publish),
orgQos(publish->qos),
orgRetain(publish->retain)
{
}
MqttPacket *PublishCopyFactory::getOptimumPacket(const uint8_t max_qos, const ProtocolVersion protocolVersion, uint16_t topic_alias, bool skip_topic)
{
const uint8_t actualQos = getEffectiveQos(max_qos);
if (packet)
{
if (protocolVersion >= ProtocolVersion::Mqtt5 && (packet->containsClientSpecificProperties() || topic_alias > 0))
{
Publish newPublish(packet->getPublishData());
newPublish.qos = actualQos;
newPublish.topicAlias = topic_alias;
newPublish.skipTopic = skip_topic;
this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, newPublish);
return this->oneShotPacket.get();
}
if (packet->getProtocolVersion() == protocolVersion && static_cast<bool>(orgQos) == static_cast<bool>(actualQos) && !packet->isAlteredByPlugin())
{
return packet;
}
const int cache_key = (static_cast<uint8_t>(protocolVersion) * 10) + actualQos;
std::unique_ptr<MqttPacket> &cachedPack = constructedPacketCache[cache_key];
if (!cachedPack)
{
Publish newPublish(packet->getPublishData());
newPublish.qos = actualQos;
cachedPack = std::make_unique<MqttPacket>(protocolVersion, newPublish);
}
return cachedPack.get();
}
// Getting an instance of a Publish object happens at least on retained messages, will messages and SYS topics. It's low traffic, anyway.
assert(publish);
publish->qos = actualQos;
publish->topicAlias = topic_alias;
publish->skipTopic = skip_topic;
this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, *publish);
return this->oneShotPacket.get();
}
uint8_t PublishCopyFactory::getEffectiveQos(uint8_t max_qos) const
{
const uint8_t effectiveQos = std::min<uint8_t>(orgQos, max_qos);
return effectiveQos;
}
bool PublishCopyFactory::getEffectiveRetain(bool retainAsPublished) const
{
return orgRetain && retainAsPublished;
}
const std::string &PublishCopyFactory::getTopic() const
{
if (packet)
return packet->getTopic();
assert(publish);
return publish->topic;
}
const std::vector<std::string> &PublishCopyFactory::getSubtopics()
{
if (packet)
{
return packet->getSubtopics();
}
else if (publish)
{
return publish->getSubtopics();
}
throw std::runtime_error("Bug in &PublishCopyFactory::getSubtopics()");
}
std::string_view PublishCopyFactory::getPayload() const
{
if (packet)
return packet->getPayloadView();
assert(publish);
return publish->payload;
}
bool PublishCopyFactory::getRetain() const
{
// Keeping this here as reminder that it should not be implemented.
assert(false);
return false;
}
/**
* @brief PublishCopyFactory::getNewPublish gets a new publish object from an existing packet or publish.
* @param new_max_qos
* @return
*
* It being a public function, the idea is that it's only needed for creating publish objects for storing QoS messages for off-line
* clients. For on-line clients, you're always making a packet (with getOptimumPacket()).
*/
Publish PublishCopyFactory::getNewPublish(uint8_t new_max_qos, bool retainAsPublished) const
{
// (At time of writing) we only need to construct new publishes for QoS (because we're storing QoS publishes for offline clients). If
// you're doing it elsewhere, it's a bug.
assert(orgQos > 0);
assert(new_max_qos > 0);
const uint8_t actualQos = getEffectiveQos(new_max_qos);
if (packet)
{
assert(packet->getQos() > 0);
Publish p(packet->getPublishData());
p.qos = actualQos;
p.retain = getEffectiveRetain(retainAsPublished);
return p;
}
assert(publish->qos > 0); // Same check as above, but then for Publish objects.
Publish p(*publish);
p.qos = actualQos;
p.retain = getEffectiveRetain(retainAsPublished);
return p;
}
std::shared_ptr<Client> PublishCopyFactory::getSender()
{
if (packet)
return packet->getSender();
return std::shared_ptr<Client>(0);
}
const std::vector<std::pair<std::string, std::string> > *PublishCopyFactory::getUserProperties() const
{
if (packet)
{
return packet->getUserProperties();
}
assert(publish);
return publish->getUserProperties();
}
void PublishCopyFactory::setSharedSubscriptionHashKey(size_t hash)
{
this->sharedSubscriptionHashKey = hash;
}