1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-09 19:31:22 +08:00

Client always created with v5 (universal) persistence format

This commit is contained in:
fpagliughi 2024-06-18 14:09:38 -04:00
parent 90eb5275a9
commit a588b29bc4
3 changed files with 19 additions and 14 deletions

View File

@ -13,7 +13,7 @@
// //
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2013-2023 Frank Pagliughi <fpagliughi@mindspring.com> * Copyright (c) 2013-2024 Frank Pagliughi <fpagliughi@mindspring.com>
* *
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0 * 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[]) int main(int argc, char* argv[])
{ {
// Create a client using MQTT v5 mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID);
mqtt::create_options createOpts(MQTTVERSION_5);
mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID, createOpts);
auto connOpts = mqtt::connect_options_builder() auto connOpts = mqtt::connect_options_builder::v5()
.clean_start(false) .clean_start(false)
.properties({ .properties({
{mqtt::property::SESSION_EXPIRY_INTERVAL, 604800} {mqtt::property::SESSION_EXPIRY_INTERVAL, 604800}

View File

@ -15,7 +15,7 @@
// //
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2019-2023 Frank Pagliughi <fpagliughi@mindspring.com> * Copyright (c) 2019-2024 Frank Pagliughi <fpagliughi@mindspring.com>
* *
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0 * 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; constexpr int QOS = 1;
const string REQ_TOPIC_HDR { "requests/math/" }; const string REQ_TOPIC_HDR { "requests/math/" };
// Create a client using MQTT v5 // Create a client
mqtt::create_options createOpts(MQTTVERSION_5); mqtt::async_client cli(SERVER_ADDRESS, "");
mqtt::async_client cli(SERVER_ADDRESS, "", createOpts);
cli.start_consuming(); cli.start_consuming();
try { try {
cout << "Connecting..." << flush; 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(); auto connRsp = tok->get_connect_response();
cout << "OK (" << connRsp.get_server_uri() << ")" << endl; cout << "OK (" << connRsp.get_server_uri() << ")" << endl;

View File

@ -51,7 +51,7 @@ async_client::async_client(const string& serverURI, const string& clientId,
: serverURI_(serverURI), clientId_(clientId), : serverURI_(serverURI), clientId_(clientId),
mqttVersion_(MQTTVERSION_DEFAULT), userCallback_(nullptr) 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(), int rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(),
MQTTCLIENT_PERSISTENCE_DEFAULT, MQTTCLIENT_PERSISTENCE_DEFAULT,
@ -76,10 +76,13 @@ async_client::async_client(const string& serverURI, const string& clientId,
: serverURI_(serverURI), clientId_(clientId), : serverURI_(serverURI), clientId_(clientId),
mqttVersion_(opts.opts_.MQTTVersion), userCallback_(nullptr) 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(), int rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(),
MQTTCLIENT_PERSISTENCE_DEFAULT, MQTTCLIENT_PERSISTENCE_DEFAULT,
const_cast<char*>(persistDir.c_str()), const_cast<char*>(persistDir.c_str()),
const_cast<MQTTAsync_createOptions*>(&opts.opts_)); &v5opts.opts_);
if (rc != 0) if (rc != 0)
throw exception(rc); throw exception(rc);
} }
@ -92,10 +95,13 @@ async_client::async_client(const string& serverURI, const string& clientId,
{ {
int rc = MQTTASYNC_SUCCESS; int rc = MQTTASYNC_SUCCESS;
create_options v5opts { opts };
v5opts.set_mqtt_version(MQTTVERSION_5);
if (!persistence) { if (!persistence) {
rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(), rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(),
MQTTCLIENT_PERSISTENCE_NONE, nullptr, MQTTCLIENT_PERSISTENCE_NONE, nullptr,
const_cast<MQTTAsync_createOptions*>(&opts.opts_)); &v5opts.opts_);
} }
else { else {
persist_.reset(new MQTTClient_persistence { 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(), rc = MQTTAsync_createWithOptions(&cli_, serverURI.c_str(), clientId.c_str(),
MQTTCLIENT_PERSISTENCE_USER, persist_.get(), MQTTCLIENT_PERSISTENCE_USER, persist_.get(),
const_cast<MQTTAsync_createOptions*>(&opts.opts_)); &v5opts.opts_);
} }
if (rc != 0) if (rc != 0)
throw exception(rc); throw exception(rc);