Add identity-first CultMesh application client
This commit is contained in:
parent
bc8270ecc3
commit
6403744ada
3 changed files with 255 additions and 20 deletions
139
src/GameCult.Mesh/CultMeshClient.cs
Normal file
139
src/GameCult.Mesh/CultMeshClient.cs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using GameCult.Networking;
|
||||
|
||||
namespace GameCult.Mesh
|
||||
{
|
||||
/// <summary>Configures one application-lifetime CultMesh client.</summary>
|
||||
public sealed class CultMeshClientOptions
|
||||
{
|
||||
/// <summary>Gets or sets the rendezvous endpoints used to bootstrap stable identities.</summary>
|
||||
public IReadOnlyList<string> RendezvousEndpoints { get; set; } = Array.Empty<string>();
|
||||
|
||||
/// <summary>Gets or sets Verse discovery transport and persistence options.</summary>
|
||||
public CultMeshVerseDiscoveryClientOptions Discovery { get; set; } = new();
|
||||
|
||||
/// <summary>Gets or sets session path and diagnostic policy.</summary>
|
||||
public CultMeshSessionManagerOptions Sessions { get; set; } = new();
|
||||
|
||||
/// <summary>Gets or sets optional transport connectors. The CultNet schema connector is used by default.</summary>
|
||||
public IReadOnlyList<ICultMeshTransportConnector>? Connectors { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Owns discovery and reusable sessions for one application lifetime.
|
||||
/// Applications address stable identities; CultMesh owns physical routes and reconnection.
|
||||
/// </summary>
|
||||
public sealed class CultMeshClient : IDisposable
|
||||
{
|
||||
private readonly CultMeshDiscoveryService _discovery;
|
||||
private readonly CultMeshSessionManager _sessions;
|
||||
private bool _disposed;
|
||||
|
||||
public CultMeshClient(CultMeshClientOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
var endpoints = options.RendezvousEndpoints
|
||||
.Where(endpoint => !string.IsNullOrWhiteSpace(endpoint))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
if (endpoints.Length == 0)
|
||||
throw new ArgumentException("At least one CultMesh rendezvous endpoint is required.", nameof(options));
|
||||
|
||||
var discoveryClient = new CultMeshVerseDiscoveryClient(options.Discovery);
|
||||
_discovery = new CultMeshDiscoveryService(
|
||||
endpoints.Select(endpoint => new RendezvousLookupSource(endpoint, discoveryClient, options.Discovery)),
|
||||
new CultMeshDiscoveryServiceOptions
|
||||
{
|
||||
Clock = options.Discovery.Clock,
|
||||
Diagnostics = options.Discovery.Diagnostics,
|
||||
Store = options.Discovery.DiscoveryStore
|
||||
});
|
||||
_sessions = new CultMeshSessionManager(
|
||||
_discovery,
|
||||
options.Connectors ?? new ICultMeshTransportConnector[]
|
||||
{
|
||||
new CultMeshSchemaTransportConnector(clock: options.Sessions.Clock)
|
||||
},
|
||||
options.Sessions);
|
||||
}
|
||||
|
||||
/// <summary>Connects to a stable endpoint identity using one application protocol.</summary>
|
||||
public Task<CultMeshSession> ConnectAsync(
|
||||
string endpointId,
|
||||
CultMeshProtocolId protocol,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return _sessions.ConnectAsync(CultMeshEndpointId.Parse(endpointId), protocol, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Connects to a stable endpoint identity using one application protocol.</summary>
|
||||
public Task<CultMeshSession> ConnectAsync(
|
||||
CultMeshEndpointId endpointId,
|
||||
CultMeshProtocolId protocol,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return _sessions.ConnectAsync(endpointId, protocol, cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_sessions.Dispose();
|
||||
_discovery.Dispose();
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException(nameof(CultMeshClient));
|
||||
}
|
||||
|
||||
private sealed class RendezvousLookupSource : ICultMeshLookupSource
|
||||
{
|
||||
private readonly string _endpoint;
|
||||
private readonly CultMeshVerseDiscoveryClient _client;
|
||||
private readonly CultMeshVerseDiscoveryClientOptions _options;
|
||||
|
||||
public RendezvousLookupSource(
|
||||
string endpoint,
|
||||
CultMeshVerseDiscoveryClient client,
|
||||
CultMeshVerseDiscoveryClientOptions options)
|
||||
{
|
||||
_endpoint = endpoint;
|
||||
_client = client;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public string SourceId => "rendezvous:" + _endpoint;
|
||||
|
||||
public async Task<IReadOnlyList<CultMeshDiscoveryObservation>> LookupAsync(
|
||||
CultMeshDiscoveryQuery query,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var response = await _client.FetchAsync(
|
||||
_endpoint,
|
||||
new CultMeshVerseCatalogRequestMessage
|
||||
{
|
||||
VerseIds = query.VerseIds.Count == 0 ? null : query.VerseIds.ToArray(),
|
||||
TransportVersion = "cultmesh.v0"
|
||||
}).ConfigureAwait(false);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var observedAt = _options.Clock.UtcNow;
|
||||
return response.Verses.Select(message => new CultMeshDiscoveryObservation(
|
||||
message.ToVerseDescriptor(),
|
||||
SourceId,
|
||||
observedAt,
|
||||
observedAt + _options.ObservationTtl,
|
||||
CultMeshDiscoveryTrust.Unsigned,
|
||||
response.SchemaVersion)).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue