-
Notifications
You must be signed in to change notification settings - Fork 10
/
ep_cpp20coro_mqtt_client.cpp
163 lines (148 loc) · 5.39 KB
/
ep_cpp20coro_mqtt_client.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
// Copyright Takatoshi Kondo 2023
//
// 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)
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <async_mqtt/all.hpp>
namespace as = boost::asio;
namespace am = async_mqtt;
as::awaitable<void>
proc(
auto& amep,
std::string_view host,
std::string_view port) {
auto exe = co_await as::this_coro::executor;
std::cout << "start" << std::endl;
try {
// Handshake undlerying layer (Name resolution and TCP handshaking)
co_await am::async_underlying_handshake(amep.next_layer(), host, port, as::use_awaitable);
std::cout << "Underlying layer handshaked" << std::endl;
// prepare will message if you need.
am::will will{
"WillTopic1",
"WillMessage1",
am::qos::at_most_once,
{ // properties
am::property::user_property{"key1", "val1"},
am::property::content_type{"text"},
}
};
// Send MQTT CONNECT
co_await amep.async_send(
am::v3_1_1::connect_packet{
true, // clean_session
0x1234, // keep_alive
"ClientIdentifier1",
will, // you can pass std::nullopt if you don't want to set the will message
"UserName1",
"Password1"
},
as::use_awaitable
);
// Recv MQTT CONNACK
if (am::packet_variant pv = co_await amep.async_recv(as::use_awaitable)) {
pv.visit(
am::overload {
[&](am::v3_1_1::connack_packet const& p) {
std::cout
<< "MQTT CONNACK recv"
<< " sp:" << p.session_present()
<< std::endl;
},
[](auto const&) {}
}
);
}
// Send MQTT SUBSCRIBE
std::vector<am::topic_subopts> sub_entry{
{"topic1", am::qos::at_most_once}
};
co_await amep.async_send(
am::v3_1_1::subscribe_packet{
*amep.acquire_unique_packet_id(), // sync version only works thread safe context
am::force_move(sub_entry)
},
as::use_awaitable
);
// Recv MQTT SUBACK
if (am::packet_variant pv = co_await amep.async_recv(as::use_awaitable)) {
pv.visit(
am::overload {
[&](am::v3_1_1::suback_packet const& p) {
std::cout
<< "MQTT SUBACK recv"
<< " pid:" << p.packet_id()
<< " entries:";
for (auto const& e : p.entries()) {
std::cout << e << " ";
}
std::cout << std::endl;
},
[](auto const&) {}
}
);
}
// Send MQTT PUBLISH
co_await amep.async_send(
am::v3_1_1::publish_packet{
*amep.acquire_unique_packet_id(), // sync version only works thread safe context
"topic1",
"payload1",
am::qos::at_least_once
},
as::use_awaitable
);
// Recv MQTT PUBLISH and PUBACK (order depends on broker)
for (std::size_t count = 0; count != 2; ++count) {
if (am::packet_variant pv = co_await amep.async_recv(as::use_awaitable)) {
pv.visit(
am::overload {
[&](am::v3_1_1::publish_packet const& p) {
std::cout
<< "MQTT PUBLISH recv"
<< " pid:" << p.packet_id()
<< " topic:" << p.topic()
<< " payload:" << p.payload()
<< " qos:" << p.opts().get_qos()
<< " retain:" << p.opts().get_retain()
<< " dup:" << p.opts().get_dup()
<< std::endl;
},
[&](am::v3_1_1::puback_packet const& p) {
std::cout
<< "MQTT PUBACK recv"
<< " pid:" << p.packet_id()
<< std::endl;
},
[](auto const&) {}
}
);
}
}
std::cout << "close" << std::endl;
co_await amep.async_close(as::use_awaitable);
}
catch (boost::system::system_error const& se) {
std::cout << se.what() << std::endl;
}
}
int main(int argc, char* argv[]) {
am::setup_log(
am::severity_level::info,
true // log colored
);
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " host port" << std::endl;
return -1;
}
as::io_context ioc;
auto amep = am::endpoint<am::role::client, am::protocol::mqtt>{
am::protocol_version::v3_1_1,
ioc.get_executor()
};
as::co_spawn(amep.get_executor(), proc(amep, argv[1], argv[2]), as::detached);
ioc.run();
}