From c85b3b30b2fe7c905c94bd63062f2a344be041f9 Mon Sep 17 00:00:00 2001 From: Will Sobel Date: Thu, 11 Apr 2024 18:32:59 -0400 Subject: [PATCH 1/2] Fixed port in url for MQTT service. --- CMakeLists.txt | 3 ++- src/mtconnect/mqtt/mqtt_client_impl.hpp | 1 + src/mtconnect/source/adapter/mqtt/mqtt_adapter.cpp | 11 +++-------- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1459c364..f55bb6b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,8 @@ set(AGENT_VERSION_MAJOR 2) set(AGENT_VERSION_MINOR 3) set(AGENT_VERSION_PATCH 0) -set(AGENT_VERSION_BUILD 6) +set(AGENT_VERSION_BUILD 7 +) set(AGENT_VERSION_RC "") # This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent diff --git a/src/mtconnect/mqtt/mqtt_client_impl.hpp b/src/mtconnect/mqtt/mqtt_client_impl.hpp index dc1747d4..f40f6973 100644 --- a/src/mtconnect/mqtt/mqtt_client_impl.hpp +++ b/src/mtconnect/mqtt/mqtt_client_impl.hpp @@ -164,6 +164,7 @@ namespace mtconnect { m_connected = false; if (m_handler && m_handler->m_disconnected) m_handler->m_disconnected(shared_from_this()); + m_handler->m_disconnected(shared_from_this()); if (m_running) { reconnect(); diff --git a/src/mtconnect/source/adapter/mqtt/mqtt_adapter.cpp b/src/mtconnect/source/adapter/mqtt/mqtt_adapter.cpp index de1399a0..00a4d61c 100644 --- a/src/mtconnect/source/adapter/mqtt/mqtt_adapter.cpp +++ b/src/mtconnect/source/adapter/mqtt/mqtt_adapter.cpp @@ -85,12 +85,7 @@ namespace mtconnect { { m_options[configuration::MqttHost] = m_options[configuration::Host]; } - if (!HasOption(m_options, configuration::MqttPort) && - HasOption(m_options, configuration::Port)) - { - m_options[configuration::MqttPort] = m_options[configuration::Port]; - } - else + if (!HasOption(m_options, configuration::MqttPort)) { m_options[configuration::MqttPort] = 1883; } @@ -123,7 +118,7 @@ namespace mtconnect { !IsOptionSet(m_options, configuration::MqttWs)) { m_client = make_shared(m_ioContext, m_options, - move(clientHandler)); + std::move(clientHandler)); } else if (IsOptionSet(m_options, configuration::MqttWs) && IsOptionSet(m_options, configuration::MqttTls)) @@ -139,7 +134,7 @@ namespace mtconnect { else { m_client = make_shared(m_ioContext, m_options, - move(clientHandler)); + std::move(clientHandler)); } m_identity = m_client->getIdentity(); From 0dd7ed93f73bd485a9d339466d2bd157431093a5 Mon Sep 17 00:00:00 2001 From: Will Sobel Date: Fri, 12 Apr 2024 09:52:41 -0400 Subject: [PATCH 2/2] fixed crash when closing mqtt client. set close handler to nullptr if already disconnected. --- src/mtconnect/mqtt/mqtt_client_impl.hpp | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/mtconnect/mqtt/mqtt_client_impl.hpp b/src/mtconnect/mqtt/mqtt_client_impl.hpp index f40f6973..25b039b6 100644 --- a/src/mtconnect/mqtt/mqtt_client_impl.hpp +++ b/src/mtconnect/mqtt/mqtt_client_impl.hpp @@ -236,6 +236,7 @@ namespace mtconnect { { client->async_disconnect(10s, [client, url](mqtt::error_code ec) { LOG(warning) << url << " disconnected: " << ec.message(); + client->set_close_handler(nullptr); }); client.reset(); @@ -412,6 +413,13 @@ namespace mtconnect { public: using base = MqttClientImpl; using base::base; + /// @brief Get a shared pointer to this + /// @return shared pointer to this + shared_ptr getptr() + { + return static_pointer_cast(shared_from_this()); + } + /// @brief Get the Mqtt TCP Client /// @return pointer to the Mqtt TCP Client auto &getClient() @@ -438,6 +446,12 @@ namespace mtconnect { public: using base = MqttClientImpl; using base::base; + /// @brief Get a shared pointer to this + /// @return shared pointer to this + shared_ptr getptr() + { + return static_pointer_cast(shared_from_this()); + } /// @brief Get the Mqtt TLS Client /// @return pointer to the Mqtt TLS Client @@ -481,6 +495,13 @@ namespace mtconnect { public: using base = MqttClientImpl; using base::base; + /// @brief Get a shared pointer to this + /// @return shared pointer to this + shared_ptr getptr() + { + return static_pointer_cast(shared_from_this()); + } + /// @brief Get the Mqtt TLS WebSocket Client /// @return pointer to the Mqtt TLS WebSocket Client auto &getClient() @@ -513,6 +534,13 @@ namespace mtconnect { public: using base = MqttClientImpl; using base::base; + /// @brief Get a shared pointer to this + /// @return shared pointer to this + shared_ptr getptr() + { + return static_pointer_cast(shared_from_this()); + } + /// @brief Get the Mqtt TLS WebSocket Client /// @return pointer to the Mqtt TLS WebSocket Client auto &getClient()