2024-05-27 10:35:06 +02:00
|
|
|
//
|
2025-02-14 13:42:23 +01:00
|
|
|
// Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
|
2024-05-27 10:35:06 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
2024-12-03 15:26:05 +01:00
|
|
|
#include <boost/asio/use_awaitable.hpp>
|
|
|
|
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
|
|
|
|
2024-03-20 10:27:28 +01:00
|
|
|
//[timeout_with_awaitable_operators
|
2025-01-13 16:11:41 +01:00
|
|
|
|
|
|
|
#include <boost/mqtt5/logger.hpp>
|
|
|
|
#include <boost/mqtt5/mqtt_client.hpp>
|
|
|
|
#include <boost/mqtt5/reason_codes.hpp>
|
|
|
|
#include <boost/mqtt5/types.hpp>
|
2024-03-20 10:27:28 +01:00
|
|
|
|
|
|
|
#include <boost/asio/as_tuple.hpp>
|
|
|
|
#include <boost/asio/co_spawn.hpp>
|
2024-12-03 15:26:05 +01:00
|
|
|
#include <boost/asio/deferred.hpp>
|
2024-03-20 10:27:28 +01:00
|
|
|
#include <boost/asio/detached.hpp>
|
|
|
|
#include <boost/asio/experimental/awaitable_operators.hpp>
|
|
|
|
#include <boost/asio/experimental/parallel_group.hpp>
|
2025-01-13 16:11:41 +01:00
|
|
|
#include <boost/asio/io_context.hpp>
|
2024-03-20 10:27:28 +01:00
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
2025-01-13 16:11:41 +01:00
|
|
|
#include <boost/asio/steady_timer.hpp>
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2024-03-20 10:27:28 +01:00
|
|
|
|
2024-12-03 15:26:05 +01:00
|
|
|
struct config {
|
2025-01-13 16:11:41 +01:00
|
|
|
std::string brokers = "broker.hivemq.com";
|
|
|
|
uint16_t port = 1883;
|
2025-02-17 12:31:40 +01:00
|
|
|
std::string client_id = "boost_mqtt5_tester";
|
2024-12-03 15:26:05 +01:00
|
|
|
};
|
2024-03-20 10:27:28 +01:00
|
|
|
|
|
|
|
// Modified completion token that will prevent co_await from throwing exceptions.
|
2024-12-03 15:26:05 +01:00
|
|
|
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::deferred);
|
|
|
|
|
|
|
|
// client_type with logging enabled
|
2025-01-13 16:11:41 +01:00
|
|
|
using client_type = boost::mqtt5::mqtt_client<
|
|
|
|
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
2024-12-03 15:26:05 +01:00
|
|
|
>;
|
2024-03-20 10:27:28 +01:00
|
|
|
|
2024-12-03 15:26:05 +01:00
|
|
|
// client_type without logging
|
2025-01-13 16:11:41 +01:00
|
|
|
//using client_type = boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
2024-03-20 10:27:28 +01:00
|
|
|
|
2024-12-03 15:26:05 +01:00
|
|
|
boost::asio::awaitable<void> send_over_mqtt(
|
2025-01-13 16:11:41 +01:00
|
|
|
const config& cfg, client_type& client
|
2024-12-03 15:26:05 +01:00
|
|
|
) {
|
2025-01-13 16:11:41 +01:00
|
|
|
client.brokers(cfg.brokers, cfg.port) // Broker that we want to connect to.
|
|
|
|
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
|
|
|
|
.async_run(boost::asio::detached); // Start the Client.
|
|
|
|
|
|
|
|
auto&& [pec, prc, puback_props] = co_await client.async_publish<boost::mqtt5::qos_e::at_least_once>(
|
2025-02-17 12:31:40 +01:00
|
|
|
"boost-mqtt5/test", "Hello World!",
|
2025-01-13 16:11:41 +01:00
|
|
|
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props {},
|
|
|
|
use_nothrow_awaitable
|
|
|
|
);
|
|
|
|
|
|
|
|
co_await client.async_disconnect(use_nothrow_awaitable);
|
|
|
|
co_return;
|
2024-03-20 10:27:28 +01:00
|
|
|
}
|
|
|
|
|
2024-12-03 15:26:05 +01:00
|
|
|
int main(int argc, char** argv) {
|
2025-01-13 16:11:41 +01:00
|
|
|
config cfg;
|
|
|
|
|
|
|
|
if (argc == 4) {
|
|
|
|
cfg.brokers = argv[1];
|
|
|
|
cfg.port = uint16_t(std::stoi(argv[2]));
|
|
|
|
cfg.client_id = argv[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::asio::io_context ioc;
|
|
|
|
|
|
|
|
co_spawn(
|
|
|
|
ioc,
|
|
|
|
[&ioc, &cfg]() -> boost::asio::awaitable<void> {
|
|
|
|
// Initialise the Client to connect to the Broker over TCP.
|
|
|
|
client_type client(ioc);
|
|
|
|
|
|
|
|
// You can also initialise the Client and its logger with a specific log_level (default log_level::info).
|
|
|
|
//client_type client(ioc, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::debug));
|
|
|
|
|
|
|
|
// Construct the timer.
|
|
|
|
boost::asio::steady_timer timer(ioc, std::chrono::seconds(5));
|
|
|
|
|
|
|
|
using namespace boost::asio::experimental::awaitable_operators;
|
|
|
|
auto res = co_await (
|
|
|
|
send_over_mqtt(cfg, client) ||
|
|
|
|
timer.async_wait(boost::asio::as_tuple(boost::asio::use_awaitable))
|
|
|
|
);
|
|
|
|
|
|
|
|
// The timer expired first. The client is cancelled.
|
|
|
|
if (res.index() == 1)
|
|
|
|
std::cout << "Send over MQTT timed out!" << std::endl;
|
|
|
|
// send_over_mqtt completed first. The timer is cancelled.
|
|
|
|
else
|
|
|
|
std::cout << "Send over MQTT completed!" << std::endl;
|
|
|
|
|
|
|
|
},
|
|
|
|
[](std::exception_ptr e) {
|
|
|
|
if (e)
|
|
|
|
std::rethrow_exception(e);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ioc.run();
|
2024-03-20 10:27:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//]
|
2024-12-03 15:26:05 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
int main() {
|
2025-01-13 16:11:41 +01:00
|
|
|
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
2024-12-03 15:26:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|