diff --git a/docs/cultnet-transport-parity.md b/docs/cultnet-transport-parity.md index 6fba4f35..d77924c7 100644 --- a/docs/cultnet-transport-parity.md +++ b/docs/cultnet-transport-parity.md @@ -238,6 +238,11 @@ Current progress: counter and duplicate backoff constants. It still owns LiteNetLib socket mechanics and UI-facing reconnect state; the controller owns retry attempt scheduling. +- C# now also has a poll-driven `CultNetRudpReconnectLoop` for the native RUDP + socket transport. Caller-owned game/service loops report closure, ask + `ReconnectIfDue(nowMs)` inside their own scheduler, and the shared controller + owns retry delay/exhaustion state while the caller-owned factory opens the + next `CultNetRudpSocketTransportConnection`. - TypeScript now has a `CultNetRudpReconnectLoop` that consumes the shared controller while keeping socket construction caller-owned. A closed RUDP transport schedules the next attempt with the portable policy, opens a fresh diff --git a/docs/runtime-parity-scope.md b/docs/runtime-parity-scope.md index 214bd649..33f74f35 100644 --- a/docs/runtime-parity-scope.md +++ b/docs/runtime-parity-scope.md @@ -30,7 +30,7 @@ CultMesh. | Runtime | Role | Claimed parity | Not claimed | | --- | --- | --- | --- | -| C# | Reference implementation and production game/server substrate | CultCache typed documents and SoA, CultNet schema-v0, LiteNetLib production path, shared transport profiles, shared RUDP socket transport, RUDP reconnect policy/controller, LiteNetLib client reconnect-controller adoption, CultMesh node/session/catalog/stream/simulation surfaces | No need to abandon LiteNetLib before the CultNet adapter fully owns production service adoption | +| C# | Reference implementation and production game/server substrate | CultCache typed documents and SoA, CultNet schema-v0, LiteNetLib production path, shared transport profiles, shared RUDP socket transport, RUDP reconnect policy/controller and reconnect-loop helper, LiteNetLib client reconnect-controller adoption, CultMesh node/session/catalog/stream/simulation surfaces | No need to abandon LiteNetLib before the CultNet adapter fully owns production service adoption | | Python | Package runtime, local daemon endpoint, raw interop peer, typed state participant | CultCache/CultNet/CultMesh package surface, schema catalogs, shard catalogs/logs, local TCP CultMesh server, session/prediction/simulation fact helpers, authority leases, streams, RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, benchmark evidence against C# cache paths | Universal C# throughput, full C# daemon/server stack, and RUDP as the only public service pipe | | TypeScript | Node/browser-adjacent projection and interop runtime | CultCache parity tests, CultNet schema-v0 peer, transport profiles, TCP-framed transport port, shared RUDP socket transport, RUDP reconnect policy/controller, RUDP reconnect-loop helper, local CultMesh node/catalog/authority/stream ergonomics | Full C# game session/server stack | | Rust | Low-level cache/transport/runtime substrate | CultCache/CultNet interop lane, transport profiles, TCP-framed port, shared RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, small CultMesh facade for peer cards, authority leases, RUDP endpoint parsing, and peer-card RUDP client construction | Full CultMesh game-session/server facade, stream catalog, or production game-session server | @@ -63,7 +63,7 @@ CultMesh. - Extend RUDP interop evidence through daemon/socket reconnect loops and the wider schema-message matrix. RUDP profiles now advertise the portable reconnect policy, runtime-local tests prove the shared controller across the - targeted runtimes, and the C# LiteNetLib client plus TypeScript/Rust/Python/Kotlin + targeted runtimes, and the C# LiteNetLib client plus C#/TypeScript/Rust/Python/Kotlin RUDP reconnect loops use that controller; broader service adoption remains a separate claim. - Keep Kotlin ergonomic parity moving with Android/JVM-shaped APIs instead of diff --git a/src/GameCult.Networking/CultNetTransport.cs b/src/GameCult.Networking/CultNetTransport.cs index 18fc1122..53acc28b 100644 --- a/src/GameCult.Networking/CultNetTransport.cs +++ b/src/GameCult.Networking/CultNetTransport.cs @@ -1834,6 +1834,139 @@ namespace GameCult.Networking } } + /// + /// Caller-owned reconnect loop for the socket-backed CultNet RUDP transport. + /// + public sealed class CultNetRudpReconnectLoop : IDisposable + { + private readonly Func _createTransport; + private readonly byte[] _connectPayload; + private CultNetRudpSocketTransportConnection? _transport; + private bool _stopped = true; + private bool _disposed; + + /// + /// Initializes a new reconnect loop around caller-owned RUDP transport construction. + /// + public CultNetRudpReconnectLoop( + Func createTransport, + CultNetReconnectPolicy? reconnectPolicy = null, + byte[]? connectPayload = null) + { + _createTransport = createTransport ?? throw new ArgumentNullException(nameof(createTransport)); + _connectPayload = connectPayload?.ToArray() ?? Array.Empty(); + ReconnectController = new CultNetReconnectController(reconnectPolicy); + } + + /// + /// Gets the shared reconnect controller that owns attempt, delay, and exhaustion state. + /// + public CultNetReconnectController ReconnectController { get; } + + /// + /// Gets the current transport, if the loop has one open. + /// + public CultNetRudpSocketTransportConnection? Transport => _transport; + + /// + /// Opens the first transport and sends its connect packet. + /// + public CultNetRudpSocketTransportConnection Start() + { + ThrowIfDisposed(); + _stopped = false; + ReconnectController.Reset(); + return OpenTransport(); + } + + /// + /// Stops reconnecting, disposes the current transport, and clears retry state. + /// + public void Stop() + { + _stopped = true; + DisposeTransport(); + ReconnectController.Reset(); + } + + /// + /// Clears retry state after the caller observes an established connection. + /// + public void MarkConnected() + { + ReconnectController.Reset(); + } + + /// + /// Reports that the current transport closed and records the next retry decision. + /// + public CultNetReconnectDecision? HandleClosed(long nowMs, int jitterMs = 0) + { + ThrowIfDisposed(); + DisposeTransport(); + return _stopped ? null : ReconnectController.RecordFailure(nowMs, jitterMs); + } + + /// + /// Opens a new transport when the shared controller says the next attempt is due. + /// + public bool ReconnectIfDue(long nowMs) + { + ThrowIfDisposed(); + if (_stopped || !ReconnectController.CanAttempt(nowMs)) + { + return false; + } + + OpenTransport(); + return true; + } + + /// + public void Dispose() + { + if (_disposed) + { + return; + } + + _disposed = true; + Stop(); + } + + private CultNetRudpSocketTransportConnection OpenTransport() + { + var next = _createTransport(); + try + { + next.Connect(_connectPayload); + } + catch + { + next.Dispose(); + throw; + } + + DisposeTransport(); + _transport = next; + return next; + } + + private void DisposeTransport() + { + _transport?.Dispose(); + _transport = null; + } + + private void ThrowIfDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(nameof(CultNetRudpReconnectLoop)); + } + } + } + /// /// Transport connection for the current TCP length-prefixed CultNet schema lane. /// diff --git a/src/GameCult.Networking/README.md b/src/GameCult.Networking/README.md index 98e53fa1..59e3bacf 100644 --- a/src/GameCult.Networking/README.md +++ b/src/GameCult.Networking/README.md @@ -366,6 +366,11 @@ The client: - automatically re-verifies using the stored signed session token - exposes reconnect state for UI/operator surfaces +The native RUDP socket path also exposes `CultNetRudpReconnectLoop` for +caller-owned game or service loops. The caller reports closure, calls +`ReconnectIfDue(nowMs)` from its own scheduler, and the shared +`CultNetReconnectController` owns retry attempts, delay, and exhaustion state. + ## Important Constraints - `Server` currently centers on `PlayerData` as the built-in account model. diff --git a/tests/GameCult.Networking.Tests/NetworkingTests.cs b/tests/GameCult.Networking.Tests/NetworkingTests.cs index 8ddf5513..8885d7cc 100644 --- a/tests/GameCult.Networking.Tests/NetworkingTests.cs +++ b/tests/GameCult.Networking.Tests/NetworkingTests.cs @@ -448,6 +448,60 @@ namespace GameCult.Networking.Tests Assert.That(controller.CanAttempt(99_000), Is.True); } + [Test] + public void RudpReconnectLoop_ConsumesSharedController() + { + using var serverSocket = BindUdpSocket(); + var remoteEndPoint = serverSocket.LocalEndPoint!; + var openedLocalPorts = new List(); + const uint connectionId = 0x22334455; + + using var loop = new CultNetRudpReconnectLoop( + () => + { + var socket = BindUdpSocket(); + openedLocalPorts.Add(((IPEndPoint)socket.LocalEndPoint!).Port); + return new CultNetRudpSocketTransportConnection(new CultNetRudpSocketTransportOptions + { + RuntimeId = "csharp-rudp-reconnect", + Socket = socket, + Mode = CultNetRudpSocketMode.Client, + RemoteEndPoint = remoteEndPoint, + ConnectionId = connectionId + }); + }, + CultNetReconnectPolicies.CreateDefault(maxAttempts: 2), + Encoding.UTF8.GetBytes("join")); + + var first = loop.Start(); + Assert.That(first.Stats.BytesSent, Is.GreaterThan(0)); + Assert.That(loop.Transport, Is.SameAs(first)); + Assert.That(openedLocalPorts, Has.Count.EqualTo(1)); + + var decision = loop.HandleClosed(10_000, 17); + Assert.That(decision, Is.Not.Null); + Assert.That(decision!.Attempt, Is.EqualTo(1)); + Assert.That(decision.ShouldRetry, Is.True); + Assert.That(decision.DelayMs, Is.EqualTo(1_017)); + Assert.That(decision.NextAttemptAtMs, Is.EqualTo(11_017)); + Assert.That(loop.ReconnectController.Attempt, Is.EqualTo(1)); + Assert.That(loop.ReconnectController.NextAttemptAtMs, Is.EqualTo(11_017)); + Assert.That(loop.Transport, Is.Null); + + Assert.That(loop.ReconnectIfDue(11_016), Is.False); + Assert.That(openedLocalPorts, Has.Count.EqualTo(1)); + Assert.That(loop.ReconnectIfDue(11_017), Is.True); + Assert.That(openedLocalPorts, Has.Count.EqualTo(2)); + Assert.That(loop.Transport, Is.Not.Null); + + loop.MarkConnected(); + Assert.That(loop.ReconnectController.Attempt, Is.EqualTo(0)); + + loop.Stop(); + Assert.That(loop.Transport, Is.Null); + Assert.That(loop.ReconnectController.Attempt, Is.EqualTo(0)); + } + [Test] public void RudpSession_HandshakeAcksReliableConnectAndAcceptPackets() {