From 3fef3f8397fd59317cdc753109cfcdfa87123692 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Wed, 17 Jan 2024 10:21:06 +0100 Subject: [PATCH] Active Resubscribe only after a correct state-change --- src/mqttclient.cpp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 42eafb8..2d1633d 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -431,29 +431,30 @@ void MqttClient::connectionStatusChanged(const std::string& id, ConnectionStatus } auto newState = determineState(connectionStates); - // bool resubscribe = (StateEnum::ConnectionFailure == m_serverState.state() && StateEnum::Good == newState); - bool resubscribe = ( StateEnum::Good == newState ); - if (resubscribe) + LogDebug( "[MqttClient::connectionStatusChanged]", std::string("Old state : " + std::to_string(static_cast(m_serverState.state()))); + LogDebug( "[MqttClient::connectionStatusChanged]", std::string("New state : " + std::to_string(static_cast(newState))); + bool resubscribe = (StateEnum::ConnectionFailure == m_serverState.state() && StateEnum::Good == newState); + // First activate pending subscriptions { - // First activate pending subscriptions + OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex); + LogDebug( "[MqttClient::connectionsStatusChanged]", std::string( m_clientId + " - Number of pending subscriptions : " + std::to_string(m_deferredSubscriptions.size() ) ) ); + while( m_deferredSubscriptions.size() > 0 ) { - OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex); - LogDebug( "[MqttClient::connectionsStatusChanged]", std::string( m_clientId + " - Number of pending subscriptions : " + std::to_string(m_deferredSubscriptions.size() ) ) ); - while( m_deferredSubscriptions.size() > 0 ) - { - auto subscription = m_deferredSubscriptions.at( 0 ); - this->subscribe( subscription.getTopic(), subscription.getQoS(), subscription.getCallBack() ); - m_deferredSubscriptions.erase( m_deferredSubscriptions.begin() ); - } + auto subscription = m_deferredSubscriptions.at( 0 ); + this->subscribe( subscription.getTopic(), subscription.getQoS(), subscription.getCallBack() ); + m_deferredSubscriptions.erase( m_deferredSubscriptions.begin() ); } + } - LogDebug( "[MqttClient::connectionStatusChanged]", - std::string( m_clientId + " - Resubscribing..." ) ); - { - OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); - m_activeTokens.clear(); - } + LogDebug( "[MqttClient::connectionStatusChanged]", + std::string( m_clientId + " - Resubscribing..." ) ); + { + OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); + m_activeTokens.clear(); + } + if (resubscribe) + { for (auto* cl : clients) { try -- libgit2 0.21.4