From 479e120fc381586a53354495393686b20163d0c3 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sat, 25 Sep 2021 19:43:31 +0900 Subject: [PATCH] Added general async_client reconnect example. --- example/CMakeLists.txt | 1 + example/no_tls_async_client_reconnect.cpp | 105 ++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 example/no_tls_async_client_reconnect.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index c8ae1138c..8a52eff5a 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,6 +1,7 @@ LIST (APPEND exec_PROGRAMS no_tls_client.cpp no_tls_async_client.cpp + no_tls_async_client_reconnect.cpp no_tls_server.cpp no_tls_both.cpp v5_no_tls_client.cpp diff --git a/example/no_tls_async_client_reconnect.cpp b/example/no_tls_async_client_reconnect.cpp new file mode 100644 index 000000000..7179dc2e6 --- /dev/null +++ b/example/no_tls_async_client_reconnect.cpp @@ -0,0 +1,105 @@ +// Copyright Takatoshi Kondo 2020 +// +// 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 +#include +#include + +#include + +int main(int argc, char** argv) { + if (argc != 3) { + std::cout << argv[0] << " host port" << std::endl; + return -1; + } + + MQTT_NS::setup_log(); + + boost::asio::io_context ioc; + + // Create no TLS client + auto c = MQTT_NS::make_async_client(ioc, argv[1], argv[2]); + + // Setup client + c->set_client_id("cid1"); + c->set_clean_session(true); + + boost::asio::steady_timer tim_connect_wait_limit(ioc); + boost::asio::steady_timer tim_reconnect_delay(ioc); + + std::function connect; + std::function reconnect; + + connect = + [&] { + std::cout << "async_connect" << std::endl; + c->async_connect( + [&] + (MQTT_NS::error_code ec){ + std::cout << "async_connect callback: " << ec.message() << std::endl; + if (ec) reconnect(); + } + ); + + std::cout << "tim_connect_wait_limit set" << std::endl; + tim_connect_wait_limit.expires_after(std::chrono::seconds(3)); + tim_connect_wait_limit.async_wait( + [&](boost::system::error_code ec) { + std::cout << "tim_connect_wait_limit callback: " << ec.message() << std::endl; + if (!ec) { + c->async_force_disconnect( + [&](boost::system::error_code ec) { + std::cout << "async_force_disconnect callback: " << ec.message() << std::endl; + } + ); + } + } + ); + }; + reconnect = + [&] { + std::cout << "tim_reconnect_delay set" << std::endl; + tim_reconnect_delay.expires_after(std::chrono::seconds(3)); + tim_reconnect_delay.async_wait( + [&](boost::system::error_code ec) { + std::cout << "tim_reconnect_delay callback: " << ec.message() << std::endl; + if (!ec) connect(); + } + ); + }; + + // Setup handlers + c->set_connack_handler( + [&] + (bool sp, MQTT_NS::connect_return_code connack_return_code){ + std::cout << "Connack handler called" << std::endl; + std::cout << " Session Present: " << std::boolalpha << sp << std::endl; + std::cout << " 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) { + tim_connect_wait_limit.cancel(); + } + else { + reconnect(); + } + return true; + }); + c->set_close_handler( + [&] + (){ + std::cout << "closed." << std::endl; + reconnect(); + }); + c->set_error_handler( + [&] + (MQTT_NS::error_code ec){ + std::cout << "error: " << ec.message() << std::endl; + reconnect(); + }); + + connect(); + ioc.run(); +}