Files
SimpleRemoter/server/go/cmd/heartbeat_ack_test.go
T
yuanyuanxianganddeepseek-flash 81f0d6c031 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
2026-11-12 11:03:57 +01:00

36 lines
1.4 KiB
Go

package main
import (
"encoding/binary"
"testing"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol"
)
// The Linux and Android clients only accept a heartbeat ACK when
// `ulLength >= 1 + sizeof(HeartbeatACK)` == 1025 bytes and silently ignore a
// shorter packet, so the 60s ACK watchdog never gets fed and the client
// reconnects forever. A 33-byte reply here did exactly that on the Go server
// (linux/main.cpp:2107 "Heartbeat ACK timeout: 60007 ms ... reconnecting",
// once a minute, with the terminal session dying on every reconnect).
func TestBuildHeartbeatACKIsFullSize(t *testing.T) {
const hbTime = uint64(0x1122334455667788)
resp := buildHeartbeatACK(hbTime, 2)
if want := 1 + 1024; len(resp) != want {
t.Fatalf("ACK length = %d, want %d (shorter packets are ignored by the client)", len(resp), want)
}
if resp[0] != protocol.CommandHeartbeat {
t.Fatalf("cmd byte = %d, want %d", resp[0], protocol.CommandHeartbeat)
}
if got := binary.LittleEndian.Uint64(resp[1+protocol.HeartbeatACKOffTime:]); got != hbTime {
t.Fatalf("echoed Time = %#x, want %#x", got, hbTime)
}
if got := resp[1+protocol.HeartbeatACKOffAuthorized]; got != 2 {
t.Fatalf("Authorized = %d, want 2", got)
}
if got := binary.LittleEndian.Uint32(resp[1+protocol.HeartbeatACKOffProcessing:]); got != 0 {
t.Fatalf("ProcessingMs = %d, want 0 (unknown => plain now-Time RTT)", got)
}
}