Add C# RUDP reconnect loop
This commit is contained in:
parent
3fde29cc04
commit
9a79586d74
5 changed files with 199 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1834,6 +1834,139 @@ namespace GameCult.Networking
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Caller-owned reconnect loop for the socket-backed CultNet RUDP transport.
|
||||
/// </summary>
|
||||
public sealed class CultNetRudpReconnectLoop : IDisposable
|
||||
{
|
||||
private readonly Func<CultNetRudpSocketTransportConnection> _createTransport;
|
||||
private readonly byte[] _connectPayload;
|
||||
private CultNetRudpSocketTransportConnection? _transport;
|
||||
private bool _stopped = true;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new reconnect loop around caller-owned RUDP transport construction.
|
||||
/// </summary>
|
||||
public CultNetRudpReconnectLoop(
|
||||
Func<CultNetRudpSocketTransportConnection> createTransport,
|
||||
CultNetReconnectPolicy? reconnectPolicy = null,
|
||||
byte[]? connectPayload = null)
|
||||
{
|
||||
_createTransport = createTransport ?? throw new ArgumentNullException(nameof(createTransport));
|
||||
_connectPayload = connectPayload?.ToArray() ?? Array.Empty<byte>();
|
||||
ReconnectController = new CultNetReconnectController(reconnectPolicy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the shared reconnect controller that owns attempt, delay, and exhaustion state.
|
||||
/// </summary>
|
||||
public CultNetReconnectController ReconnectController { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current transport, if the loop has one open.
|
||||
/// </summary>
|
||||
public CultNetRudpSocketTransportConnection? Transport => _transport;
|
||||
|
||||
/// <summary>
|
||||
/// Opens the first transport and sends its connect packet.
|
||||
/// </summary>
|
||||
public CultNetRudpSocketTransportConnection Start()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
_stopped = false;
|
||||
ReconnectController.Reset();
|
||||
return OpenTransport();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops reconnecting, disposes the current transport, and clears retry state.
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
_stopped = true;
|
||||
DisposeTransport();
|
||||
ReconnectController.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears retry state after the caller observes an established connection.
|
||||
/// </summary>
|
||||
public void MarkConnected()
|
||||
{
|
||||
ReconnectController.Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports that the current transport closed and records the next retry decision.
|
||||
/// </summary>
|
||||
public CultNetReconnectDecision? HandleClosed(long nowMs, int jitterMs = 0)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
DisposeTransport();
|
||||
return _stopped ? null : ReconnectController.RecordFailure(nowMs, jitterMs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a new transport when the shared controller says the next attempt is due.
|
||||
/// </summary>
|
||||
public bool ReconnectIfDue(long nowMs)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (_stopped || !ReconnectController.CanAttempt(nowMs))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
OpenTransport();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transport connection for the current TCP length-prefixed CultNet schema lane.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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<int>();
|
||||
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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue