Add ergonomic live body subscriptions
This commit is contained in:
parent
f3d008023e
commit
5587f5c6d1
19 changed files with 382 additions and 24 deletions
|
|
@ -7,6 +7,169 @@ using GameCult.Networking;
|
|||
|
||||
namespace GameCult.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// Declares one logical body demand. CultMesh owns locality detection and transport selection;
|
||||
/// consumers name the body and the planes they can actually read.
|
||||
/// </summary>
|
||||
public sealed class CultMeshLiveBodySubscription
|
||||
{
|
||||
public CultMeshLiveBodySubscription(
|
||||
string subscriptionId,
|
||||
string consumerRuntimeId,
|
||||
string bodyId)
|
||||
{
|
||||
SubscriptionId = Require(subscriptionId, nameof(subscriptionId));
|
||||
ConsumerRuntimeId = Require(consumerRuntimeId, nameof(consumerRuntimeId));
|
||||
BodyId = Require(bodyId, nameof(bodyId));
|
||||
}
|
||||
|
||||
public string SubscriptionId { get; }
|
||||
public string ConsumerRuntimeId { get; }
|
||||
public string BodyId { get; }
|
||||
public string PublicationRecordKey =>
|
||||
CultMeshBodyPublicationDocument.CreateLatestRecordKey(BodyId).Value;
|
||||
|
||||
private static string Require(string value, string parameterName) =>
|
||||
string.IsNullOrWhiteSpace(value)
|
||||
? throw new ArgumentException("Value is required.", parameterName)
|
||||
: value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One ephemeral body subscription. Publication metadata remains reactive control state while
|
||||
/// body bytes stay on the negotiated body plane.
|
||||
/// </summary>
|
||||
public sealed class CultMeshLiveBody : IDisposable
|
||||
{
|
||||
private readonly CultNetDatabaseSubscriptionClient _owner;
|
||||
private readonly CultMeshBodyPublicationResolver _resolver;
|
||||
private readonly CultMeshLiveBodySubscription _subscription;
|
||||
private readonly object _gate = new();
|
||||
private CultMeshBodyPublicationDocument? _current;
|
||||
private bool _hasValue;
|
||||
private bool _disposed;
|
||||
|
||||
internal CultMeshLiveBody(
|
||||
CultNetDatabaseSubscriptionClient owner,
|
||||
CultMeshBodyPublicationResolver resolver,
|
||||
CultMeshLiveBodySubscription subscription)
|
||||
{
|
||||
_owner = owner ?? throw new ArgumentNullException(nameof(owner));
|
||||
_resolver = resolver ?? throw new ArgumentNullException(nameof(resolver));
|
||||
_subscription = subscription ?? throw new ArgumentNullException(nameof(subscription));
|
||||
_owner.Changed += OnChanged;
|
||||
}
|
||||
|
||||
/// <summary>Raised when the provider publishes a new generation descriptor.</summary>
|
||||
public event Action<CultMeshBodyPublicationDocument>? Changed;
|
||||
|
||||
/// <summary>Raised when the provider withdraws the logical body.</summary>
|
||||
public event Action? Removed;
|
||||
|
||||
public string BodyId => _subscription.BodyId;
|
||||
|
||||
public bool HasValue
|
||||
{
|
||||
get { lock (_gate) return _hasValue; }
|
||||
}
|
||||
|
||||
public CultMeshBodyPublicationDocument Current
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
return _hasValue
|
||||
? _current!
|
||||
: throw new InvalidOperationException(
|
||||
$"Live body '{_subscription.BodyId}' is not currently published.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the current generation read-only using the fastest valid advertised representation.
|
||||
/// The caller owns the returned lease and should dispose it before opening a later generation.
|
||||
/// </summary>
|
||||
public CultMeshBodyNegotiationResult OpenCurrentReadOnly(DateTimeOffset? nowUtc = null)
|
||||
{
|
||||
CultMeshBodyPublicationDocument publication;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException(nameof(CultMeshLiveBody));
|
||||
if (!_hasValue)
|
||||
throw new InvalidOperationException(
|
||||
$"Live body '{_subscription.BodyId}' is not currently published.");
|
||||
publication = _current!;
|
||||
}
|
||||
|
||||
return _resolver.NegotiateReadOnly(publication, new CultMeshBodyValidationRequest
|
||||
{
|
||||
BodyId = publication.BodyId,
|
||||
SchemaId = publication.SchemaId,
|
||||
LayoutVersion = publication.LayoutVersion,
|
||||
ProducerEpoch = publication.ProducerEpoch,
|
||||
Sequence = publication.Sequence,
|
||||
Capacity = publication.Capacity,
|
||||
AccessMode = CultMeshBodyAccessMode.ReadOnly,
|
||||
NowUtc = nowUtc ?? DateTimeOffset.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
internal void Initialize(CultMeshBodyPublicationDocument publication)
|
||||
{
|
||||
CultMeshBodyPublicationValidator.Validate(publication, expectedBodyId: _subscription.BodyId);
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed || _hasValue) return;
|
||||
_current = publication;
|
||||
_hasValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_current = null;
|
||||
_hasValue = false;
|
||||
}
|
||||
_owner.Changed -= OnChanged;
|
||||
_owner.Unsubscribe(_subscription.SubscriptionId);
|
||||
}
|
||||
|
||||
private void OnChanged(CultNetReplicatedDocumentChange change)
|
||||
{
|
||||
if (!string.Equals(change.SubscriptionId, _subscription.SubscriptionId, StringComparison.Ordinal) ||
|
||||
!string.Equals(change.RecordKey, _subscription.PublicationRecordKey, StringComparison.Ordinal) ||
|
||||
!string.Equals(change.SchemaId, CultMeshBodyPublicationSchemaVersions.Publication, StringComparison.Ordinal))
|
||||
return;
|
||||
|
||||
if (string.Equals(change.ChangeKind, "removed", StringComparison.Ordinal))
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_current = null;
|
||||
_hasValue = false;
|
||||
}
|
||||
Removed?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
if (change.Document is not CultMeshBodyPublicationDocument publication)
|
||||
return;
|
||||
CultMeshBodyPublicationValidator.Validate(publication, expectedBodyId: _subscription.BodyId);
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_current = publication;
|
||||
_hasValue = true;
|
||||
}
|
||||
Changed?.Invoke(publication);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One exact hot-body subscription. The view record, latest-publication record, consumer body
|
||||
/// demand, and locally supported planes are one contract so callers cannot accidentally stream
|
||||
|
|
@ -68,12 +231,14 @@ namespace GameCult.Mesh
|
|||
BodyId = bodyId;
|
||||
Consumers = consumers;
|
||||
RequiresSharedMemory = consumers.Any(value => value.Transport == CultMeshBodyTransportKind.SharedMemory);
|
||||
RequiresSharedFileMapping = consumers.Any(value => value.Transport == CultMeshBodyTransportKind.SharedFileMapping);
|
||||
RequiresNetwork = consumers.Any(value => value.Transport == CultMeshBodyTransportKind.Network);
|
||||
}
|
||||
|
||||
public string BodyId { get; }
|
||||
public bool HasConsumers => Consumers.Count > 0;
|
||||
public bool RequiresSharedMemory { get; }
|
||||
public bool RequiresSharedFileMapping { get; }
|
||||
public bool RequiresNetwork { get; }
|
||||
public IReadOnlyList<CultMeshBodyConsumerRoute> Consumers { get; }
|
||||
}
|
||||
|
|
@ -172,6 +337,11 @@ namespace GameCult.Mesh
|
|||
demand.ConsumerRuntimeId,
|
||||
demand.SubscriptionId,
|
||||
CultMeshBodyTransportKind.SharedMemory);
|
||||
if (demand.SameMachine && Supports(supported, CultMeshBodyTransportKind.SharedFileMapping))
|
||||
return new CultMeshBodyConsumerRoute(
|
||||
demand.ConsumerRuntimeId,
|
||||
demand.SubscriptionId,
|
||||
CultMeshBodyTransportKind.SharedFileMapping);
|
||||
if (Supports(supported, CultMeshBodyTransportKind.Network))
|
||||
return new CultMeshBodyConsumerRoute(
|
||||
demand.ConsumerRuntimeId,
|
||||
|
|
|
|||
Loading…
Reference in a new issue