-
Notifications
You must be signed in to change notification settings - Fork 23
/
flashmq_plugin.cpp
198 lines (149 loc) · 4.75 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
/*
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 "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);
}
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_subscription(const std::string &clientid, const std::string &topicFilter)
{
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)
{
store->removeSubscription(client, topicFilter);
}
}
}
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();
if (!d)
return;
d->pollExternalFd(fd, events, p);
}
void flashmq_poll_remove_fd(uint32_t fd)
{
ThreadData *d = ThreadGlobals::getThreadData();
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)
{
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)
{
ThreadData *d = ThreadGlobals::getThreadData();
if (!d)
return;
d->removeDelayedTask(id);
}