Skip to content

Commit 66f384d

Browse files
authored
Merge pull request #870 from redboltz/add_reconnect_example
Added general async_client reconnect example.
2 parents f41bf2b + 479e120 commit 66f384d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

example/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
LIST (APPEND exec_PROGRAMS
22
no_tls_client.cpp
33
no_tls_async_client.cpp
4+
no_tls_async_client_reconnect.cpp
45
no_tls_server.cpp
56
no_tls_both.cpp
67
v5_no_tls_client.cpp
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright Takatoshi Kondo 2020
2+
//
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <iostream>
8+
#include <iomanip>
9+
#include <map>
10+
11+
#include <mqtt_client_cpp.hpp>
12+
13+
int main(int argc, char** argv) {
14+
if (argc != 3) {
15+
std::cout << argv[0] << " host port" << std::endl;
16+
return -1;
17+
}
18+
19+
MQTT_NS::setup_log();
20+
21+
boost::asio::io_context ioc;
22+
23+
// Create no TLS client
24+
auto c = MQTT_NS::make_async_client(ioc, argv[1], argv[2]);
25+
26+
// Setup client
27+
c->set_client_id("cid1");
28+
c->set_clean_session(true);
29+
30+
boost::asio::steady_timer tim_connect_wait_limit(ioc);
31+
boost::asio::steady_timer tim_reconnect_delay(ioc);
32+
33+
std::function<void()> connect;
34+
std::function<void()> reconnect;
35+
36+
connect =
37+
[&] {
38+
std::cout << "async_connect" << std::endl;
39+
c->async_connect(
40+
[&]
41+
(MQTT_NS::error_code ec){
42+
std::cout << "async_connect callback: " << ec.message() << std::endl;
43+
if (ec) reconnect();
44+
}
45+
);
46+
47+
std::cout << "tim_connect_wait_limit set" << std::endl;
48+
tim_connect_wait_limit.expires_after(std::chrono::seconds(3));
49+
tim_connect_wait_limit.async_wait(
50+
[&](boost::system::error_code ec) {
51+
std::cout << "tim_connect_wait_limit callback: " << ec.message() << std::endl;
52+
if (!ec) {
53+
c->async_force_disconnect(
54+
[&](boost::system::error_code ec) {
55+
std::cout << "async_force_disconnect callback: " << ec.message() << std::endl;
56+
}
57+
);
58+
}
59+
}
60+
);
61+
};
62+
reconnect =
63+
[&] {
64+
std::cout << "tim_reconnect_delay set" << std::endl;
65+
tim_reconnect_delay.expires_after(std::chrono::seconds(3));
66+
tim_reconnect_delay.async_wait(
67+
[&](boost::system::error_code ec) {
68+
std::cout << "tim_reconnect_delay callback: " << ec.message() << std::endl;
69+
if (!ec) connect();
70+
}
71+
);
72+
};
73+
74+
// Setup handlers
75+
c->set_connack_handler(
76+
[&]
77+
(bool sp, MQTT_NS::connect_return_code connack_return_code){
78+
std::cout << "Connack handler called" << std::endl;
79+
std::cout << " Session Present: " << std::boolalpha << sp << std::endl;
80+
std::cout << " Connack Return Code: "
81+
<< MQTT_NS::connect_return_code_to_str(connack_return_code) << std::endl;
82+
if (connack_return_code == MQTT_NS::connect_return_code::accepted) {
83+
tim_connect_wait_limit.cancel();
84+
}
85+
else {
86+
reconnect();
87+
}
88+
return true;
89+
});
90+
c->set_close_handler(
91+
[&]
92+
(){
93+
std::cout << "closed." << std::endl;
94+
reconnect();
95+
});
96+
c->set_error_handler(
97+
[&]
98+
(MQTT_NS::error_code ec){
99+
std::cout << "error: " << ec.message() << std::endl;
100+
reconnect();
101+
});
102+
103+
connect();
104+
ioc.run();
105+
}

0 commit comments

Comments
 (0)