-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathflashmq_plugin.cpp
279 lines (216 loc) · 8.31 KB
/
flashmq_plugin.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
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 "flashmq_plugin.h"
#include "flashmq_plugin_deprecated.h"
#include "logger.h"
#include "threaddata.h"
#include "threadglobals.h"
#include "subscriptionstore.h"
#include "mainapp.h"
#include "utils.h"
void flashmq_logf(int level, const char *str, ...)
{
Logger *logger = Logger::getInstance();
va_list valist;
va_start(valist, str);
logger->logf(level, str, valist);
va_end(valist);
}
/**
* @brief flashmq_plugin_remove_client for previous plugin versions.
*/
void flashmq_plugin_remove_client(const std::string &clientid, bool alsoSession, ServerDisconnectReasons reasonCode)
{
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
std::shared_ptr<Session> session = store->lockSession(clientid);
if (session)
{
std::shared_ptr<Client> client = session->makeSharedClient();
if (client)
{
ReasonCodes _code = static_cast<ReasonCodes>(reasonCode);
std::shared_ptr<ThreadData> td = client->lockThreadData();
if (td)
{
td->serverInitiatedDisconnect(client, _code, "Removed from plugin");
}
}
if (alsoSession)
store->removeSession(session);
}
}
void flashmq_plugin_remove_client_v4(const std::weak_ptr<Session> &session, bool alsoSession, ServerDisconnectReasons reasonCode)
{
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
if (!store) return;
std::shared_ptr<Session> session_locked = session.lock();
if (!session_locked) return;
std::shared_ptr<Client> client = session_locked->makeSharedClient();
if (!client) return;
std::shared_ptr<ThreadData> td = client->lockThreadData();
if (!td) return;
ReasonCodes _code = static_cast<ReasonCodes>(reasonCode);
td->serverInitiatedDisconnect(client, _code, "Removed from plugin");
if (alsoSession)
store->removeSession(session_locked);
}
/**
* @brief flashmq_plugin_remove_subscription for previous plugin versions.
*/
void flashmq_plugin_remove_subscription(const std::string &clientid, const std::string &topicFilter)
{
if (!(isValidUtf8(topicFilter) && isValidSubscribePath(topicFilter)))
throw std::runtime_error("Unsubscribing from plugin failed: invalid topic filter: " + topicFilter);
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
std::shared_ptr<Session> session = store->lockSession(clientid);
if (!session) return;
std::vector<std::string> subtopics = splitTopic(topicFilter);
std::string shareName;
std::string _;
parseSubscriptionShare(subtopics, shareName, _);
store->removeSubscription(session, subtopics, shareName);
}
void flashmq_plugin_remove_subscription_v4(const std::weak_ptr<Session> &session, const std::string &topicFilter)
{
if (!(isValidUtf8(topicFilter) && isValidSubscribePath(topicFilter)))
throw std::runtime_error("Unsubscribing from plugin failed: invalid topic filter: " + topicFilter);
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
std::shared_ptr<Session> session_locked = session.lock();
if (!session_locked) return;
std::vector<std::string> subtopics = splitTopic(topicFilter);
std::string shareName;
std::string _;
parseSubscriptionShare(subtopics, shareName, _);
store->removeSubscription(session_locked, subtopics, shareName);
}
bool flashmq_plugin_add_subscription(
const std::weak_ptr<Session> &session, const std::string &topicFilter, uint8_t qos, bool noLocal, bool retainAsPublished,
const uint32_t subscriptionIdentifier)
{
if (!(isValidUtf8(topicFilter) && isValidSubscribePath(topicFilter)))
throw std::runtime_error("Subscribing from plugin failed: invalid topic filter: " + topicFilter);
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
if (!store) return false;
std::shared_ptr<Session> session_locked = session.lock();
if (!session_locked) return false;
std::vector<std::string> subtopics = splitTopic(topicFilter);
std::string shareName;
std::string topicDummy;
parseSubscriptionShare(subtopics, shareName, topicDummy);
const AddSubscriptionType result = store->addSubscription(session_locked, subtopics, qos, noLocal, retainAsPublished, shareName, subscriptionIdentifier);
return result == AddSubscriptionType::Invalid ? false : true;
}
void flashmq_continue_async_authentication(const std::weak_ptr<Client> &client, AuthResult result, const std::string &authMethod, const std::string &returnData)
{
std::shared_ptr<Client> c = client.lock();
if (!c)
return;
std::shared_ptr<ThreadData> td = c->lockThreadData();
if (!td)
return;
td->queueContinuationOfAuthentication(c, result, authMethod, returnData);
}
void flashmq_publish_message(const std::string &topic, const uint8_t qos, const bool retain, const std::string &payload, uint32_t expiryInterval,
const std::vector<std::pair<std::string, std::string>> *userProperties,
const std::string *responseTopic, const std::string *correlationData, const std::string *contentType)
{
Publish pub(topic, payload, qos);
pub.retain = retain;
if (userProperties)
{
for (const std::pair<std::string, std::string> &pair : *userProperties)
{
pub.addUserProperty(pair.first, pair.second);
}
}
if (expiryInterval)
{
pub.setExpireAfter(expiryInterval);
}
if (responseTopic)
{
pub.responseTopic = *responseTopic;
}
if (correlationData)
{
pub.correlationData = *correlationData;
}
if (contentType)
{
pub.contentType = *contentType;
}
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
if (pub.retain)
{
store->setRetainedMessage(pub, pub.getSubtopics());
}
PublishCopyFactory factory(&pub);
store->queuePacketAtSubscribers(factory, "");
}
void flashmq_get_client_address(const std::weak_ptr<Client> &client, std::string *text, FlashMQSockAddr *addr)
{
std::shared_ptr<Client> c = client.lock();
if (!c)
return;
const struct sockaddr *orgAddr = c->getAddr();
if (text)
*text = sockaddrToString(orgAddr);
if (addr)
memcpy(addr->getAddr(), orgAddr, addr->getLen());
}
void flashmq_poll_add_fd(int fd, uint32_t events, const std::weak_ptr<void> &p)
{
ThreadData *d = ThreadGlobals::getThreadData().get();
if (!d)
return;
d->pollExternalFd(fd, events, p);
}
void flashmq_poll_remove_fd(uint32_t fd)
{
ThreadData *d = ThreadGlobals::getThreadData().get();
if (!d)
return;
d->pollExternalRemove(fd);
}
sockaddr *FlashMQSockAddr::getAddr()
{
return reinterpret_cast<struct sockaddr*>(&this->addr_in6);
}
constexpr int FlashMQSockAddr::getLen()
{
return sizeof(struct sockaddr_in6);
}
uint32_t flashmq_add_task(std::function<void ()> f, uint32_t delay_in_ms)
{
std::shared_ptr<ThreadData> d = ThreadGlobals::getThreadData();
if (!d)
throw std::runtime_error("No thread data?");
return d->addDelayedTask(f, delay_in_ms);
}
void flashmq_remove_task(uint32_t id)
{
std::shared_ptr<ThreadData> d = ThreadGlobals::getThreadData();
if (!d)
return;
d->removeDelayedTask(id);
}
void flashmq_get_session_pointer(const std::string &clientid, const std::string &username, std::weak_ptr<Session> &sessionOut)
{
std::shared_ptr<SubscriptionStore> store = MainApp::getMainApp()->getSubscriptionStore();
if (!store) return;
std::shared_ptr<Session> session = store->lockSession(clientid);
if (!session) return;
if (session->getUsername() != username) return;
sessionOut = session;
}
void flashmq_get_client_pointer(const std::weak_ptr<Session> &session, std::weak_ptr<Client> &clientOut)
{
std::shared_ptr<Session> sessionLocked = session.lock();
if (!sessionLocked) return;
clientOut = sessionLocked->makeSharedClient();
}