张洋 vor 3 Wochen
Ursprung
Commit
a7cb580f53

+ 5 - 0
zhipuzi_pos_windows/tool/CAppEnv.cpp

@@ -104,6 +104,8 @@ void CAppEnv::DelWorkerNum()
 
 void CAppEnv::AsyncInit()
 {
+	this->AddWorkerNum();
+
 	//初始化副屏
 	CShuangpingTool::GetInstance()->Init();
 
@@ -115,4 +117,7 @@ void CAppEnv::AsyncInit()
 
 	//初始化特征数据库
 	SQLiteVecManager::GetInstance()->initializeDatabase(1280);
+
+	//全部初始化完成,才允许退出程序,不然的话程序容易崩
+	this->DelWorkerNum();
 }

+ 23 - 4
zhipuzi_pos_windows/worker/CMqttClientWorker.cpp

@@ -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,改成释放智能指针

+ 2 - 4
zhipuzi_pos_windows/worker/CMqttClientWorker.h

@@ -36,10 +36,8 @@ private:
     // Options to use if we need to reconnect
     mqtt::connect_options m_connOpts;
 
-    bool m_is_mqtt_connected = false;
-
-    //工作状态
-    bool m_is_running = false;
+	std::atomic_bool m_is_mqtt_connected{ false };
+	std::atomic_bool m_is_running{ false };
 
 	//正在退出/关闭,禁止重连和回调继续处理
 	std::atomic_bool m_is_closing{ false };