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