namespace SVSim.BattleNode.Reliability;
///
/// Per-session inbound-emit ledger. Dedupes the client's pubSeq so we never dispatch
/// a retransmitted emit twice; ack-echo (via SIO callback) is the caller's job.
///
///
/// State is bounded: the ledger keeps the most recent
/// pubSeqs in an LRU ring. Seqs below HighWaterMark - WindowSize are
/// treated as stale-below-window and rejected without recording — this is what
/// prevents window eviction from re-admitting an old seq as novel. The pubSeq is
/// client-assigned monotonically; the bound is sized well above the realistic
/// Socket.IO retransmit horizon, so legitimate retransmits always fall inside.
///
public sealed class InboundTracker
{
/// Sliding-window size. Anything below HighWaterMark - WindowSize is dropped.
public const int WindowSize = 256;
private readonly HashSet _seen = new(WindowSize);
private readonly Queue _order = new(WindowSize);
/// Highest pubSeq observed so far. Reported via Gungnir for diagnostics.
public long HighWaterMark { get; private set; }
/// Record an incoming pubSeq. Returns true if the caller should dispatch the envelope, false on duplicate or stale-below-window.
public bool Observe(long pubSeq)
{
// Stale-below-window guard. Required AFTER HighWaterMark is past the window,
// otherwise an evicted ring entry would re-admit as novel.
if (HighWaterMark > 0 && pubSeq <= HighWaterMark - WindowSize)
return false;
if (pubSeq > HighWaterMark)
{
HighWaterMark = pubSeq;
Record(pubSeq);
return true;
}
if (_seen.Contains(pubSeq))
return false;
Record(pubSeq);
return true;
}
private void Record(long pubSeq)
{
if (_order.Count >= WindowSize)
{
var evicted = _order.Dequeue();
_seen.Remove(evicted);
}
_order.Enqueue(pubSeq);
_seen.Add(pubSeq);
}
}