mirror of
https://github.com/boostorg/mqtt5.git
synced 2025-08-03 06:24:44 +02:00
Async.MQTT5 -> Boost.MQTT5
Summary: related to T15996 folder structure include/async_mqtt5 -> include/boost/mqtt5 namespace async_mqtt5 -> namespace boost::mqtt5 all tabs replaced with 4 spaces (because tabs are banned) boost-like order of includes TODO: fix all docs Reviewers: ivica Reviewed By: ivica Subscribers: iljazovic, miljen Differential Revision: https://repo.mireo.local/D33152
This commit is contained in:
@@ -9,97 +9,97 @@
|
||||
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
||||
|
||||
//[hello_world_in_coro_multithreaded_env
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/as_tuple.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/asio/thread_pool.hpp>
|
||||
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// client_type with logging enabled
|
||||
using client_type = async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
using client_type = boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
>;
|
||||
|
||||
// client_type without logging
|
||||
//using client_type = async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
//using client_type = boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
|
||||
// Modified completion token that will prevent co_await from throwing exceptions.
|
||||
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::deferred);
|
||||
|
||||
boost::asio::awaitable<void> publish_hello_world(
|
||||
const config& cfg, client_type& client,
|
||||
const boost::asio::strand<boost::asio::thread_pool::executor_type>& strand
|
||||
const config& cfg, client_type& client,
|
||||
const boost::asio::strand<boost::asio::thread_pool::executor_type>& strand
|
||||
) {
|
||||
// Confirmation that the coroutine running in the strand.
|
||||
assert(strand.running_in_this_thread());
|
||||
// Confirmation that the coroutine running in the strand.
|
||||
assert(strand.running_in_this_thread());
|
||||
|
||||
// All these function calls will be executed by the strand that is executing the coroutine.
|
||||
// All the completion handler's associated executors will be that same strand
|
||||
// because the Client was constructed with it as the default associated executor.
|
||||
client.brokers(cfg.brokers, cfg.port) // Set the Broker to connect to.
|
||||
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
|
||||
.async_run(boost::asio::detached); // Start the Client.
|
||||
// All these function calls will be executed by the strand that is executing the coroutine.
|
||||
// All the completion handler's associated executors will be that same strand
|
||||
// because the Client was constructed with it as the default associated executor.
|
||||
client.brokers(cfg.brokers, cfg.port) // Set the Broker to connect to.
|
||||
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
|
||||
.async_run(boost::asio::detached); // Start the Client.
|
||||
|
||||
auto&& [ec, rc, puback_props] = co_await client.async_publish<async_mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test" /* topic */, "Hello world!" /* payload*/, async_mqtt5::retain_e::no,
|
||||
async_mqtt5::publish_props {}, use_nothrow_awaitable);
|
||||
auto&& [ec, rc, puback_props] = co_await client.async_publish<boost::mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test" /* topic */, "Hello world!" /* payload*/, boost::mqtt5::retain_e::no,
|
||||
boost::mqtt5::publish_props {}, use_nothrow_awaitable);
|
||||
|
||||
co_await client.async_disconnect(use_nothrow_awaitable);
|
||||
co_return;
|
||||
co_await client.async_disconnect(use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
|
||||
// Create a thread pool with 4 threads.
|
||||
boost::asio::thread_pool tp(4);
|
||||
// Create a thread pool with 4 threads.
|
||||
boost::asio::thread_pool tp(4);
|
||||
|
||||
// Create an explicit strand from io_context's executor.
|
||||
// The strand guarantees a serialised handler execution regardless of the
|
||||
// number of threads running in the io_context.
|
||||
boost::asio::strand strand = boost::asio::make_strand(tp.get_executor());
|
||||
// Create an explicit strand from io_context's executor.
|
||||
// The strand guarantees a serialised handler execution regardless of the
|
||||
// number of threads running in the io_context.
|
||||
boost::asio::strand strand = boost::asio::make_strand(tp.get_executor());
|
||||
|
||||
// Create the Client with the explicit strand as the default associated executor.
|
||||
client_type client(strand, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Create the Client with the explicit strand as the default associated executor.
|
||||
client_type client(strand, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// Spawn the coroutine.
|
||||
// The executor that executes the coroutine must be the same executor
|
||||
// that is the Client's default associated executor.
|
||||
co_spawn(
|
||||
strand,
|
||||
publish_hello_world(cfg, client, strand),
|
||||
[](std::exception_ptr e) {
|
||||
if (e)
|
||||
std::rethrow_exception(e);
|
||||
}
|
||||
);
|
||||
// Spawn the coroutine.
|
||||
// The executor that executes the coroutine must be the same executor
|
||||
// that is the Client's default associated executor.
|
||||
co_spawn(
|
||||
strand,
|
||||
publish_hello_world(cfg, client, strand),
|
||||
[](std::exception_ptr e) {
|
||||
if (e)
|
||||
std::rethrow_exception(e);
|
||||
}
|
||||
);
|
||||
|
||||
tp.join();
|
||||
tp.join();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//]
|
||||
@@ -109,7 +109,7 @@ int main(int argc, char** argv) {
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -6,101 +6,101 @@
|
||||
//
|
||||
|
||||
//[hello_world_in_multithreaded_env
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/reason_codes.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/dispatch.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/asio/thread_pool.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/dispatch.hpp>
|
||||
#include <boost/asio/thread_pool.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/reason_codes.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
|
||||
// Create a thread pool with 4 threads.
|
||||
boost::asio::thread_pool tp(4);
|
||||
|
||||
// Create an explicit strand from thread_pool's executor.
|
||||
// The strand guarantees a serialised handler execution regardless of the
|
||||
// number of threads running in the thread_pool.
|
||||
boost::asio::strand strand = boost::asio::make_strand(tp.get_executor());
|
||||
// Create a thread pool with 4 threads.
|
||||
boost::asio::thread_pool tp(4);
|
||||
|
||||
// Create an explicit strand from thread_pool's executor.
|
||||
// The strand guarantees a serialised handler execution regardless of the
|
||||
// number of threads running in the thread_pool.
|
||||
boost::asio::strand strand = boost::asio::make_strand(tp.get_executor());
|
||||
|
||||
// Create the Client with the explicit strand as the default associated executor.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
> client(strand, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Create the Client with the explicit strand as the default associated executor.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
> client(strand, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// Configure the client.
|
||||
client.brokers(cfg.brokers, cfg.port) // Broker that we want to connect to.
|
||||
.credentials(cfg.client_id); // Set the Client Identifier. (optional)
|
||||
// Configure the client.
|
||||
client.brokers(cfg.brokers, cfg.port) // Broker that we want to connect to.
|
||||
.credentials(cfg.client_id); // Set the Client Identifier. (optional)
|
||||
|
||||
// Start the Client.
|
||||
// The async_run function call must be posted/dispatched to the strand.
|
||||
boost::asio::dispatch(
|
||||
boost::asio::bind_executor(
|
||||
strand,
|
||||
[&client, &strand, &cfg] {
|
||||
// Considering that the default associated executor of all completion handlers is the strand,
|
||||
// it is not necessary to explicitly bind it to async_run or other async_xxx's handlers.
|
||||
client.async_run(boost::asio::detached); // Start the Client.
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// The async_publish function call must be posted/dispatched to the strand.
|
||||
// The associated executor of async_publish's completion handler must be the same strand.
|
||||
boost::asio::dispatch(
|
||||
boost::asio::bind_executor(
|
||||
strand,
|
||||
[&client, &strand, &cfg] {
|
||||
assert(strand.running_in_this_thread());
|
||||
// Start the Client.
|
||||
// The async_run function call must be posted/dispatched to the strand.
|
||||
boost::asio::dispatch(
|
||||
boost::asio::bind_executor(
|
||||
strand,
|
||||
[&client, &strand, &cfg] {
|
||||
// Considering that the default associated executor of all completion handlers is the strand,
|
||||
// it is not necessary to explicitly bind it to async_run or other async_xxx's handlers.
|
||||
client.async_run(boost::asio::detached); // Start the Client.
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// The async_publish function call must be posted/dispatched to the strand.
|
||||
// The associated executor of async_publish's completion handler must be the same strand.
|
||||
boost::asio::dispatch(
|
||||
boost::asio::bind_executor(
|
||||
strand,
|
||||
[&client, &strand, &cfg] {
|
||||
assert(strand.running_in_this_thread());
|
||||
|
||||
client.async_publish<async_mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test", "Hello World!", async_mqtt5::retain_e::no,
|
||||
async_mqtt5::publish_props {},
|
||||
// You may bind the strand to this handler, but it is not necessary
|
||||
// as the strand is already the default associated handler.
|
||||
[&client, &strand](
|
||||
async_mqtt5::error_code ec, async_mqtt5::reason_code rc,
|
||||
async_mqtt5::puback_props props
|
||||
) {
|
||||
assert(strand.running_in_this_thread());
|
||||
client.async_publish<boost::mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test", "Hello World!", boost::mqtt5::retain_e::no,
|
||||
boost::mqtt5::publish_props {},
|
||||
// You may bind the strand to this handler, but it is not necessary
|
||||
// as the strand is already the default associated handler.
|
||||
[&client, &strand](
|
||||
boost::mqtt5::error_code ec, boost::mqtt5::reason_code rc,
|
||||
boost::mqtt5::puback_props props
|
||||
) {
|
||||
assert(strand.running_in_this_thread());
|
||||
|
||||
std::cout << ec.message() << std::endl;
|
||||
std::cout << rc.message() << std::endl;
|
||||
std::cout << ec.message() << std::endl;
|
||||
std::cout << rc.message() << std::endl;
|
||||
|
||||
// Stop the Client.
|
||||
client.cancel();
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
);
|
||||
// Stop the Client.
|
||||
client.cancel();
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
tp.join();
|
||||
tp.join();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//]
|
||||
|
@@ -6,64 +6,64 @@
|
||||
//
|
||||
|
||||
//[hello_world_over_tcp
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883; // 1883 is the default TCP MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883; // 1883 is the default TCP MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
//[init_tcp_client_with_logger
|
||||
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
> client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
//]
|
||||
//[init_tcp_client_with_logger
|
||||
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
> client(ioc, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
//]
|
||||
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
|
||||
|
||||
//[configure_tcp_client
|
||||
client.brokers(cfg.brokers, cfg.port) // Set the Broker to connect to.
|
||||
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
|
||||
.async_run(boost::asio::detached); // Start the Client.
|
||||
//]
|
||||
//[configure_tcp_client
|
||||
client.brokers(cfg.brokers, cfg.port) // Set the Broker to connect to.
|
||||
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
|
||||
.async_run(boost::asio::detached); // Start the Client.
|
||||
//]
|
||||
|
||||
//[publish_hello_world
|
||||
client.async_publish<async_mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
async_mqtt5::retain_e::yes, async_mqtt5::publish_props {},
|
||||
[&client](async_mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
//[publish_hello_world
|
||||
client.async_publish<boost::mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
boost::mqtt5::retain_e::yes, boost::mqtt5::publish_props {},
|
||||
[&client](boost::mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
|
||||
// Disconnnect the Client.
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
//]
|
||||
// Disconnnect the Client.
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
//]
|
||||
|
||||
ioc.run();
|
||||
ioc.run();
|
||||
}
|
||||
//]
|
||||
|
@@ -6,96 +6,96 @@
|
||||
//
|
||||
|
||||
//[hello_world_over_tls
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 8883; // 8883 is the default TLS MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 8883; // 8883 is the default TLS MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// External customization point.
|
||||
namespace async_mqtt5 {
|
||||
namespace boost::mqtt5 {
|
||||
|
||||
// Specify that the TLS handshake will be performed as a client.
|
||||
template <typename StreamBase>
|
||||
struct tls_handshake_type<boost::asio::ssl::stream<StreamBase>> {
|
||||
static constexpr auto client = boost::asio::ssl::stream_base::client;
|
||||
static constexpr auto client = boost::asio::ssl::stream_base::client;
|
||||
};
|
||||
|
||||
// This client uses this function to indicate which hostname it is
|
||||
// attempting to connect to at the start of the handshaking process.
|
||||
template <typename StreamBase>
|
||||
void assign_tls_sni(
|
||||
const authority_path& ap,
|
||||
boost::asio::ssl::context& /* ctx */,
|
||||
boost::asio::ssl::stream<StreamBase>& stream
|
||||
const authority_path& ap,
|
||||
boost::asio::ssl::context& /* ctx */,
|
||||
boost::asio::ssl::stream<StreamBase>& stream
|
||||
) {
|
||||
SSL_set_tlsext_host_name(stream.native_handle(), ap.host.c_str());
|
||||
SSL_set_tlsext_host_name(stream.native_handle(), ap.host.c_str());
|
||||
}
|
||||
|
||||
} // end namespace async_mqtt5
|
||||
} // end namespace boost::mqtt5
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// TLS context that the underlying SSL stream will use.
|
||||
// The purpose of the context is to allow us to set up TLS/SSL-related options.
|
||||
// See ``__SSL__`` for more information and options.
|
||||
boost::asio::ssl::context context(boost::asio::ssl::context::tls_client);
|
||||
// TLS context that the underlying SSL stream will use.
|
||||
// The purpose of the context is to allow us to set up TLS/SSL-related options.
|
||||
// See ``__SSL__`` for more information and options.
|
||||
boost::asio::ssl::context context(boost::asio::ssl::context::tls_client);
|
||||
|
||||
// Set up the TLS context.
|
||||
// This step is highly dependent on the specific requirements of the Broker you are connecting to.
|
||||
// Each broker may have its own standards and expectations for establishing a secure TLS/SSL connection.
|
||||
// This can include verifying certificates, setting up private keys, PSK authentication, and others.
|
||||
// Set up the TLS context.
|
||||
// This step is highly dependent on the specific requirements of the Broker you are connecting to.
|
||||
// Each broker may have its own standards and expectations for establishing a secure TLS/SSL connection.
|
||||
// This can include verifying certificates, setting up private keys, PSK authentication, and others.
|
||||
|
||||
// Construct the Client with ``__SSL_STREAM__`` as the underlying stream
|
||||
// with ``__SSL_CONTEXT__`` as the ``__TlsContext__`` type
|
||||
// and logging enabled.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>,
|
||||
boost::asio::ssl::context,
|
||||
async_mqtt5::logger
|
||||
> client(ioc, std::move(context), async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Construct the Client with ``__SSL_STREAM__`` as the underlying stream
|
||||
// with ``__SSL_CONTEXT__`` as the ``__TlsContext__`` type
|
||||
// and logging enabled.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>,
|
||||
boost::asio::ssl::context,
|
||||
boost::mqtt5::logger
|
||||
> client(ioc, std::move(context), boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//async_mqtt5::mqtt_client<
|
||||
// boost::asio::ssl::stream<boost::asio::ip::tcp::socket>,
|
||||
// boost::asio::ssl::context
|
||||
//> client(ioc, std::move(context));
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//boost::mqtt5::mqtt_client<
|
||||
// boost::asio::ssl::stream<boost::asio::ip::tcp::socket>,
|
||||
// boost::asio::ssl::context
|
||||
//> client(ioc, std::move(context));
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
client.async_publish<async_mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props{},
|
||||
[&client](async_mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
client.async_publish<boost/mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props{},
|
||||
[&client](boost::mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
|
||||
ioc.run();
|
||||
ioc.run();
|
||||
}
|
||||
//]
|
||||
|
@@ -6,62 +6,60 @@
|
||||
//
|
||||
|
||||
//[hello_world_over_websocket_tcp
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
#include <boost/mqtt5/websocket.hpp> // WebSocket traits
|
||||
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <boost/beast/websocket.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
#include <async_mqtt5/websocket.hpp> // WebSocket traits
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com/mqtt"; // Path example: localhost/mqtt
|
||||
uint16_t port = 8000; // 8083 is the default Webscoket/TCP MQTT port. However, HiveMQ's public broker uses 8000 instead.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com/mqtt"; // Path example: localhost/mqtt
|
||||
uint16_t port = 8000; // 8083 is the default Webscoket/TCP MQTT port. However, HiveMQ's public broker uses 8000 instead.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Construct the Client with WebSocket/TCP as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::beast::websocket::stream<boost::asio::ip::tcp::socket>,
|
||||
std::monostate,
|
||||
async_mqtt5::logger
|
||||
> client(ioc, {}, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Construct the Client with WebSocket/TCP as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::beast::websocket::stream<boost::asio::ip::tcp::socket>,
|
||||
std::monostate,
|
||||
boost::mqtt5::logger
|
||||
> client(ioc, {}, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//async_mqtt5::mqtt_client<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> client(ioc);
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//boost::mqtt5::mqtt_client<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> client(ioc);
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
client.async_publish<async_mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props {},
|
||||
[&client](async_mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
client.async_publish<boost::mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props {},
|
||||
[&client](boost::mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
|
||||
ioc.run();
|
||||
ioc.run();
|
||||
}
|
||||
//]
|
||||
|
@@ -6,99 +6,97 @@
|
||||
//
|
||||
|
||||
//[hello_world_over_websocket_tls
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
#include <boost/mqtt5/websocket.hpp> // WebSocket traits
|
||||
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/beast/ssl/ssl_stream.hpp> // async_teardown specialization for WebSocket SSL stream
|
||||
#include <boost/beast/websocket.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <boost/beast/ssl/ssl_stream.hpp> // async_teardown specialization for WebSocket SSL stream
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
#include <async_mqtt5/websocket.hpp> // WebSocket traits
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com/mqtt"; // Path example: localhost/mqtt
|
||||
uint16_t port = 8884; // 8884 is the default Websocket/TLS MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com/mqtt"; // Path example: localhost/mqtt
|
||||
uint16_t port = 8884; // 8884 is the default Websocket/TLS MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// External customization point.
|
||||
namespace async_mqtt5 {
|
||||
namespace boost::mqtt5 {
|
||||
|
||||
// Specify that the TLS handshake will be performed as a client.
|
||||
template <typename StreamBase>
|
||||
struct tls_handshake_type<boost::asio::ssl::stream<StreamBase>> {
|
||||
static constexpr auto client = boost::asio::ssl::stream_base::client;
|
||||
static constexpr auto client = boost::asio::ssl::stream_base::client;
|
||||
};
|
||||
|
||||
// This client uses this function to indicate which hostname it is
|
||||
// attempting to connect to at the start of the handshaking process.
|
||||
template <typename StreamBase>
|
||||
void assign_tls_sni(
|
||||
const authority_path& ap,
|
||||
boost::asio::ssl::context& /*ctx*/,
|
||||
boost::asio::ssl::stream<StreamBase>& stream
|
||||
const authority_path& ap,
|
||||
boost::asio::ssl::context& /*ctx*/,
|
||||
boost::asio::ssl::stream<StreamBase>& stream
|
||||
) {
|
||||
SSL_set_tlsext_host_name(stream.native_handle(), ap.host.c_str());
|
||||
SSL_set_tlsext_host_name(stream.native_handle(), ap.host.c_str());
|
||||
}
|
||||
|
||||
} // end namespace async_mqtt5
|
||||
} // end namespace boost::mqtt5
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// TLS context that the underlying SSL stream will use.
|
||||
// The purpose of the context is to allow us to set up TLS/SSL-related options.
|
||||
// See ``__SSL__`` for more information and options.
|
||||
boost::asio::ssl::context context(boost::asio::ssl::context::tls_client);
|
||||
// TLS context that the underlying SSL stream will use.
|
||||
// The purpose of the context is to allow us to set up TLS/SSL-related options.
|
||||
// See ``__SSL__`` for more information and options.
|
||||
boost::asio::ssl::context context(boost::asio::ssl::context::tls_client);
|
||||
|
||||
// Set up the TLS context.
|
||||
// This step is highly dependent on the specific requirements of the Broker you are connecting to.
|
||||
// Each broker may have its own standards and expectations for establishing a secure TLS/SSL connection.
|
||||
// This can include verifying certificates, setting up private keys, PSK authentication, and others.
|
||||
// Set up the TLS context.
|
||||
// This step is highly dependent on the specific requirements of the Broker you are connecting to.
|
||||
// Each broker may have its own standards and expectations for establishing a secure TLS/SSL connection.
|
||||
// This can include verifying certificates, setting up private keys, PSK authentication, and others.
|
||||
|
||||
// Construct the Client with WebSocket/SSL as the underlying stream
|
||||
// with ``__SSL_CONTEXT__`` as the ``__TlsContext__`` type and enabled logging.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>,
|
||||
boost::asio::ssl::context,
|
||||
async_mqtt5::logger
|
||||
> client(ioc, std::move(context), async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Construct the Client with WebSocket/SSL as the underlying stream
|
||||
// with ``__SSL_CONTEXT__`` as the ``__TlsContext__`` type and enabled logging.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>,
|
||||
boost::asio::ssl::context,
|
||||
boost::mqtt5::logger
|
||||
> client(ioc, std::move(context), boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//async_mqtt5::mqtt_client<
|
||||
// boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>,
|
||||
// boost::asio::ssl::context
|
||||
//> client(ioc, std::move(context));
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//boost::mqtt5::mqtt_client<
|
||||
// boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>,
|
||||
// boost::asio::ssl::context
|
||||
//> client(ioc, std::move(context));
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
client.async_publish<async_mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props{},
|
||||
[&client](async_mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
client.async_publish<boost::mqtt5::qos_e::at_most_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props{},
|
||||
[&client](boost::mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
}
|
||||
);
|
||||
|
||||
ioc.run();
|
||||
ioc.run();
|
||||
}
|
||||
//]
|
||||
|
@@ -6,65 +6,65 @@
|
||||
//
|
||||
|
||||
//[multiflight_client
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/reason_codes.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/reason_codes.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
> client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
> client(ioc, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
// Publish with QoS 2 five times in a row without waiting for the handler
|
||||
// of the previous async_publish call to be invoked.
|
||||
for (auto i = 1; i <= 5; ++i)
|
||||
client.async_publish<async_mqtt5::qos_e::exactly_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props {},
|
||||
[i](async_mqtt5::error_code ec, async_mqtt5::reason_code rc, async_mqtt5::pubcomp_props) {
|
||||
std::cout << "Publish number " << i << " completed with: " << std::endl;
|
||||
std::cout << "\t ec: " << ec.message() << std::endl;
|
||||
std::cout << "\t rc: " << rc.message() << std::endl;
|
||||
}
|
||||
);
|
||||
// Publish with QoS 2 five times in a row without waiting for the handler
|
||||
// of the previous async_publish call to be invoked.
|
||||
for (auto i = 1; i <= 5; ++i)
|
||||
client.async_publish<boost::mqtt5::qos_e::exactly_once>(
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props {},
|
||||
[i](boost::mqtt5::error_code ec, boost::mqtt5::reason_code rc, boost::mqtt5::pubcomp_props) {
|
||||
std::cout << "Publish number " << i << " completed with: " << std::endl;
|
||||
std::cout << "\t ec: " << ec.message() << std::endl;
|
||||
std::cout << "\t rc: " << rc.message() << std::endl;
|
||||
}
|
||||
);
|
||||
|
||||
// We can stop the Client by using signals.
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&client](async_mqtt5::error_code, int) {
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
});
|
||||
// We can stop the Client by using signals.
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&client](boost::mqtt5::error_code, int) {
|
||||
client.async_disconnect(boost::asio::detached);
|
||||
});
|
||||
|
||||
ioc.run();
|
||||
ioc.run();
|
||||
}
|
||||
//]
|
||||
|
@@ -9,131 +9,130 @@
|
||||
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
||||
|
||||
//[publisher
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/reason_codes.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/as_tuple.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/reason_codes.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// Modified completion token that will prevent co_await from throwing exceptions.
|
||||
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::deferred);
|
||||
|
||||
// client_type with logging enabled
|
||||
using client_type = async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
using client_type = boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
>;
|
||||
|
||||
// client_type without logging
|
||||
//using client_type = async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
//using client_type = boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
|
||||
int next_sensor_reading() {
|
||||
srand(static_cast<unsigned int>(std::time(0)));
|
||||
return rand() % 100;
|
||||
srand(static_cast<unsigned int>(std::time(0)));
|
||||
return rand() % 100;
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> publish_sensor_readings(
|
||||
const config& cfg, client_type& client, boost::asio::steady_timer& timer
|
||||
const config& cfg, client_type& client, boost::asio::steady_timer& timer
|
||||
) {
|
||||
// Configure the Client.
|
||||
// It is mandatory to call brokers() and async_run() to configure the Brokers to connect to and start the Client.
|
||||
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.
|
||||
// Configure the Client.
|
||||
// It is mandatory to call brokers() and async_run() to configure the Brokers to connect to and start the Client.
|
||||
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.
|
||||
|
||||
for (;;) {
|
||||
// Get the next sensor reading.
|
||||
auto reading = std::to_string(next_sensor_reading());
|
||||
for (;;) {
|
||||
// Get the next sensor reading.
|
||||
auto reading = std::to_string(next_sensor_reading());
|
||||
|
||||
// Publish the sensor reading with QoS 1.
|
||||
auto&& [ec, rc, props] = co_await client.async_publish<async_mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test" /* topic */, reading /* payload */,
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props {}, use_nothrow_awaitable
|
||||
);
|
||||
// An error can occur as a result of:
|
||||
// a) wrong publish parameters
|
||||
// b) mqtt_client::cancel is called while the Client is publishing the message
|
||||
// resulting in cancellation.
|
||||
if (ec) {
|
||||
std::cout << "Publish error occurred: " << ec.message() << std::endl;
|
||||
break;
|
||||
}
|
||||
// Publish the sensor reading with QoS 1.
|
||||
auto&& [ec, rc, props] = co_await client.async_publish<boost::mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test" /* topic */, reading /* payload */,
|
||||
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props {}, use_nothrow_awaitable
|
||||
);
|
||||
// An error can occur as a result of:
|
||||
// a) wrong publish parameters
|
||||
// b) mqtt_client::cancel is called while the Client is publishing the message
|
||||
// resulting in cancellation.
|
||||
if (ec) {
|
||||
std::cout << "Publish error occurred: " << ec.message() << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Reason code is the reply from the server presenting the result of the publish operation.
|
||||
std::cout << "Result of publish request: " << rc.message() << std::endl;
|
||||
if (!rc)
|
||||
std::cout << "Published sensor reading: " << reading << std::endl;
|
||||
// Reason code is the reply from the server presenting the result of the publish operation.
|
||||
std::cout << "Result of publish request: " << rc.message() << std::endl;
|
||||
if (!rc)
|
||||
std::cout << "Published sensor reading: " << reading << std::endl;
|
||||
|
||||
// Wait 5 seconds before publishing the next reading.
|
||||
timer.expires_after(std::chrono::seconds(5));
|
||||
auto&& [tec] = co_await timer.async_wait(use_nothrow_awaitable);
|
||||
// Wait 5 seconds before publishing the next reading.
|
||||
timer.expires_after(std::chrono::seconds(5));
|
||||
auto&& [tec] = co_await timer.async_wait(use_nothrow_awaitable);
|
||||
|
||||
// An error occurred if we cancelled the timer.
|
||||
if (tec)
|
||||
break;
|
||||
}
|
||||
// An error occurred if we cancelled the timer.
|
||||
if (tec)
|
||||
break;
|
||||
}
|
||||
|
||||
co_return;
|
||||
co_return;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
|
||||
// Initialise execution context.
|
||||
boost::asio::io_context ioc;
|
||||
// Initialise execution context.
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Initialise the Client to connect to the Broker over TCP.
|
||||
client_type client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Initialise the Client to connect to the Broker over TCP.
|
||||
client_type client(ioc, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// Initialise the timer.
|
||||
boost::asio::steady_timer timer(ioc);
|
||||
// Initialise the timer.
|
||||
boost::asio::steady_timer timer(ioc);
|
||||
|
||||
// Set up signals to stop the program on demand.
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&client, &timer](async_mqtt5::error_code /* ec */, int /* signal */) {
|
||||
// After we are done with publishing all the messages, cancel the timer and the Client.
|
||||
// Alternatively, use mqtt_client::async_disconnect.
|
||||
timer.cancel();
|
||||
client.cancel();
|
||||
});
|
||||
// Set up signals to stop the program on demand.
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&client, &timer](boost::mqtt5::error_code /* ec */, int /* signal */) {
|
||||
// After we are done with publishing all the messages, cancel the timer and the Client.
|
||||
// Alternatively, use mqtt_client::async_disconnect.
|
||||
timer.cancel();
|
||||
client.cancel();
|
||||
});
|
||||
|
||||
// Spawn the coroutine.
|
||||
co_spawn(
|
||||
ioc.get_executor(),
|
||||
publish_sensor_readings(cfg, client, timer),
|
||||
[](std::exception_ptr e) {
|
||||
if (e)
|
||||
std::rethrow_exception(e);
|
||||
}
|
||||
);
|
||||
// Spawn the coroutine.
|
||||
co_spawn(
|
||||
ioc.get_executor(),
|
||||
publish_sensor_readings(cfg, client, timer),
|
||||
[](std::exception_ptr e) {
|
||||
if (e)
|
||||
std::rethrow_exception(e);
|
||||
}
|
||||
);
|
||||
|
||||
// Start the execution.
|
||||
ioc.run();
|
||||
// Start the execution.
|
||||
ioc.run();
|
||||
}
|
||||
|
||||
//]
|
||||
@@ -143,7 +142,7 @@ int main(int argc, char** argv) {
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -9,142 +9,142 @@
|
||||
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
||||
|
||||
//[receiver
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/reason_codes.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/as_tuple.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/reason_codes.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// Modified completion token that will prevent co_await from throwing exceptions.
|
||||
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::deferred);
|
||||
|
||||
// client_type with logging enabled
|
||||
using client_type = async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
using client_type = boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
>;
|
||||
|
||||
// client_type without logging
|
||||
//using client_type = async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
//using client_type = boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
|
||||
boost::asio::awaitable<bool> subscribe(client_type& client) {
|
||||
// Configure the request to subscribe to a Topic.
|
||||
async_mqtt5::subscribe_topic sub_topic = async_mqtt5::subscribe_topic{
|
||||
"test" /* topic */,
|
||||
async_mqtt5::subscribe_options {
|
||||
async_mqtt5::qos_e::exactly_once, // All messages will arrive at QoS 2.
|
||||
async_mqtt5::no_local_e::no, // Forward message from Clients with same ID.
|
||||
async_mqtt5::retain_as_published_e::retain, // Keep the original RETAIN flag.
|
||||
async_mqtt5::retain_handling_e::send // Send retained messages when the subscription is established.
|
||||
}
|
||||
};
|
||||
// Configure the request to subscribe to a Topic.
|
||||
boost::mqtt5::subscribe_topic sub_topic = boost::mqtt5::subscribe_topic{
|
||||
"test" /* topic */,
|
||||
boost::mqtt5::subscribe_options {
|
||||
boost::mqtt5::qos_e::exactly_once, // All messages will arrive at QoS 2.
|
||||
boost::mqtt5::no_local_e::no, // Forward message from Clients with same ID.
|
||||
boost::mqtt5::retain_as_published_e::retain, // Keep the original RETAIN flag.
|
||||
boost::mqtt5::retain_handling_e::send // Send retained messages when the subscription is established.
|
||||
}
|
||||
};
|
||||
|
||||
// Subscribe to a single Topic.
|
||||
auto&& [ec, sub_codes, sub_props] = co_await client.async_subscribe(
|
||||
sub_topic, async_mqtt5::subscribe_props {}, use_nothrow_awaitable
|
||||
);
|
||||
// Note: you can subscribe to multiple Topics in one mqtt_client::async_subscribe call.
|
||||
// Subscribe to a single Topic.
|
||||
auto&& [ec, sub_codes, sub_props] = co_await client.async_subscribe(
|
||||
sub_topic, boost::mqtt5::subscribe_props {}, use_nothrow_awaitable
|
||||
);
|
||||
// Note: you can subscribe to multiple Topics in one mqtt_client::async_subscribe call.
|
||||
|
||||
// An error can occur as a result of:
|
||||
// a) wrong subscribe parameters
|
||||
// b) mqtt_client::cancel is called while the Client is in the process of subscribing
|
||||
if (ec)
|
||||
std::cout << "Subscribe error occurred: " << ec.message() << std::endl;
|
||||
else
|
||||
std::cout << "Result of subscribe request: " << sub_codes[0].message() << std::endl;
|
||||
// An error can occur as a result of:
|
||||
// a) wrong subscribe parameters
|
||||
// b) mqtt_client::cancel is called while the Client is in the process of subscribing
|
||||
if (ec)
|
||||
std::cout << "Subscribe error occurred: " << ec.message() << std::endl;
|
||||
else
|
||||
std::cout << "Result of subscribe request: " << sub_codes[0].message() << std::endl;
|
||||
|
||||
co_return !ec && !sub_codes[0]; // True if the subscription was successfully established.
|
||||
co_return !ec && !sub_codes[0]; // True if the subscription was successfully established.
|
||||
}
|
||||
|
||||
boost::asio::awaitable<void> subscribe_and_receive(
|
||||
const config& cfg, client_type& client
|
||||
const config& cfg, client_type& client
|
||||
) {
|
||||
// Configure the Client.
|
||||
// It is mandatory to call brokers() and async_run() to configure the Brokers to connect to and start the Client.
|
||||
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.
|
||||
// Configure the Client.
|
||||
// It is mandatory to call brokers() and async_run() to configure the Brokers to connect to and start the Client.
|
||||
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.
|
||||
|
||||
// Before attempting to receive an Application Message from the Topic we just subscribed to,
|
||||
// it is advisable to verify that the subscription succeeded.
|
||||
// It is not recommended to call mqtt_client::async_receive if you do not have any
|
||||
// subscription established as the corresponding handler will never be invoked.
|
||||
if (!(co_await subscribe(client)))
|
||||
co_return;
|
||||
// Before attempting to receive an Application Message from the Topic we just subscribed to,
|
||||
// it is advisable to verify that the subscription succeeded.
|
||||
// It is not recommended to call mqtt_client::async_receive if you do not have any
|
||||
// subscription established as the corresponding handler will never be invoked.
|
||||
if (!(co_await subscribe(client)))
|
||||
co_return;
|
||||
|
||||
for (;;) {
|
||||
// Receive an Appplication Message from the subscribed Topic(s).
|
||||
auto&& [ec, topic, payload, publish_props] = co_await client.async_receive(use_nothrow_awaitable);
|
||||
for (;;) {
|
||||
// Receive an Appplication Message from the subscribed Topic(s).
|
||||
auto&& [ec, topic, payload, publish_props] = co_await client.async_receive(use_nothrow_awaitable);
|
||||
|
||||
if (ec == async_mqtt5::client::error::session_expired) {
|
||||
// The Client has reconnected, and the prior session has expired.
|
||||
// As a result, any previous subscriptions have been lost and must be reinstated.
|
||||
if (co_await subscribe(client))
|
||||
continue;
|
||||
else
|
||||
break;
|
||||
} else if (ec)
|
||||
break;
|
||||
if (ec == boost::mqtt5::client::error::session_expired) {
|
||||
// The Client has reconnected, and the prior session has expired.
|
||||
// As a result, any previous subscriptions have been lost and must be reinstated.
|
||||
if (co_await subscribe(client))
|
||||
continue;
|
||||
else
|
||||
break;
|
||||
} else if (ec)
|
||||
break;
|
||||
|
||||
std::cout << "Received message from the Broker" << std::endl;
|
||||
std::cout << "\t topic: " << topic << std::endl;
|
||||
std::cout << "\t payload: " << payload << std::endl;
|
||||
}
|
||||
std::cout << "Received message from the Broker" << std::endl;
|
||||
std::cout << "\t topic: " << topic << std::endl;
|
||||
std::cout << "\t payload: " << payload << std::endl;
|
||||
}
|
||||
|
||||
co_return;
|
||||
co_return;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
|
||||
// Initialise execution context.
|
||||
boost::asio::io_context ioc;
|
||||
// Initialise execution context.
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Initialise the Client to connect to the Broker over TCP.
|
||||
client_type client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Initialise the Client to connect to the Broker over TCP.
|
||||
client_type client(ioc, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// Set up signals to stop the program on demand.
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&client](async_mqtt5::error_code /* ec */, int /* signal */) {
|
||||
// After we are done with publishing all the messages, cancel the timer and the Client.
|
||||
// Alternatively, use mqtt_client::async_disconnect.
|
||||
client.cancel();
|
||||
});
|
||||
// Set up signals to stop the program on demand.
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait([&client](boost::mqtt5::error_code /* ec */, int /* signal */) {
|
||||
// After we are done with publishing all the messages, cancel the timer and the Client.
|
||||
// Alternatively, use mqtt_client::async_disconnect.
|
||||
client.cancel();
|
||||
});
|
||||
|
||||
// Spawn the coroutine.
|
||||
co_spawn(
|
||||
ioc,
|
||||
subscribe_and_receive(cfg, client),
|
||||
[](std::exception_ptr e) {
|
||||
if (e)
|
||||
std::rethrow_exception(e);
|
||||
}
|
||||
);
|
||||
// Spawn the coroutine.
|
||||
co_spawn(
|
||||
ioc,
|
||||
subscribe_and_receive(cfg, client),
|
||||
[](std::exception_ptr e) {
|
||||
if (e)
|
||||
std::rethrow_exception(e);
|
||||
}
|
||||
);
|
||||
|
||||
// Start the execution.
|
||||
ioc.run();
|
||||
// Start the execution.
|
||||
ioc.run();
|
||||
}
|
||||
|
||||
//]
|
||||
@@ -154,7 +154,7 @@ int main(int argc, char** argv) {
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -9,104 +9,106 @@
|
||||
#ifdef BOOST_ASIO_HAS_CO_AWAIT
|
||||
|
||||
//[timeout_with_awaitable_operators
|
||||
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/reason_codes.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/as_tuple.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/experimental/awaitable_operators.hpp>
|
||||
#include <boost/asio/experimental/parallel_group.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
|
||||
#include <boost/mysql/any_connection.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/asio/as_tuple.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <boost/asio/use_awaitable.hpp>
|
||||
#include <boost/asio/experimental/awaitable_operators.hpp>
|
||||
#include <boost/asio/experimental/parallel_group.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/reason_codes.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// Modified completion token that will prevent co_await from throwing exceptions.
|
||||
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::deferred);
|
||||
|
||||
// client_type with logging enabled
|
||||
using client_type = async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
using client_type = boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
>;
|
||||
|
||||
// client_type without logging
|
||||
//using client_type = async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
//using client_type = boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
|
||||
|
||||
boost::asio::awaitable<void> send_over_mqtt(
|
||||
const config& cfg, client_type& client
|
||||
const config& cfg, client_type& client
|
||||
) {
|
||||
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.
|
||||
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<async_mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test", "Hello World!",
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props {},
|
||||
use_nothrow_awaitable
|
||||
);
|
||||
auto&& [pec, prc, puback_props] = co_await client.async_publish<boost::mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test", "Hello World!",
|
||||
boost::mqtt5::retain_e::no, boost::mqtt5::publish_props {},
|
||||
use_nothrow_awaitable
|
||||
);
|
||||
|
||||
co_await client.async_disconnect(use_nothrow_awaitable);
|
||||
co_return;
|
||||
co_await client.async_disconnect(use_nothrow_awaitable);
|
||||
co_return;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
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);
|
||||
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 */, async_mqtt5::logger(async_mqtt5::log_level::debug));
|
||||
// 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));
|
||||
// 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))
|
||||
);
|
||||
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);
|
||||
}
|
||||
);
|
||||
// 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();
|
||||
ioc.run();
|
||||
}
|
||||
|
||||
//]
|
||||
@@ -116,7 +118,7 @@ int main(int argc, char** argv) {
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -6,94 +6,94 @@
|
||||
//
|
||||
|
||||
//[timeout_with_parallel_group
|
||||
#include <boost/mqtt5/logger.hpp>
|
||||
#include <boost/mqtt5/mqtt_client.hpp>
|
||||
#include <boost/mqtt5/reason_codes.hpp>
|
||||
#include <boost/mqtt5/types.hpp>
|
||||
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/experimental/parallel_group.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/deferred.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <boost/asio/experimental/parallel_group.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/reason_codes.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
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;
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
> client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
|
||||
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
|
||||
boost::mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, boost::mqtt5::logger
|
||||
> client(ioc, {} /* tls_context */, boost::mqtt5::logger(boost::mqtt5::log_level::info));
|
||||
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//boost::mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
|
||||
|
||||
// Construct the timer.
|
||||
boost::asio::steady_timer timer(ioc, std::chrono::seconds(5));
|
||||
// Construct the timer.
|
||||
boost::asio::steady_timer timer(ioc, std::chrono::seconds(5));
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
// Subscribe to a Topic.
|
||||
client.async_subscribe(
|
||||
{ "test" /* Topic */}, async_mqtt5::subscribe_props {},
|
||||
[](async_mqtt5::error_code ec, std::vector<async_mqtt5::reason_code> rcs, async_mqtt5::suback_props) {
|
||||
std::cout << "[subscribe ec]: " << ec.message() << std::endl;
|
||||
std::cout << "[subscribe rc]: " << rcs[0].message() << std::endl;
|
||||
}
|
||||
);
|
||||
// Subscribe to a Topic.
|
||||
client.async_subscribe(
|
||||
{ "test" /* Topic */}, boost::mqtt5::subscribe_props {},
|
||||
[](boost::mqtt5::error_code ec, std::vector<boost::mqtt5::reason_code> rcs, boost::mqtt5::suback_props) {
|
||||
std::cout << "[subscribe ec]: " << ec.message() << std::endl;
|
||||
std::cout << "[subscribe rc]: " << rcs[0].message() << std::endl;
|
||||
}
|
||||
);
|
||||
|
||||
// Create a parallel group to wait up to 5 seconds to receive a message
|
||||
// using client.async_receive(...).
|
||||
boost::asio::experimental::make_parallel_group(
|
||||
timer.async_wait(boost::asio::deferred),
|
||||
client.async_receive(boost::asio::deferred)
|
||||
).async_wait(
|
||||
boost::asio::experimental::wait_for_one(),
|
||||
[&client](
|
||||
std::array<std::size_t, 2> ord, // Completion order
|
||||
async_mqtt5::error_code /* timer_ec */, // timer.async_wait(...) handler signature
|
||||
// client.async_receive(...) handler signature
|
||||
async_mqtt5::error_code receive_ec,
|
||||
std::string topic, std::string payload, async_mqtt5::publish_props /* props */
|
||||
) {
|
||||
if (ord[0] == 1) {
|
||||
std::cout << "Received a message!" << std::endl;
|
||||
std::cout << "[receive ec]: " << receive_ec.message() << std::endl;
|
||||
std::cout << "[receive topic]: " << topic << std::endl;
|
||||
std::cout << "[receive payload]: " << payload << std::endl;
|
||||
}
|
||||
else
|
||||
std::cout << "Timed out! Did not receive a message within 5 seconds." << std::endl;
|
||||
// Create a parallel group to wait up to 5 seconds to receive a message
|
||||
// using client.async_receive(...).
|
||||
boost::asio::experimental::make_parallel_group(
|
||||
timer.async_wait(boost::asio::deferred),
|
||||
client.async_receive(boost::asio::deferred)
|
||||
).async_wait(
|
||||
boost::asio::experimental::wait_for_one(),
|
||||
[&client](
|
||||
std::array<std::size_t, 2> ord, // Completion order
|
||||
boost::mqtt5::error_code /* timer_ec */, // timer.async_wait(...) handler signature
|
||||
// client.async_receive(...) handler signature
|
||||
boost::mqtt5::error_code receive_ec,
|
||||
std::string topic, std::string payload, boost::mqtt5::publish_props /* props */
|
||||
) {
|
||||
if (ord[0] == 1) {
|
||||
std::cout << "Received a message!" << std::endl;
|
||||
std::cout << "[receive ec]: " << receive_ec.message() << std::endl;
|
||||
std::cout << "[receive topic]: " << topic << std::endl;
|
||||
std::cout << "[receive payload]: " << payload << std::endl;
|
||||
}
|
||||
else
|
||||
std::cout << "Timed out! Did not receive a message within 5 seconds." << std::endl;
|
||||
|
||||
client.cancel();
|
||||
}
|
||||
);
|
||||
client.cancel();
|
||||
}
|
||||
);
|
||||
|
||||
ioc.run();
|
||||
ioc.run();
|
||||
}
|
||||
|
||||
//]
|
||||
|
Reference in New Issue
Block a user