From a588b29bc49d7fcde32539bbee0a25663eadabe6 Mon Sep 17 00:00:00 2001 From: fpagliughi Date: Tue, 18 Jun 2024 14:09:38 -0400 Subject: [PATCH] Client always created with v5 (universal) persistence format --- examples/async_consume_v5.cpp | 8 +++----- examples/rpc_math_cli.cpp | 11 ++++++----- src/async_client.cpp | 14 ++++++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/examples/async_consume_v5.cpp b/examples/async_consume_v5.cpp index 067dd46..fcb2532 100644 --- a/examples/async_consume_v5.cpp +++ b/examples/async_consume_v5.cpp @@ -13,7 +13,7 @@ // /******************************************************************************* - * Copyright (c) 2013-2023 Frank Pagliughi + * Copyright (c) 2013-2024 Frank Pagliughi * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -49,11 +49,9 @@ const int QOS = 1; int main(int argc, char* argv[]) { - // Create a client using MQTT v5 - mqtt::create_options createOpts(MQTTVERSION_5); - mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID, createOpts); + mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID); - auto connOpts = mqtt::connect_options_builder() + auto connOpts = mqtt::connect_options_builder::v5() .clean_start(false) .properties({ {mqtt::property::SESSION_EXPIRY_INTERVAL, 604800} diff --git a/examples/rpc_math_cli.cpp b/examples/rpc_math_cli.cpp index 261558a..a543828 100644 --- a/examples/rpc_math_cli.cpp +++ b/examples/rpc_math_cli.cpp @@ -15,7 +15,7 @@ // /******************************************************************************* - * Copyright (c) 2019-2023 Frank Pagliughi + * Copyright (c) 2019-2024 Frank Pagliughi * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -59,15 +59,16 @@ int main(int argc, char* argv[]) constexpr int QOS = 1; const string REQ_TOPIC_HDR { "requests/math/" }; - // Create a client using MQTT v5 - mqtt::create_options createOpts(MQTTVERSION_5); - mqtt::async_client cli(SERVER_ADDRESS, "", createOpts); + // Create a client + mqtt::async_client cli(SERVER_ADDRESS, ""); cli.start_consuming(); try { cout << "Connecting..." << flush; - mqtt::token_ptr tok = cli.connect(); //connOpts); + auto connOpts = mqtt::connect_options::v5(); + + mqtt::token_ptr tok = cli.connect(connOpts); auto connRsp = tok->get_connect_response(); cout << "OK (" << connRsp.get_server_uri() << ")" << endl; diff --git a/src/async_client.cpp b/src/async_client.cpp index 039f397..10bc78f 100644 --- a/src/async_client.cpp +++ b/src/async_client.cpp @@ -51,7 +51,7 @@ async_client::async_client(const string& serverURI, const string& clientId, : serverURI_(serverURI), clientId_(clientId), mqttVersion_(MQTTVERSION_DEFAULT), userCallback_(nullptr) { - create_options opts(MQTTVERSION_DEFAULT, maxBufferedMessages); + create_options opts(MQTTVERSION_5, maxBufferedMessages); int rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(), MQTTCLIENT_PERSISTENCE_DEFAULT, @@ -76,10 +76,13 @@ async_client::async_client(const string& serverURI, const string& clientId, : serverURI_(serverURI), clientId_(clientId), mqttVersion_(opts.opts_.MQTTVersion), userCallback_(nullptr) { + create_options v5opts { opts }; + v5opts.set_mqtt_version(MQTTVERSION_5); + int rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(), MQTTCLIENT_PERSISTENCE_DEFAULT, const_cast(persistDir.c_str()), - const_cast(&opts.opts_)); + &v5opts.opts_); if (rc != 0) throw exception(rc); } @@ -92,10 +95,13 @@ async_client::async_client(const string& serverURI, const string& clientId, { int rc = MQTTASYNC_SUCCESS; + create_options v5opts { opts }; + v5opts.set_mqtt_version(MQTTVERSION_5); + if (!persistence) { rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(), MQTTCLIENT_PERSISTENCE_NONE, nullptr, - const_cast(&opts.opts_)); + &v5opts.opts_); } else { persist_.reset(new MQTTClient_persistence { @@ -112,7 +118,7 @@ async_client::async_client(const string& serverURI, const string& clientId, rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(), MQTTCLIENT_PERSISTENCE_USER, persist_.get(), - const_cast(&opts.opts_)); + &v5opts.opts_); } if (rc != 0) throw exception(rc);