diff --git a/Sources/MWBCore/Sequencing.swift b/Sources/MWBCore/Sequencing.swift new file mode 100644 index 0000000..b216308 --- /dev/null +++ b/Sources/MWBCore/Sequencing.swift @@ -0,0 +1,83 @@ +// 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. Every +/// `Session` in the app shares one instance; it is not `Sendable`, so the app +/// owns it on the one queue that owns the sessions. +public final class PacketSequencer { + 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 { + value = value &+ 1 + return value + } + + /// The last id handed out, for the app to persist as `PackageID`. + public var current: Int32 { 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 + } + } +}