From 2c0c99a53b977cc292da46250e3492a9a0b59526 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Wed, 6 Jul 2022 15:49:34 +0200 Subject: [PATCH] Fix connect Callbacks --- include/clientpaho.h | 8 +------- src/clientpaho.cpp | 36 ++++++++++-------------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/include/clientpaho.h b/include/clientpaho.h index bfe6e2b..049c885 100644 --- a/include/clientpaho.h +++ b/include/clientpaho.h @@ -204,12 +204,6 @@ private: void callbackEventHandler(); /** - * @brief Callback method that is called when a first connect succeeds. - * @param reason Som extra information if there is any. - */ - void onFirstConnectOnInstance(const std::string &reason); - - /** * @brief Callback method that is called when a reconnect succeeds. * @param cause The cause of the original disconnect. */ @@ -221,7 +215,7 @@ private: * The connection status is set to Connected. * @param response A success response with connection data. */ - void onConnectSuccessOnInstance(const MqttSuccess& response); + void onConnectSuccessOnInstance(); /** * @brief Callback that is called when a connect call fails after being sent to the endpoint. diff --git a/src/clientpaho.cpp b/src/clientpaho.cpp index 155a32c..24573da 100644 --- a/src/clientpaho.cpp +++ b/src/clientpaho.cpp @@ -203,7 +203,7 @@ std::int32_t ClientPaho::connect( bool wait, const mqtt_LWT &lwt ) MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; conn_opts.keepAliveInterval = 5; conn_opts.cleansession = 1; - conn_opts.onSuccess = &ClientPaho::onConnectSuccess; + conn_opts.onSuccess = nullptr; conn_opts.onFailure = &ClientPaho::onConnectFailure; conn_opts.context = this; conn_opts.automaticReconnect = 1; @@ -764,23 +764,6 @@ void ClientPaho::callbackEventHandler() } // ("ClientPaho", "%1 - leaving callback event handler", m_clientId); } - -void ClientPaho::onFirstConnectOnInstance(const std::string &reason) -{ - (void)reason; - { - OSDEV_COMPONENTS_LOCKGUARD(m_mutex); - // Remove the connect callback that is used in reconnect scenarios. - auto rc = MQTTAsync_setConnected(m_client, this, nullptr ); - if (MQTTASYNC_SUCCESS != rc) - { - LogError( "[ClientPaho]", std::string( "onFirstConnectOnInstance " + m_clientId + " - registering the connected callback failed with code : " + pahoAsyncErrorCodeToString(rc) ) ); - } - } - - setConnectionStatus(ConnectionStatus::Connected); -} - void ClientPaho::onConnectOnInstance(const std::string& cause) { (void)cause; @@ -795,11 +778,8 @@ void ClientPaho::onConnectOnInstance(const std::string& cause) setConnectionStatus(ConnectionStatus::Connected); } -void ClientPaho::onConnectSuccessOnInstance(const MqttSuccess& response) +void ClientPaho::onConnectSuccessOnInstance() { - auto connectData = response.connectionData(); - LogDebug( "[ClientPaho]", std::string( "onConnectSuccessOnInstance " + m_clientId + " - connected to endpoint " + connectData.serverUri() + - " (mqtt version " + std::to_string( connectData.mqttVersion() ) + ", session present " + ( connectData.sessionPresent() ? "TRUE" : "FALSE" ) + " )" ) ); { OSDEV_COMPONENTS_LOCKGUARD(m_mutex); // Register the connect callback that is used in reconnect scenarios. @@ -808,12 +788,14 @@ void ClientPaho::onConnectSuccessOnInstance(const MqttSuccess& response) { LogError( "[ClientPaho]", std::string( "onConnectSuccesOnInstance " + m_clientId + " - registering the connected callback failed with code : " + pahoAsyncErrorCodeToString(rc) ) ); } + // For MQTTV5 //rc = MQTTAsync_setDisconnected(m_client, this, &ClientPaho::onDisconnect); //if (MQTTASYNC_SUCCESS != rc) { // // ("ClientPaho", "onConnectSuccessOnInstance %1 - registering the disconnected callback failed with code %2", m_clientId, pahoAsyncErrorCodeToString(rc)); //} // ("ClientPaho", "onConnectSuccessOnInstance %1 - pending operations : %2, removing operation -100", m_clientId, m_pendingOperations); + m_operationResult[-100] = true; m_pendingOperations.erase(-100); } @@ -1128,7 +1110,7 @@ void ClientPaho::onFirstConnect(void* context, char* cause) { auto *cl = reinterpret_cast(context); std::string reason(nullptr == cause ? "Unknown cause" : cause); - cl->pushIncomingEvent([cl, reason]() { cl->onFirstConnectOnInstance(reason); }); + cl->pushIncomingEvent([cl, reason]() { cl->onConnectSuccessOnInstance(); }); } } @@ -1146,7 +1128,7 @@ void ClientPaho::onConnect(void* context, char* cause) // static void ClientPaho::onConnectSuccess(void* context, MQTTAsync_successData* response) { - LogInfo( "[ClientPaho::onConnectSuccess]", "onConnectSucces triggered.." ); + LogInfo( "[ClientPaho::onConnectSuccess]", "onConnectSuccess triggered.." ); if (context) { auto* cl = reinterpret_cast(context); @@ -1154,15 +1136,17 @@ void ClientPaho::onConnectSuccess(void* context, MQTTAsync_successData* response { // connect should always have a valid response struct. LogError( "[ClientPaho]", "onConnectSuccess - no response data"); + return; } - MqttSuccess resp(response->token, ConnectionData(response->alt.connect.serverURI, response->alt.connect.MQTTVersion, response->alt.connect.sessionPresent)); - cl->pushIncomingEvent([cl, resp]() { cl->onConnectSuccessOnInstance(resp); }); + // MqttSuccess resp(response->token, ConnectionData(response->alt.connect.serverURI, response->alt.connect.MQTTVersion, response->alt.connect.sessionPresent)); + cl->pushIncomingEvent([cl]() { cl->onConnectSuccessOnInstance(); }); } } // static void ClientPaho::onConnectFailure(void* context, MQTTAsync_failureData* response) { + LogDebug("[ClientPaho::onConnectFailure]", std::string( "Connection Failure?" )); if (context) { auto* cl = reinterpret_cast(context); -- libgit2 0.21.4