first commit

This commit is contained in:
cxh 2025-09-08 15:45:22 +08:00
parent 2b7b79d5bc
commit 649b6cf0a2
3 changed files with 15 additions and 15 deletions

View File

@ -46,5 +46,6 @@ int main()
rtsp_thread.join(); rtsp_thread.join();
mqtt_thread.join(); mqtt_thread.join();
LOG_INFO("[MAIN] Program exited cleanly.");
return 0; return 0;
} }

View File

@ -4,7 +4,7 @@
std::shared_ptr<MQTTClient> mqtt_client; std::shared_ptr<MQTTClient> mqtt_client;
std::atomic<bool> mqtt_restart_required{false}; std::atomic<bool> mqtt_restart_required{false};
static uint16_t broadcast_sequence = 0; extern std::atomic<bool> g_running;
// MQTT 回调定义 // MQTT 回调定义
static void on_mqtt_connected() static void on_mqtt_connected()
@ -36,11 +36,11 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
} }
void mqtt_client_thread_func() void mqtt_client_thread_func()
{
while (true)
{ {
const auto &cfg = g_app_config.mqtt; const auto &cfg = g_app_config.mqtt;
while (g_running)
{
mqtt_client = std::make_unique<MQTTClient>(cfg); mqtt_client = std::make_unique<MQTTClient>(cfg);
mqtt_client->setConnectCallback(on_mqtt_connected); mqtt_client->setConnectCallback(on_mqtt_connected);
mqtt_client->setDisconnectCallback(on_mqtt_disconnected); mqtt_client->setDisconnectCallback(on_mqtt_disconnected);
@ -48,16 +48,15 @@ void mqtt_client_thread_func()
mqtt_client->connect(); mqtt_client->connect();
// 主线程监听重启信号 while (!mqtt_restart_required && g_running)
while (!mqtt_restart_required)
{ {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
// 需要重启 // 需要重启或退出
LOG_INFO("[MQTT] Restarting client..."); mqtt_client->disconnect();
mqtt_client->disconnect(); // 可加锁
mqtt_client.reset(); mqtt_client.reset();
mqtt_restart_required = false; mqtt_restart_required = false;
} }
LOG_INFO("[MQTT] Client thread exiting.");
} }

View File

@ -60,7 +60,12 @@ void RTSPManager::stop()
{ {
if (loop) if (loop)
{ {
g_main_loop_quit(loop); // 退出 GMainLoop // 在 loop 所在线程安全退出
g_main_context_invoke(nullptr, [](gpointer data) -> gboolean
{
GMainLoop *loop = static_cast<GMainLoop *>(data);
g_main_loop_quit(loop);
return G_SOURCE_REMOVE; }, loop);
} }
if (server) if (server)
@ -68,11 +73,6 @@ void RTSPManager::stop()
g_object_unref(server); g_object_unref(server);
server = nullptr; server = nullptr;
} }
if (loop)
{
g_main_loop_unref(loop);
loop = nullptr;
}
LOG_INFO("[RTSP] Server stopped."); LOG_INFO("[RTSP] Server stopped.");
} }