From 2a0b85b4966338ecab6d4bd29590a2b1708e6c35 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Sun, 10 Nov 2024 16:19:33 +0100 Subject: [PATCH] changes to MQTT plugin --- .../DataStreamMQTT/mqtt_client.cpp | 18 ++++-------------- .../DataStreamMQTT/mqtt_client.h | 2 +- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/plotjuggler_plugins/DataStreamMQTT/mqtt_client.cpp b/plotjuggler_plugins/DataStreamMQTT/mqtt_client.cpp index 4b71c57de..ca37ce048 100644 --- a/plotjuggler_plugins/DataStreamMQTT/mqtt_client.cpp +++ b/plotjuggler_plugins/DataStreamMQTT/mqtt_client.cpp @@ -215,21 +215,13 @@ bool MQTTClient::configureMosquitto(const MosquittoConfig& config) { // Threaded mode may not be supported on windows (libmosquitto < 2.1). // See https://github.com/eclipse/mosquitto/issues/2707 - Q_ASSERT(_thread == nullptr); - _thread = new std::thread([this]() { + _thread = std::thread([this]() { int rc = mosquitto_loop_forever(this->_mosq, -1, 1); if (rc != MOSQ_ERR_SUCCESS) { debug() << "MQTT loop forever failed:" << mosquitto_strerror(rc); } }); - if (_thread == nullptr) - { - QMessageBox::warning(nullptr, "MQTT Client", QString("Failed to start MQTT client"), - QMessageBox::Ok); - debug() << "MQTT start failed: could not allocate memory."; - return false; - } } else if (rc != MOSQ_ERR_SUCCESS) { @@ -247,11 +239,9 @@ void MQTTClient::disconnect() { mosquitto_disconnect(_mosq); mosquitto_loop_stop(_mosq, true); - if (_thread != nullptr) + if (_thread.joinable()) { - _thread->join(); - delete _thread; - _thread = nullptr; + _thread.join(); } mosquitto_destroy(_mosq); _mosq = nullptr; @@ -311,4 +301,4 @@ void MQTTClient::unsubscribe(const std::string& topic) { mosquitto_unsubscribe(_mosq, nullptr, topic.c_str()); } -} \ No newline at end of file +} diff --git a/plotjuggler_plugins/DataStreamMQTT/mqtt_client.h b/plotjuggler_plugins/DataStreamMQTT/mqtt_client.h index a9ee7974e..62c8e9f48 100644 --- a/plotjuggler_plugins/DataStreamMQTT/mqtt_client.h +++ b/plotjuggler_plugins/DataStreamMQTT/mqtt_client.h @@ -54,7 +54,7 @@ class MQTTClient : public QObject std::unordered_set _topics_set; std::mutex _mutex; MosquittoConfig _config; - std::thread* _thread; + std::thread _thread; }; #endif // MQTT_CLIENT_H