Route snapshot sessions through CultMesh sessions
This commit is contained in:
parent
469b9729d2
commit
fb73c7beff
3 changed files with 100 additions and 2 deletions
|
|
@ -160,6 +160,7 @@ namespace GameCult.Mesh
|
|||
public sealed class CultMeshSession : IDisposable
|
||||
{
|
||||
private readonly Subject<CultMeshSessionState> _states = new();
|
||||
private readonly ConcurrentBag<IDisposable> _subscriptions = new();
|
||||
private bool _disposed;
|
||||
internal CultMeshSession(CultMeshEndpointId endpointId, CultMeshProtocolId protocol, ICultNetSchemaClient channel, CultMeshSessionState state)
|
||||
{
|
||||
|
|
@ -170,17 +171,35 @@ namespace GameCult.Mesh
|
|||
}
|
||||
public CultMeshEndpointId EndpointId { get; }
|
||||
public CultMeshProtocolId Protocol { get; }
|
||||
public ICultNetSchemaClient Channel { get; }
|
||||
internal ICultNetSchemaClient Channel { get; }
|
||||
public CultMeshSessionState State { get; private set; }
|
||||
public Observable<CultMeshSessionState> WatchState() => _states;
|
||||
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage => Channel.SendCultNet(message);
|
||||
public IDisposable OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
|
||||
{
|
||||
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
||||
var subscription = new SessionSubscription<T>(callback);
|
||||
Channel.OnCultNet<T>(subscription.Invoke);
|
||||
_subscriptions.Add(subscription);
|
||||
return subscription;
|
||||
}
|
||||
internal void Transition(CultMeshSessionState state) { State = state; _states.OnNext(state); }
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
foreach (var subscription in _subscriptions) subscription.Dispose();
|
||||
Channel.Dispose();
|
||||
_states.Dispose();
|
||||
}
|
||||
|
||||
private sealed class SessionSubscription<T> : IDisposable where T : ICultNetSchemaMessage
|
||||
{
|
||||
private Action<T>? _callback;
|
||||
public SessionSubscription(Action<T> callback) { _callback = callback; }
|
||||
public void Invoke(T message) => Volatile.Read(ref _callback)?.Invoke(message);
|
||||
public void Dispose() => Interlocked.Exchange(ref _callback, null);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CultMeshSessionManagerOptions
|
||||
|
|
|
|||
|
|
@ -69,6 +69,9 @@ namespace GameCult.Mesh
|
|||
private readonly string _endpoint;
|
||||
private readonly CultMeshSnapshotRequestOptions _defaults;
|
||||
private readonly ICultNetSchemaClient _client;
|
||||
private readonly CultMeshSession? _sharedSession;
|
||||
private readonly IDisposable? _responseSubscription;
|
||||
private readonly IDisposable? _errorSubscription;
|
||||
private readonly CultNetDocumentRegistry _registry;
|
||||
private readonly SemaphoreSlim _requests = new(1, 1);
|
||||
private readonly object _completionLock = new();
|
||||
|
|
@ -93,6 +96,32 @@ namespace GameCult.Mesh
|
|||
_client.Connect(host, port);
|
||||
}
|
||||
|
||||
private CultMeshSnapshotSession(
|
||||
CultMeshSession session,
|
||||
CultMeshSnapshotRequestOptions options,
|
||||
CultNetDocumentRegistry? registry)
|
||||
{
|
||||
_sharedSession = session ?? throw new ArgumentNullException(nameof(session));
|
||||
_endpoint = session.State.Path?.Endpoint ?? session.EndpointId.Value;
|
||||
_defaults = CultMesh.CloneSnapshotRequestOptions(options);
|
||||
_registry = registry ?? new CultNetDocumentRegistry();
|
||||
_client = session.Channel;
|
||||
_responseSubscription = session.OnCultNet<CultNetSnapshotResponseRawMessage>(OnResponse);
|
||||
_errorSubscription = session.OnCultNet<CultNetErrorMessage>(OnError);
|
||||
}
|
||||
|
||||
public static async Task<CultMeshSnapshotSession> ConnectAsync(
|
||||
CultMeshSessionManager sessions,
|
||||
CultMeshEndpointId endpointId,
|
||||
CultMeshSnapshotRequestOptions options,
|
||||
CultNetDocumentRegistry? registry = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (sessions == null) throw new ArgumentNullException(nameof(sessions));
|
||||
var session = await sessions.ConnectAsync(endpointId, CultMeshProtocols.Documents, cancellationToken).ConfigureAwait(false);
|
||||
return new CultMeshSnapshotSession(session, options ?? new CultMeshSnapshotRequestOptions(), registry);
|
||||
}
|
||||
|
||||
/// <summary>Fetches one raw snapshot while retaining the underlying endpoint connection.</summary>
|
||||
public async Task<CultNetSnapshotResponseRawMessage> FetchSnapshotAsync(
|
||||
IReadOnlyList<string>? schemaIds = null,
|
||||
|
|
@ -165,7 +194,9 @@ namespace GameCult.Mesh
|
|||
_disposed = true;
|
||||
lock (_completionLock)
|
||||
_completion?.TrySetException(new ObjectDisposedException(nameof(CultMeshSnapshotSession)));
|
||||
_client.Dispose();
|
||||
_responseSubscription?.Dispose();
|
||||
_errorSubscription?.Dispose();
|
||||
if (_sharedSession == null) _client.Dispose();
|
||||
_requests.Dispose();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue