Fix(server,client): keep web terminal alive across heartbeat reconnects

The Go server answered TOKEN_HEARTBEAT with a 33-byte CMD_HEARTBEAT_ACK,
while the Linux and Android clients gate the ACK on 1 + sizeof(HeartbeatACK)
(1025 bytes) and silently drop anything shorter. Their application-level ACK
watchdog therefore never got fed and disconnected the main connection every
60s. Each re-login replaced the whole hub.Device, wiping the screen/shell
sub-conn bindings that ride their own TCP connections, so a web terminal kept
receiving shell output while every keystroke was dropped — the "terminal goes
unresponsive after a while" symptom — and the shell could no longer be torn
down (orphan bash + leaked sockets).

Clients now accept the 32-byte old ACK (Windows already did), and the server
sends the full 1024-byte struct. Hub.Register carries the sub-conn session
state across a re-login, and the disconnect cleanup is pinned to the
connection it belongs to so a stale OnDisconnect cannot tear down a
registration that has already replaced it. A shell sub-conn that can no
longer be bound now tells the viewer instead of leaving the pane waiting.

Verified on aa: RTT restored, zero "Heartbeat ACK timeout" lines, main-conn
port stable; Go build/vet/test pass. Server deploy to the Go host is still
pending, so un-upgraded clients will keep reconnecting until then.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 11:03:57 +01:00
1 parent 4aedca8ee0
commit 81f0d6c031
8 files changed
+412 -46

No files matched your search

+13 -5
View File
@@ -710,14 +710,22 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
Mprintf("** Screen preview thread failed: %s ***\n", e.what());
}
} else if (szBuffer[0] == CMD_HEARTBEAT_ACK) {
if (ulLength >= 1 + sizeof(HeartbeatACK)) {
HeartbeatACK* ack = (HeartbeatACK*)(szBuffer + 1);
// 必须同时认短包:完整结构是 1024 字节,但老服务端 / Go 服务端只回
// HeartbeatACK_OldSize(32)。长度不足就当「没收到」会让下面的 ACK 看门狗
// 每 60s 断开重连一次(表现为链路一直在抖、主连接端口不断变化)。
// Windows 端同样按 min(ulLength, 32) 兼容,见 KernelManager.cpp:2413。
if (ulLength >= 1 + HeartbeatACK_OldSize) {
HeartbeatACK ack = {}; // 短包时缺的字段保持 0(Authorization / ProcessingMs 等)
const size_t copyLen = (ulLength - 1 >= sizeof(HeartbeatACK))
? sizeof(HeartbeatACK)
: (size_t)(ulLength - 1);
memcpy(&ack, szBuffer + 1, copyLen);
uint64_t now = GetUnixMs();
g_lastHeartbeatAckMs.store(now, std::memory_order_relaxed); // 喂应用层 ACK 看门狗
int64_t total_rtt_ms = (int64_t)now - (int64_t)ack->Time;
int64_t total_rtt_ms = (int64_t)now - (int64_t)ack.Time;
int64_t rtt_ms = total_rtt_ms;
if (ack->ProcessingMs > 0 && (int64_t)ack->ProcessingMs < total_rtt_ms)
rtt_ms = total_rtt_ms - (int64_t)ack->ProcessingMs;
if (ack.ProcessingMs > 0 && (int64_t)ack.ProcessingMs < total_rtt_ms)
rtt_ms = total_rtt_ms - (int64_t)ack.ProcessingMs;
g_rttEstimator.update_from_sample((double)rtt_ms);
// 心跳节奏太密日志会刷屏;最多 60s 一行
static time_t lastAckLog = 0;