Files
SVSimServer/SVSim.BattleNode/Reliability/OutboundSequencer.cs
gamer147 10d9f74d05 feat(battle-node): add OutboundSequencer.Clear() for terminate cascade
Audit Md11 (part 2 of 2). Adds an explicit Clear() so BattleSession can
release the archive at battle-end instead of waiting for the participant to
be GC'd. _next is intentionally NOT reset — a post-Clear emit is a bug per
the design, but the seq stream must stay monotonic if it does happen.

Tests cover empty archive after Clear, _next preservation across Clear,
and Clear-on-empty no-op. The BattleSession integration that calls Clear
lands in the next commit.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 13:07:00 -04:00

37 lines
1.4 KiB
C#

using SVSim.BattleNode.Protocol;
namespace SVSim.BattleNode.Reliability;
/// <summary>
/// Per-session outbound ledger. Assigns monotonic playSeq to ordered pushes and archives
/// them for future Resume retransmit (v2). No-stock control pushes (BattleFinish/JudgeResult/Resume)
/// are wrapped with no playSeq and skip the archive.
/// </summary>
public sealed class OutboundSequencer
{
private long _next = 1;
private readonly Dictionary<long, MsgEnvelope> _archive = new();
public IReadOnlyDictionary<long, MsgEnvelope> Archive => _archive;
public MsgEnvelope AssignAndArchive(MsgEnvelope envelope)
{
var seq = _next++;
var stamped = envelope with { PlaySeq = seq };
_archive[seq] = stamped;
return stamped;
}
public MsgEnvelope WrapNoStock(MsgEnvelope envelope) =>
envelope with { PlaySeq = null };
/// <summary>
/// Drop all archived envelopes. Called from BattleSession's terminate cascade so
/// the archive — the heavy state — is released the moment the battle ends, rather
/// than waiting for the participant to be GC'd. <c>_next</c> is left untouched:
/// a participant emitting after Clear is a bug, not a recovery case, but the seq
/// stream stays monotonic so a stray emit doesn't silently re-use a playSeq value.
/// </summary>
public void Clear() => _archive.Clear();
}