first commit
This commit is contained in:
parent
a66d6eb7c8
commit
a7b821a6f3
33
src/main.cpp
33
src/main.cpp
@ -9,15 +9,34 @@
|
|||||||
|
|
||||||
std::atomic<bool> g_running(true);
|
std::atomic<bool> g_running(true);
|
||||||
|
|
||||||
void signalHandler(int)
|
void signalHandler(int signum)
|
||||||
{
|
{
|
||||||
|
static bool already_called = false;
|
||||||
|
if (already_called)
|
||||||
|
return;
|
||||||
|
already_called = true;
|
||||||
|
|
||||||
|
LOG_INFO("[MAIN] Received signal " + std::to_string(signum) + ", shutting down...");
|
||||||
g_running = false;
|
g_running = false;
|
||||||
RTSPManager::stop(); // 停止 RTSP loop
|
RTSPManager::stop(); // 停止 RTSP loop
|
||||||
|
|
||||||
|
// 设置超时,防止无限等待
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
|
||||||
|
// 强制退出如果仍然卡住
|
||||||
|
if (signum == SIGINT)
|
||||||
|
{
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
signal(SIGINT, signalHandler);
|
struct sigaction sigIntHandler;
|
||||||
|
sigIntHandler.sa_handler = signalHandler;
|
||||||
|
sigemptyset(&sigIntHandler.sa_mask);
|
||||||
|
sigIntHandler.sa_flags = 0;
|
||||||
|
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
// 初始化日志文件
|
// 初始化日志文件
|
||||||
@ -45,11 +64,19 @@ int main()
|
|||||||
while (g_running)
|
while (g_running)
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
|
|
||||||
// 等待退出信号
|
// 在main函数等待线程结束处添加日志
|
||||||
if (rtsp_thread.joinable())
|
if (rtsp_thread.joinable())
|
||||||
|
{
|
||||||
|
LOG_INFO("[MAIN] Waiting for RTSP thread to finish...");
|
||||||
rtsp_thread.join();
|
rtsp_thread.join();
|
||||||
|
LOG_INFO("[MAIN] RTSP thread finished");
|
||||||
|
}
|
||||||
if (mqtt_thread.joinable())
|
if (mqtt_thread.joinable())
|
||||||
|
{
|
||||||
|
LOG_INFO("[MAIN] Waiting for MQTT thread to finish...");
|
||||||
mqtt_thread.join();
|
mqtt_thread.join();
|
||||||
|
LOG_INFO("[MAIN] MQTT thread finished");
|
||||||
|
}
|
||||||
|
|
||||||
LOG_INFO("[MAIN] Program exited cleanly.");
|
LOG_INFO("[MAIN] Program exited cleanly.");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -38,9 +38,6 @@ void MQTTClient::connect()
|
|||||||
.finalize();
|
.finalize();
|
||||||
|
|
||||||
client_->connect(connOpts)->wait(); // 阻塞等待连接
|
client_->connect(connOpts)->wait(); // 阻塞等待连接
|
||||||
connected_ = true;
|
|
||||||
if (on_connect_)
|
|
||||||
on_connect_();
|
|
||||||
}
|
}
|
||||||
catch (const mqtt::exception &e)
|
catch (const mqtt::exception &e)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -219,8 +219,12 @@ void mqtt_client_thread_func()
|
|||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
if (!mqtt_client->isConnected())
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
// 更频繁地检查退出标志
|
||||||
|
for (int i = 0; i < 10 && g_running && !mqtt_client->isConnected(); i++)
|
||||||
|
{
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (g_running && mqtt_client->isConnected())
|
while (g_running && mqtt_client->isConnected())
|
||||||
@ -233,12 +237,22 @@ void mqtt_client_thread_func()
|
|||||||
{
|
{
|
||||||
LOG_ERROR("[MQTT] Heartbeat error: " + std::string(e.what()));
|
LOG_ERROR("[MQTT] Heartbeat error: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(heartbeat_interval);
|
// 将长睡眠拆分为多个短睡眠,以便更频繁检查退出标志
|
||||||
|
auto sleep_time = heartbeat_interval;
|
||||||
|
while (sleep_time.count() > 0 && g_running && mqtt_client->isConnected())
|
||||||
|
{
|
||||||
|
auto chunk = std::min(sleep_time, std::chrono::milliseconds(200));
|
||||||
|
std::this_thread::sleep_for(chunk);
|
||||||
|
sleep_time -= chunk;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 需要重启或退出
|
// 需要重启或退出
|
||||||
mqtt_client->disconnect();
|
if (mqtt_client)
|
||||||
mqtt_client.reset();
|
{
|
||||||
|
mqtt_client->disconnect();
|
||||||
|
mqtt_client.reset();
|
||||||
|
}
|
||||||
mqtt_restart_required = false;
|
mqtt_restart_required = false;
|
||||||
}
|
}
|
||||||
LOG_INFO("[MQTT] Client thread exiting.");
|
LOG_INFO("[MQTT] Client thread exiting.");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user