Fix: Harden the Android client connection lifecycle

Five review findings from the screen-preview work, all in the same
connection plumbing.

The main client handle is now a shared_ptr taken through GetMainClient().
ScreenPreviewThread, ActivityReportThread and sendClientMsg run detached
and can outlive ConnectionThread's teardown, so a raw pointer snapshot
could dereference freed memory; the copy they hold now keeps the client
alive until the send is done. sendClientMsg also uses one handle for
both the connected check and the send instead of reading the global
twice.

The reconnect loop now checks its connection generation in every
condition, so a thread that was asked to stop can no longer reconnect
after a quick nativeStop -> nativeInit reset g_bExit and g_running into
"running again", which produced two main connections with the same
client ID.

ActivityReportThread gets the same try/catch and RAII in-flight counter
as the preview thread: an exception used to call std::terminate, and one
failure left the counter above zero, silently dropping every later
get_activity_history.

nativeStop now stops the activity sampler, and Stop() clears m_started
so a service restart can start it again. Sub-connections (remote screen
and client log) now inherit g_SETTINGS.protoType like the main
connection; without it they silently dialed plain TCP even when WS/WSS
was configured.

Verified on the emulator against a local debug server: get_screenshot,
get_activity_history and get_client_log all still answer, and the log
sub-connection completes the TLS handshake.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 20:59:10 +01:00
1 parent f29150d20e
commit ac43ad92ef
2 files changed
+79 -34

No files matched your search

+3
View File
@@ -486,6 +486,9 @@ public:
m_running = false;
if (m_thread.joinable())
m_thread.join();
// 允许再次 Start:Android 的 nativeStop→nativeInit(服务重启)会走这条路;
// 桌面端只在进程退出时 Stop 一次,多这一步无害。
m_started.store(false);
}
// 最新在前;进行中的窗口(已驻留达阈值)排在最前