GameCult.Networking: resend the capped frame's answer until acknowledged, then end the session
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 8m47s
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Successful in 1m13s

The schema server answered the 8th undecodable frame and ended the peer's session in the same
call; ending clears the session's pending reliable packets, so a lost datagram was never
resent. The session now stays until the peer has acknowledged every answer (a lost one is
resent by PollResends) or a 2 s grace runs out, serves nothing after the capped frame
meanwhile, and then sends the goodbye.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-08 11:10:48 +00:00
commit 0b1a468ddd
3 changed files with 149 additions and 2 deletions

View file

@ -126,6 +126,12 @@ namespace GameCult.Networking
/// </summary>
public const int MaxConsecutiveRefusedFrames = 8;
/// <summary>
/// How long a session whose capped frame was answered waits for the peer to acknowledge the answer
/// before it ends anyway.
/// </summary>
internal const long CappedAnswerGraceMs = 2000;
/// <summary>
/// Gets or sets the logger used by the server. R-AM: the dispatch backstop in DispatchAsync
/// logs every untyped fault through this instead of swallowing it silently.
@ -310,6 +316,9 @@ namespace GameCult.Networking
{
if (!string.Equals(delivered.Frame.ChannelId, "schema", StringComparison.Ordinal))
return false;
// The capped frame was answered: nothing after it is served while the answer is delivered.
if (delivered.Peer.EndingReason != null)
return false;
// A peer's message that cannot be decoded (malformed bytes, an unknown schemaVersion) or
// handled ends that peer's session, never the poll: the drain goes on serving every other
@ -337,8 +346,10 @@ namespace GameCult.Networking
Logger.LogError($"CultNet RUDP schema server could not decode a peer's message ({ex.GetType().Name}); it was refused and that peer's session kept.");
return false;
}
var ended = _transport.EndPeerSession(delivered.Peer, CultNetRudpSession.RefusedPacketReason);
Logger.LogError($"CultNet RUDP schema server could not decode a peer's message ({ex.GetType().Name}); {SessionOutcome(ended)}");
// The answer was sent reliably: the session ends once the peer has acknowledged it (a lost
// datagram is resent first), or after the grace.
_transport.EndPeerSessionWhenAnswered(delivered.Peer, CultNetRudpSession.RefusedPacketReason, CappedAnswerGraceMs);
Logger.LogError($"CultNet RUDP schema server could not decode a peer's message ({ex.GetType().Name}); that peer's session ends once its answer is acknowledged.");
return false;
}
_undecodableFramesInARow.GetOrCreateValue(delivered.Peer).Value = 0;

View file

@ -2759,6 +2759,11 @@ namespace GameCult.Networking
// under SessionGate by whichever thread's send found the failure; the polling thread reads
// it when it retires the peer.
internal byte[]? UnsendableReason { get; set; }
// Set once the peer's capped frame has been answered: the session ends with this reason when the
// peer has acknowledged every answer, or at the deadline. Under SessionGate.
internal byte[]? EndingReason { get; set; }
internal long EndingDeadlineMs { get; set; }
}
/// <summary>
@ -2928,6 +2933,7 @@ namespace GameCult.Networking
public bool TryReceiveOnce(out CultNetRudpSocketServerFrame? delivered)
{
RetireUnsendablePeers();
EndAnsweredPeers();
if (_deliveredFrames.Count > 0)
{
delivered = _deliveredFrames.Dequeue();
@ -3146,6 +3152,7 @@ namespace GameCult.Networking
public void PollResends()
{
RetireUnsendablePeers();
EndAnsweredPeers();
if (_socket.Poll(0, SelectMode.SelectRead))
return;
foreach (var peer in _peers.Values.ToArray())
@ -3244,6 +3251,44 @@ namespace GameCult.Networking
return true;
}
// Polling thread only. The application answered a peer's capped frame: the answer is reliable, so a
// lost datagram is resent, and the session ends with `reason` once the peer has acknowledged every
// answer or `graceMs` have passed (EndAnsweredPeers). Frames of the peer still queued are not delivered.
internal void EndPeerSessionWhenAnswered(CultNetRudpSocketServerPeer peer, byte[] reason, long graceMs)
{
if (peer == null) throw new ArgumentNullException(nameof(peer));
if (reason == null) throw new ArgumentNullException(nameof(reason));
lock (peer.SessionGate)
{
if (peer.Session.Ended || peer.EndingReason != null)
return;
peer.EndingReason = reason;
peer.EndingDeadlineMs = NowMs() + graceMs;
}
var kept = _deliveredFrames.Where(frame => !ReferenceEquals(frame.Peer, peer)).ToArray();
_deliveredFrames.Clear();
foreach (var frame in kept)
_deliveredFrames.Enqueue(frame);
}
// Polling thread only.
private void EndAnsweredPeers()
{
foreach (var peer in _peers.Values.ToArray())
{
byte[]? reason;
lock (peer.SessionGate)
{
reason = peer.EndingReason != null
&& (peer.Session.OutstandingReliablePacketCount == 0 || NowMs() >= peer.EndingDeadlineMs)
? peer.EndingReason
: null;
}
if (reason != null)
EndPeerSession(peer, reason);
}
}
// Polling thread only. A peer a new Connect already replaced was reported then, and its key now
// names the peer that replaced it.
private void RetireUnsendablePeers()