// 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 } } }