Commit 3fef3f8397fd59317cdc753109cfcdfa87123692

Authored by Peter M. Groen
1 parent 6f2da92f

Active Resubscribe only after a correct state-change

Showing 1 changed file with 19 additions and 18 deletions
src/mqttclient.cpp
@@ -431,29 +431,30 @@ void MqttClient::connectionStatusChanged(const std::string& id, ConnectionStatus @@ -431,29 +431,30 @@ void MqttClient::connectionStatusChanged(const std::string& id, ConnectionStatus
431 } 431 }
432 432
433 auto newState = determineState(connectionStates); 433 auto newState = determineState(connectionStates);
434 - // bool resubscribe = (StateEnum::ConnectionFailure == m_serverState.state() && StateEnum::Good == newState);  
435 - bool resubscribe = ( StateEnum::Good == newState );  
436 - if (resubscribe) 434 + LogDebug( "[MqttClient::connectionStatusChanged]", std::string("Old state : " + std::to_string(static_cast<int>(m_serverState.state())));
  435 + LogDebug( "[MqttClient::connectionStatusChanged]", std::string("New state : " + std::to_string(static_cast<int>(newState)));
  436 + bool resubscribe = (StateEnum::ConnectionFailure == m_serverState.state() && StateEnum::Good == newState);
  437 + // First activate pending subscriptions
437 { 438 {
438 - // First activate pending subscriptions 439 + OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex);
  440 + LogDebug( "[MqttClient::connectionsStatusChanged]", std::string( m_clientId + " - Number of pending subscriptions : " + std::to_string(m_deferredSubscriptions.size() ) ) );
  441 + while( m_deferredSubscriptions.size() > 0 )
439 { 442 {
440 - OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex);  
441 - LogDebug( "[MqttClient::connectionsStatusChanged]", std::string( m_clientId + " - Number of pending subscriptions : " + std::to_string(m_deferredSubscriptions.size() ) ) );  
442 - while( m_deferredSubscriptions.size() > 0 )  
443 - {  
444 - auto subscription = m_deferredSubscriptions.at( 0 );  
445 - this->subscribe( subscription.getTopic(), subscription.getQoS(), subscription.getCallBack() );  
446 - m_deferredSubscriptions.erase( m_deferredSubscriptions.begin() );  
447 - } 443 + auto subscription = m_deferredSubscriptions.at( 0 );
  444 + this->subscribe( subscription.getTopic(), subscription.getQoS(), subscription.getCallBack() );
  445 + m_deferredSubscriptions.erase( m_deferredSubscriptions.begin() );
448 } 446 }
  447 + }
449 448
450 - LogDebug( "[MqttClient::connectionStatusChanged]",  
451 - std::string( m_clientId + " - Resubscribing..." ) );  
452 - {  
453 - OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex);  
454 - m_activeTokens.clear();  
455 - } 449 + LogDebug( "[MqttClient::connectionStatusChanged]",
  450 + std::string( m_clientId + " - Resubscribing..." ) );
  451 + {
  452 + OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex);
  453 + m_activeTokens.clear();
  454 + }
456 455
  456 + if (resubscribe)
  457 + {
457 for (auto* cl : clients) 458 for (auto* cl : clients)
458 { 459 {
459 try 460 try