diff --git a/Sources/MWBNet/ControlChannel.swift b/Sources/MWBNet/ControlChannel.swift index e05c8b5..8d12163 100644 --- a/Sources/MWBNet/ControlChannel.swift +++ b/Sources/MWBNet/ControlChannel.swift @@ -56,7 +56,9 @@ public final class ControlChannel: @unchecked Sendable { private var failures: [String: Int] = [:] private var blocked: Set = [] private var dialDeadline: TimeInterval = 0 - private var lastUserActivity: TimeInterval = 0 + private var inputEventCount: UInt64 = 0 + private var beatInputEventCount: UInt64 = 0 + private var lastBeatAt: TimeInterval = 0 private var controlledPeerID: UInt32? private var lastPathStatus: NWPath.Status? private var connectionCount = 0 @@ -193,9 +195,11 @@ public final class ControlChannel: @unchecked Sendable { } /// Tells the channel the user just did something, which is what turns the - /// next `Heartbeat` into an `Awake` (`Common.cs:545-552`). + /// next `Heartbeat` into an `Awake` (`Common.cs:545-558`). This is our + /// `Event.RealInputEventCount++` (`InputHook.cs:223, 345`): the count is + /// what matters, not when it happened. public func noteUserActivity() { - queue.async { [self] in lastUserActivity = monotonicNow() } + queue.async { [self] in inputEventCount &+= 1 } } /// Names the peer currently receiving our input, or `nil` when control is @@ -443,11 +447,22 @@ public final class ControlChannel: @unchecked Sendable { source.resume() } + /// `Awake` or `Heartbeat` is decided by whether the real-input counter moved + /// since the *previous* heartbeat, not by a window around `now`: + /// `SendAwakeBeat` sends `Awake` when `lastRealInputEventCount != + /// Event.RealInputEventCount` and records the counter again on its way out + /// (`Common.cs:545-558`). The two counters here are that pair, and the beat + /// they are recorded on is the one global heartbeat Windows's 100 ms timer + /// runs for every socket at once (`frmScreen.cs:565-575`). private func tick() { guard running else { return } let now = monotonicNow() let interval = config.blockScreenSaver ? Session.fastHeartbeat : Session.slowHeartbeat - let userActive = lastUserActivity > 0 && now - lastUserActivity <= interval + let userActive = inputEventCount != beatInputEventCount + if now - lastBeatAt >= interval { + lastBeatAt = now + beatInputEventCount = inputEventCount + } for connection in connections { connection.isControlled = connection.peerID != nil && connection.peerID == controlledPeerID connection.tick(now: now, userActive: userActive) diff --git a/docs/features/control-channel/README.md b/docs/features/control-channel/README.md index 22fa4cb..cc2eaf5 100644 --- a/docs/features/control-channel/README.md +++ b/docs/features/control-channel/README.md @@ -89,7 +89,11 @@ No packet is interpreted here. budget expired is actually dialled. - **Ticks**: the channel runs one 100 ms `DispatchSourceTimer` that calls `Session.tick(now:userActive:)` on every connection (the app - reports `userActive` via `noteUserActivity()`), drains outboxes and + reports input via `noteUserActivity()`, which is our + `Event.RealInputEventCount++`, `InputHook.cs:223, 345`; `userActive` is + then "the counter moved since the previous heartbeat", the test + `SendAwakeBeat` makes and re-records every time it runs, + `Common.cs:545-558` — not a sliding window around `now`), drains outboxes and events, and detects the C2 "30 s silence while we are sending input" stall: if the connection the sink marked as `controlled` receives nothing for 30 s while packets are being forwarded to it, close it. diff --git a/docs/protocol.md b/docs/protocol.md index 654db23..b6e2ea7 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -215,10 +215,16 @@ Status colours are in `doc/devdocs/modules/mousewithoutborders.md`. ### 3.3 Liveness -- Heartbeat from the 100 ms UI timer (`frmScreen.cs:285-288, 566-575`): +- Heartbeat from the 100 ms UI timer (`frmScreen.cs:285-288, 565-575`): every 60 s when `BlockScreenSaver` is on, else every 300 s. - `Awake (21)` if the user was recently active, else `Heartbeat (20)` - (`Common.cs:499-513, 662-676`). + `Awake (21)` if real input happened since the previous heartbeat, else + `Heartbeat (20)` — `SendAwakeBeat` compares `lastRealInputEventCount != + Event.RealInputEventCount` and re-records the counter every time it runs + (`Common.cs:542-559`), so this is a since-the-last-beat test, not a + sliding window. The counter is bumped by the input hook + (`Event.cs:51-55`, `InputHook.cs:223, 345`). `Awake` also requires + `BlockScreenSaver`, `IsMyDesktopActive()` and neither the logon nor the + screen-saver desktop (`Common.cs:547-548`). - Key propagation chain during pairing: `Heartbeat_ex (51)` → receiver adopts key, replies `Heartbeat_ex_l2 (52)` → `Heartbeat_ex_l3 (53)` (`Receiver.cs:210-243`). *Port decision: not implemented in M2; the