-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtls_ws_both.cpp
394 lines (361 loc) · 14.7 KB
/
tls_ws_both.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright Takatoshi Kondo 2017
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// no_tls client and server
#include <iostream>
#include <iomanip>
#include <map>
#include <mqtt_client_cpp.hpp>
#include <boost/lexical_cast.hpp>
#include "locked_cout.hpp"
template <typename Client, typename Disconnect>
void client_proc(
Client& c,
std::uint16_t& pid_sub1,
std::uint16_t& pid_sub2,
Disconnect const& disconnect) {
using packet_id_t = typename std::remove_reference_t<decltype(*c)>::packet_id_t;
// Setup client
c->set_client_id("cid1");
c->set_clean_session(true);
// Setup handlers
c->set_connack_handler(
[&c, &pid_sub1, &pid_sub2]
(bool sp, MQTT_NS::connect_return_code connack_return_code){
locked_cout() << "[client] Connack handler called" << std::endl;
locked_cout() << "[client] Session Present: " << std::boolalpha << sp << std::endl;
locked_cout() << "[client] Connack Return Code: "
<< MQTT_NS::connect_return_code_to_str(connack_return_code) << std::endl;
if (connack_return_code == MQTT_NS::connect_return_code::accepted) {
pid_sub1 = c->subscribe("mqtt_client_cpp/topic1", MQTT_NS::qos::at_most_once);
pid_sub2 = c->subscribe(
std::vector<std::tuple<MQTT_NS::string_view, MQTT_NS::subscribe_options>>
{
{ "mqtt_client_cpp/topic2_1", MQTT_NS::qos::at_least_once },
{ "mqtt_client_cpp/topic2_2", MQTT_NS::qos::exactly_once }
}
);
}
return true;
});
c->set_close_handler(
[]
(){
locked_cout() << "[client] closed." << std::endl;
});
c->set_error_handler(
[]
(MQTT_NS::error_code ec){
locked_cout() << "[client] error: " << ec.message() << std::endl;
});
c->set_puback_handler(
[&]
(packet_id_t packet_id){
locked_cout() << "[client] puback received. packet_id: " << packet_id << std::endl;
disconnect();
return true;
});
c->set_pubrec_handler(
[&]
(packet_id_t packet_id){
locked_cout() << "[client] pubrec received. packet_id: " << packet_id << std::endl;
return true;
});
c->set_pubcomp_handler(
[&]
(packet_id_t packet_id){
locked_cout() << "[client] pubcomp received. packet_id: " << packet_id << std::endl;
disconnect();
return true;
});
c->set_suback_handler(
[&]
(packet_id_t packet_id, std::vector<MQTT_NS::suback_return_code> results){
locked_cout() << "[client] suback received. packet_id: " << packet_id << std::endl;
for (auto const& e : results) {
locked_cout() << "[client] subscribe result: " << e << std::endl;
}
if (packet_id == pid_sub1) {
c->publish("mqtt_client_cpp/topic1", "test1", MQTT_NS::qos::at_most_once);
}
else if (packet_id == pid_sub2) {
c->publish("mqtt_client_cpp/topic2_1", "test2_1", MQTT_NS::qos::at_least_once);
c->publish("mqtt_client_cpp/topic2_2", "test2_2", MQTT_NS::qos::exactly_once);
}
return true;
});
c->set_publish_handler(
[&]
(MQTT_NS::optional<packet_id_t> packet_id,
MQTT_NS::publish_options pubopts,
MQTT_NS::buffer topic_name,
MQTT_NS::buffer contents){
locked_cout() << "[client] publish received. "
<< " dup: " << pubopts.get_dup()
<< " qos: " << pubopts.get_qos()
<< " retain: " << pubopts.get_retain() << std::endl;
if (packet_id)
locked_cout() << "[client] packet_id: " << *packet_id << std::endl;
locked_cout() << "[client] topic_name: " << topic_name << std::endl;
locked_cout() << "[client] contents: " << contents << std::endl;
disconnect();
return true;
});
// Connect
c->connect();
}
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <mqtt_server_cpp.hpp>
namespace mi = boost::multi_index;
using con_t = MQTT_NS::server_tls_ws<>::endpoint_t;
using con_sp_t = std::shared_ptr<con_t>;
using con_wp_t = std::weak_ptr<con_t>;
struct sub_con {
sub_con(MQTT_NS::buffer topic, con_sp_t con, MQTT_NS::qos qos_value)
:topic(std::move(topic)), con(std::move(con)), qos_value(qos_value) {}
MQTT_NS::buffer topic;
con_sp_t con;
MQTT_NS::qos qos_value;
};
struct tag_topic {};
struct tag_con {};
struct tag_con_topic {};
using mi_sub_con = mi::multi_index_container<
sub_con,
mi::indexed_by<
mi::ordered_unique<
mi::tag<tag_con_topic>,
mi::composite_key<
sub_con,
BOOST_MULTI_INDEX_MEMBER(sub_con, con_sp_t, con),
BOOST_MULTI_INDEX_MEMBER(sub_con, MQTT_NS::buffer, topic)
>
>,
mi::ordered_non_unique<
mi::tag<tag_topic>,
BOOST_MULTI_INDEX_MEMBER(sub_con, MQTT_NS::buffer, topic)
>,
mi::ordered_non_unique<
mi::tag<tag_con>,
BOOST_MULTI_INDEX_MEMBER(sub_con, con_sp_t, con)
>
>
>;
inline void close_proc(std::set<con_sp_t>& cons, mi_sub_con& subs, con_sp_t const& con) {
cons.erase(con);
auto& idx = subs.get<tag_con>();
auto r = idx.equal_range(con);
idx.erase(r.first, r.second);
}
template <typename Server>
void server_proc(Server& s, std::set<con_sp_t>& connections, mi_sub_con& subs) {
s.set_error_handler(
[](MQTT_NS::error_code ec) {
locked_cout() << "[server] error: " << ec.message() << std::endl;
}
);
s.set_accept_handler(
[&s, &connections, &subs](con_sp_t spep) {
auto& ep = *spep;
std::weak_ptr<con_t> wp(spep);
using packet_id_t = typename std::remove_reference_t<decltype(ep)>::packet_id_t;
locked_cout() << "[server] accept" << std::endl;
// For server close if ep is closed.
auto g = MQTT_NS::shared_scope_guard(
[&s] {
locked_cout() << "[server] session end" << std::endl;
s.close();
}
);
// Pass spep to keep lifetime.
// It makes sure wp.lock() never return nullptr in the handlers below
// including close_handler and error_handler.
ep.start_session(std::make_tuple(std::move(spep), std::move(g)));
// set connection (lower than MQTT) level handlers
ep.set_close_handler(
[&connections, &subs, wp]
(){
locked_cout() << "[server] closed." << std::endl;
auto sp = wp.lock();
BOOST_ASSERT(sp);
close_proc(connections, subs, sp);
});
ep.set_error_handler(
[&connections, &subs, wp]
(MQTT_NS::error_code ec){
locked_cout() << "[server] error: " << ec.message() << std::endl;
auto sp = wp.lock();
BOOST_ASSERT(sp);
close_proc(connections, subs, sp);
});
// set MQTT level handlers
ep.set_connect_handler(
[&connections, wp]
(MQTT_NS::buffer client_id,
MQTT_NS::optional<MQTT_NS::buffer> username,
MQTT_NS::optional<MQTT_NS::buffer> password,
MQTT_NS::optional<MQTT_NS::will>,
bool clean_session,
std::uint16_t keep_alive) {
using namespace MQTT_NS::literals;
locked_cout() << "[server] client_id : " << client_id << std::endl;
locked_cout() << "[server] username : " << (username ? username.value() : "none"_mb) << std::endl;
locked_cout() << "[server] password : " << (password ? password.value() : "none"_mb) << std::endl;
locked_cout() << "[server] clean_session: " << std::boolalpha << clean_session << std::endl;
locked_cout() << "[server] keep_alive : " << keep_alive << std::endl;
auto sp = wp.lock();
BOOST_ASSERT(sp);
connections.insert(sp);
sp->connack(false, MQTT_NS::connect_return_code::accepted);
return true;
}
);
ep.set_disconnect_handler(
[&connections, &subs, wp]
(){
locked_cout() << "[server] disconnect received." << std::endl;
auto sp = wp.lock();
BOOST_ASSERT(sp);
close_proc(connections, subs, sp);
});
ep.set_puback_handler(
[]
(packet_id_t packet_id){
locked_cout() << "[server] puback received. packet_id: " << packet_id << std::endl;
return true;
});
ep.set_pubrec_handler(
[]
(packet_id_t packet_id){
locked_cout() << "[server] pubrec received. packet_id: " << packet_id << std::endl;
return true;
});
ep.set_pubrel_handler(
[]
(packet_id_t packet_id){
locked_cout() << "[server] pubrel received. packet_id: " << packet_id << std::endl;
return true;
});
ep.set_pubcomp_handler(
[]
(packet_id_t packet_id){
locked_cout() << "[server] pubcomp received. packet_id: " << packet_id << std::endl;
return true;
});
ep.set_publish_handler(
[&subs]
(MQTT_NS::optional<packet_id_t> packet_id,
MQTT_NS::publish_options pubopts,
MQTT_NS::buffer topic_name,
MQTT_NS::buffer contents){
locked_cout() << "[server] publish received."
<< " dup: " << pubopts.get_dup()
<< " qos: " << pubopts.get_qos()
<< " retain: " << pubopts.get_retain() << std::endl;
if (packet_id)
locked_cout() << "[server] packet_id: " << *packet_id << std::endl;
locked_cout() << "[server] topic_name: " << topic_name << std::endl;
locked_cout() << "[server] contents: " << contents << std::endl;
auto const& idx = subs.get<tag_topic>();
auto r = idx.equal_range(topic_name);
for (; r.first != r.second; ++r.first) {
r.first->con->publish(
topic_name,
contents,
std::min(r.first->qos_value, pubopts.get_qos())
);
}
return true;
});
ep.set_subscribe_handler(
[&subs, wp]
(packet_id_t packet_id,
std::vector<MQTT_NS::subscribe_entry> entries) {
locked_cout() << "[server] subscribe received. packet_id: " << packet_id << std::endl;
std::vector<MQTT_NS::suback_return_code> res;
res.reserve(entries.size());
auto sp = wp.lock();
BOOST_ASSERT(sp);
for (auto const& e : entries) {
locked_cout() << "[server] topic_filter: " << e.topic_filter << " qos: " << e.subopts.get_qos() << std::endl;
res.emplace_back(MQTT_NS::qos_to_suback_return_code(e.subopts.get_qos()));
subs.emplace(std::move(e.topic_filter), sp, e.subopts.get_qos());
}
sp->suback(packet_id, res);
return true;
}
);
ep.set_unsubscribe_handler(
[&subs, wp]
(packet_id_t packet_id,
std::vector<MQTT_NS::unsubscribe_entry> entries) {
locked_cout() << "[server] unsubscribe received. packet_id: " << packet_id << std::endl;
auto sp = wp.lock();
for (auto const& e : entries) {
auto it = subs.find(std::make_tuple(sp, e.topic_filter));
if (it != subs.end()) {
subs.erase(it);
}
}
BOOST_ASSERT(sp);
sp->unsuback(packet_id);
return true;
}
);
}
);
s.listen();
}
int main(int argc, char** argv) {
if (argc != 2) {
locked_cout() << argv[0] << " port" << std::endl;
return -1;
}
MQTT_NS::setup_log();
std::string path = argv[0];
std::size_t pos = path.find_last_of("/\\");
std::string base = (pos == std::string::npos) ? "./" : path.substr(0, pos + 1);
boost::asio::io_context ioc;
std::uint16_t port = boost::lexical_cast<std::uint16_t>(argv[1]);
// server
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
ctx.use_certificate_chain_file(base + "server.crt.pem");
ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem);
boost::asio::io_context iocs;
auto s = MQTT_NS::server_tls_ws<>(
boost::asio::ip::tcp::endpoint(
boost::asio::ip::tcp::v4(),
port
),
std::move(ctx),
iocs
);
std::set<con_sp_t> connections;
mi_sub_con subs;
std::thread th(
[&] {
server_proc(s, connections, subs);
iocs.run();
}
);
// client
std::uint16_t pid_sub1;
std::uint16_t pid_sub2;
auto c = MQTT_NS::make_tls_sync_client_ws(ioc, "localhost", port);
c->get_ssl_context().load_verify_file(base + "cacert.pem");
int count = 0;
auto disconnect = [&] {
if (++count == 5) c->disconnect();
};
client_proc(c, pid_sub1, pid_sub2, disconnect);
ioc.run();
th.join();
}