Replay CultMesh subscriptions per physical session

This commit is contained in:
Meta Vens 2026-07-13 04:25:03 +01:00
commit 6ffb70cb39
3 changed files with 22 additions and 7 deletions

View file

@ -303,6 +303,7 @@ namespace GameCult.Mesh
private readonly TaskCompletionSource<bool> _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly object _subscribeLock = new();
private Task? _subscribeTask;
private long _subscribedGeneration = -1;
private IDisposable? _stateWatch;
private bool _disposed;
@ -337,7 +338,9 @@ namespace GameCult.Mesh
{
lock (_subscribeLock)
{
if (_subscribeTask is { IsCompleted: false }) return _subscribeTask;
var generation = _session.PhysicalGeneration;
if (_subscribedGeneration == generation && _subscribeTask != null) return _subscribeTask;
_subscribedGeneration = generation;
return _subscribeTask = SubscribeAsync();
}
}
@ -380,6 +383,7 @@ namespace GameCult.Mesh
private readonly TaskCompletionSource<bool> _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly object _subscribeLock = new();
private Task? _subscribeTask;
private long _subscribedGeneration = -1;
private IDisposable? _stateWatch;
private bool _disposed;
@ -412,7 +416,9 @@ namespace GameCult.Mesh
{
lock (_subscribeLock)
{
if (_subscribeTask is { IsCompleted: false }) return _subscribeTask;
var generation = _session.PhysicalGeneration;
if (_subscribedGeneration == generation && _subscribeTask != null) return _subscribeTask;
_subscribedGeneration = generation;
return _subscribeTask = SubscribeAsync();
}
}

View file

@ -163,6 +163,7 @@ namespace GameCult.Mesh
private readonly Subject<CultMeshSessionState> _states = new();
private readonly ConcurrentBag<IDisposable> _subscriptions = new();
private readonly ManagedSchemaChannel _channel;
private long _physicalGeneration;
private bool _disposed;
internal CultMeshSession(CultMeshEndpointId endpointId, CultMeshProtocolId protocol, ICultNetSchemaClient channel, CultMeshSessionState state)
{
@ -175,6 +176,7 @@ namespace GameCult.Mesh
public CultMeshProtocolId Protocol { get; }
internal ICultNetSchemaClient Channel => _channel;
public CultMeshSessionState State { get; private set; }
internal long PhysicalGeneration => Interlocked.Read(ref _physicalGeneration);
public Observable<CultMeshSessionState> WatchState() => _states;
public ICultNetSchemaClient OpenSchemaClient() => new SessionSchemaClient(this);
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage => Channel.SendCultNet(message);
@ -187,7 +189,11 @@ namespace GameCult.Mesh
return subscription;
}
internal void Transition(CultMeshSessionState state) { State = state; _states.OnNext(state); }
internal void ReplacePhysicalChannel(ICultNetSchemaClient channel) => _channel.Replace(channel);
internal void ReplacePhysicalChannel(ICultNetSchemaClient channel)
{
_channel.Replace(channel);
Interlocked.Increment(ref _physicalGeneration);
}
public void Dispose()
{
if (_disposed) return;

View file

@ -424,9 +424,12 @@ and client disposal.
online physical incarnation, cache-backed typed handles, and readiness after
the first successful snapshot from any incarnation.
**Derived state:** physical client identity, attempt count, cached snapshots,
and online/reconnecting/offline projections. No individual request owns the
logical handle.
**Derived state:** physical client identity and generation, attempt count,
cached snapshots, and online/reconnecting/offline projections. The session
increments the generation only after replacing its physical channel. A binding
may coalesce duplicate subscription attempts within that generation, but a
pending request from an older generation cannot suppress replay. No individual
request owns the logical handle.
**Forbidden writers:** bindings and schema clients cannot choose endpoints,
replace channels, reconnect, or declare session state. An abandoned initial
@ -435,7 +438,7 @@ readiness.
**Shared paths:** initial open and reconnect both use session-manager path
resolution. Document and collection activation both replay the same binding
intent when the logical session returns online.
intent once per physical generation when the logical session returns online.
**Cut line:** the initial subscribe no longer runs before reconnect observation,
and a per-binding semaphore no longer lets an unanswered physical request hold