|
|
@@ -87,24 +87,43 @@ void CMqttClientWorker::Run()
|
|
|
try
|
|
|
{
|
|
|
LOG_INFO("Disconnecting from the MQTT server...");
|
|
|
+
|
|
|
auto client = m_async_client;
|
|
|
- if (client)
|
|
|
+ if (client && client->is_connected())
|
|
|
{
|
|
|
auto tok = client->disconnect();
|
|
|
+
|
|
|
if (!tok->wait_for(std::chrono::seconds(2)))
|
|
|
{
|
|
|
LOG_INFO("MQTT disconnect timeout, wait for pending callbacks to finish.");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- LOG_INFO("OK...");
|
|
|
+ LOG_INFO("MQTT disconnected.");
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("MQTT client is already disconnected.");
|
|
|
+ }
|
|
|
}
|
|
|
catch (const mqtt::exception& exc)
|
|
|
{
|
|
|
- LOG_INFO(("disconnect error, exc:" + exc.get_message()).c_str());
|
|
|
- CLewaimaiLog::OutputDebugMessage(("disconnect error, exc:" + exc.get_message()).c_str());
|
|
|
+ // is_connected() 与 disconnect() 之间连接仍可能被远端断开。
|
|
|
+ if (exc.get_return_code() == MQTTASYNC_DISCONNECTED)
|
|
|
+ {
|
|
|
+ LOG_INFO("MQTT client was already disconnected.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ const std::string msg =
|
|
|
+ "MQTT disconnect error [" +
|
|
|
+ std::to_string(exc.get_return_code()) +
|
|
|
+ "]: " + exc.get_message();
|
|
|
+
|
|
|
+ LOG_INFO(msg.c_str());
|
|
|
+ CLewaimaiLog::OutputDebugMessage(msg.c_str());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//不要 delete,改成释放智能指针
|