Files
lucioandClaude Opus 5 8125cb1ede session-matrix: PacketSequencer is thread-safe (app-engine request 1)
Cross-lane edit, authorised by the lead. Contracts C1 gives the process
one sequencer and two callers: the Engine actor stamps application
packets, each `Session` stamps its own handshake, heartbeat, `Hi` and
`ByeBye` on the channel's queue (`Session.swift:402-403`). `value &+ 1`
with no synchronisation can lose an update and hand two packets the same
`Id`, which the peer's replay ring drops (`Receiver.cs:40-82`). The
counter now lives under an `NSLock` — Foundation only, so MWBCore's
layering holds — and the class is `@unchecked Sendable`, which is the
guarantee Windows gets from `Interlocked.Increment` (`Common.cs:916`).
`ReplayFilter` is unchanged: one queue touches it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-16 03:56:52 -07:00

99 lines
3.8 KiB
Swift

// The two process-wide counters that guard packet identity: the outgoing `Id`
// sequence and the ring of recently seen ids that drops replays.
// See docs/protocol.md §1.4.
//
// Ported from App/Core/Common.cs SkSend (916), App/Core/Package.cs (22) and
// App/Core/Receiver.cs PreProcess (30-82).
import Foundation
/// The outgoing packet `Id` counter, one per process as on Windows.
///
/// Windows keeps a single `Package.PackageID` (`Package.cs:22`) incremented by
/// `Interlocked.Increment` for every broadcast (`Common.cs:916`) and persists it
/// as the `PackageID` setting, so ids keep rising across restarts.
///
/// **Thread-safe, and it has to be.** Contracts C1 gives the process one
/// sequencer and two callers: the app stamps every application packet on the
/// Engine actor, while each `Session` stamps its own handshake, heartbeat, `Hi`
/// and `ByeBye` on the channel's queue (`Session.swift:402-403`). An
/// unsynchronised `value &+ 1` can lose an update there and hand two packets the
/// same `Id`, which the peer's replay ring reads as a duplicate and drops
/// (`Receiver.cs:40-82`). The lock is held for one add, so it is never
/// contended for long. Windows gets the same guarantee from
/// `Interlocked.Increment` (`Common.cs:916`).
public final class PacketSequencer: @unchecked Sendable {
private let lock = NSLock()
private var value: Int32
/// A sequencer resuming from the persisted `PackageID` setting.
public init(start: Int32 = 0) {
value = start
}
/// The next id: pre-incremented like `Interlocked.Increment`
/// (`Common.cs:916`), wrapping on overflow as C#'s unchecked `int` does.
public func next() -> Int32 {
lock.lock()
defer { lock.unlock() }
value = value &+ 1
return value
}
/// The last id handed out, for the app to persist as `PackageID`.
public var current: Int32 {
lock.lock()
defer { lock.unlock() }
return value
}
}
/// The replay window: the last 50 packet ids, in a ring, with the four
/// exemptions Windows makes.
///
/// `Receiver.PreProcess` (`Receiver.cs:40-82`) drops a packet whose id is still
/// in the ring. One instance per process, shared by every `Session`, because
/// the Windows ring is static and a peer's two connections must not deliver the
/// same broadcast twice.
public final class ReplayFilter {
/// Ids remembered (`QUEUE_SIZE = 50`, `Receiver.cs:30`).
public static let windowSize = 50
private var ring: [Int32]
private var index: Int
/// An empty filter. The ring starts zero-filled exactly as the Windows
/// `int[]` does, so a packet with `Id == 0` reads as a duplicate — which is
/// what Windows does too, and why `Id` starts at 1.
public init() {
ring = [Int32](repeating: 0, count: ReplayFilter.windowSize)
index = 0
}
/// Whether this packet has been seen before and must be dropped.
///
/// Types sent by the single-direction `TcpSend` path, and the two clipboard
/// chunk streams whose ids repeat by design, are exempt and always pass
/// (`Receiver.cs:58-61`). A non-exempt packet is recorded as it passes.
public func isDuplicate(_ packet: Packet) -> Bool {
if ReplayFilter.isExempt(packet.type) {
return false
}
let id = packet.id
if ring.contains(id) { // Receiver.cs:66-73
return true
}
index = (index + 1) % ReplayFilter.windowSize // Receiver.cs:76
ring[index] = id
return false
}
/// The four types `PreProcess` never de-duplicates (`Receiver.cs:58-61`).
private static func isExempt(_ type: PackageType) -> Bool {
switch type {
case .clipboardText, .clipboardImage, .handshake, .handshakeAck: return true
default: return false
}
}
}