Stream exact hot state over negotiated body planes

This commit is contained in:
Meta Vens 2026-07-19 05:54:24 +01:00
commit 743fa3889e
22 changed files with 320 additions and 14 deletions

View file

@ -2852,6 +2852,40 @@ namespace GameCult.Mesh
return new CultMeshRudpEndpoint(host, uri.Port, $"rudp://{uriHost}:{uri.Port}");
}
/// <summary>
/// Opens one exact hot-body subscription and advertises every body plane this runtime can
/// actually open. The provider derives locality and publishes only the selected plane.
/// </summary>
public static Task<IReadOnlyList<object>> SubscribeHotBodyAsync(
CultNetDatabaseSubscriptionClient subscriptions,
IEnumerable<CultMeshBodyTransportKind> supportedBodyTransports,
CultMeshHotBodySubscription subscription)
{
if (subscriptions == null) throw new ArgumentNullException(nameof(subscriptions));
if (supportedBodyTransports == null) throw new ArgumentNullException(nameof(supportedBodyTransports));
if (subscription == null) throw new ArgumentNullException(nameof(subscription));
var transports = supportedBodyTransports.Distinct().ToArray();
if (transports.Length == 0)
throw new ArgumentException("At least one readable body transport is required.", nameof(supportedBodyTransports));
return subscriptions.SubscribeAsync(
subscription.SubscriptionId,
recordKeys: subscription.RecordKeys,
schemaIds: subscription.SchemaIds,
consumerRuntimeId: subscription.ConsumerRuntimeId,
bodyIds: new[] { subscription.BodyId },
supportedBodyTransports: transports.Select(value => value.ToString()));
}
/// <summary>Opens an exact hot-body subscription using a resolver's readable planes.</summary>
public static Task<IReadOnlyList<object>> SubscribeHotBodyAsync(
CultNetDatabaseSubscriptionClient subscriptions,
CultMeshBodyPublicationResolver bodyResolver,
CultMeshHotBodySubscription subscription)
{
if (bodyResolver == null) throw new ArgumentNullException(nameof(bodyResolver));
return SubscribeHotBodyAsync(subscriptions, bodyResolver.SupportedTransports, subscription);
}
/// <summary>Resolves a CultMesh URI or RUDP endpoint into a concrete RUDP endpoint.</summary>
public static CultMeshRudpEndpoint ResolveRudpEndpoint(string endpointOrUri)
{

View file

@ -150,6 +150,11 @@ namespace GameCult.Mesh
private readonly Func<CultMeshBodyDescriptor, bool> _authorizeProducer;
private readonly Func<string, CultMeshBodyDescriptor, bool>? _authorizeNamedProducer;
/// <summary>Gets locally available body planes in fastest-local-first order.</summary>
public IReadOnlyList<CultMeshBodyTransportKind> SupportedTransports => _adapters.Keys
.OrderByDescending(TransportPreference)
.ToArray();
public CultMeshBodyTransportService(
IEnumerable<ICultMeshBodyTransportAdapter> adapters,
Func<CultMeshBodyDescriptor, bool> authorizeProducer)
@ -264,6 +269,13 @@ namespace GameCult.Mesh
left.AccessMode == right.AccessMode &&
left.Synchronization == right.Synchronization &&
string.Equals(left.SemanticHash, right.SemanticHash, StringComparison.Ordinal);
private static int TransportPreference(CultMeshBodyTransportKind transport) => transport switch
{
CultMeshBodyTransportKind.SharedMemory => 3,
CultMeshBodyTransportKind.SharedFileMapping => 2,
_ => 1
};
}
public sealed class CultMeshMappedBodyPublisher

View file

@ -7,6 +7,57 @@ using GameCult.Networking;
namespace GameCult.Mesh
{
/// <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
/// body bytes through retained document snapshots.
/// </summary>
public sealed class CultMeshHotBodySubscription
{
public CultMeshHotBodySubscription(
string subscriptionId,
string consumerRuntimeId,
string viewRecordKey,
string viewSchemaId,
string bodyId,
IEnumerable<string>? additionalRecordKeys = null,
IEnumerable<string>? additionalSchemaIds = null)
{
SubscriptionId = Require(subscriptionId, nameof(subscriptionId));
ConsumerRuntimeId = Require(consumerRuntimeId, nameof(consumerRuntimeId));
BodyId = Require(bodyId, nameof(bodyId));
RecordKeys = new[]
{
Require(viewRecordKey, nameof(viewRecordKey)),
CultMeshBodyPublicationDocument.CreateLatestRecordKey(bodyId).Value
}
.Concat(additionalRecordKeys ?? Array.Empty<string>())
.Where(value => !string.IsNullOrWhiteSpace(value))
.Distinct(StringComparer.Ordinal)
.ToArray();
SchemaIds = new[]
{
Require(viewSchemaId, nameof(viewSchemaId)),
CultMeshBodyPublicationSchemaVersions.Publication
}
.Concat(additionalSchemaIds ?? Array.Empty<string>())
.Where(value => !string.IsNullOrWhiteSpace(value))
.Distinct(StringComparer.Ordinal)
.ToArray();
}
public string SubscriptionId { get; }
public string ConsumerRuntimeId { get; }
public string BodyId { get; }
public IReadOnlyList<string> RecordKeys { get; }
public IReadOnlyList<string> SchemaIds { get; }
private static string Require(string value, string parameterName) =>
string.IsNullOrWhiteSpace(value)
? throw new ArgumentException("Value is required.", parameterName)
: value;
}
/// <summary>Derived transport work for one logical hot body.</summary>
public sealed class CultMeshBodyDemandPlan
{

View file

@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
@ -361,6 +362,9 @@ namespace GameCult.Mesh
public CultMeshBodyPublicationResolver(CultMeshBodyTransportService transport) =>
_transport = transport ?? throw new ArgumentNullException(nameof(transport));
/// <summary>Gets the body planes this resolver can actually open.</summary>
public IReadOnlyList<CultMeshBodyTransportKind> SupportedTransports => _transport.SupportedTransports;
public ICultMeshBodyReadLease ResolveReadOnly(
CultMeshBodyPublicationDocument publication,
CultMeshBodyValidationRequest request) =>

View file

@ -918,6 +918,14 @@ stream demand merely by existing.
and server disposal all update the same typed demand projection. Stable peer
identity, not per-message wrapper identity, keys subscription lifetime.
`CultMesh.SubscribeHotBodyAsync(...)` is the public construction path. Its
`CultMeshHotBodySubscription` keeps the exact view record, the body's stable
latest-publication record, consumer identity, and logical body demand in one
contract. The local `CultMeshBodyTransportService` contributes only adapters it
can actually open, ordered shared-memory first; the server combines those
capabilities with observed peer locality. Applications do not hand-assemble
transport-name arrays or request one snapshot per frame.
**Cut line:** local frame publication writes directly into a mapped write lease
and commits its descriptor. The compatibility byte-array publisher is copy-in
only. The current remote frame representation may still use the verified CDN
@ -925,6 +933,12 @@ body path when remote demand exists; Phase 6 must replace that expiry-sensitive
traffic with a stream/datagram body plane rather than enlarging snapshot or CDN
timeouts.
Reliable control messages larger than the RUDP receive mask explicitly
acknowledge the packet just received. A retransmit older than the rolling
32-packet summary can therefore always leave the sender queue; the rolling mask
remains useful for piggyback acknowledgements but is not allowed to strand a
fragment indefinitely.
### Gate
- Two local consumers map one verified CDN artifact without duplicate process

View file

@ -163,16 +163,17 @@ namespace GameCult.Networking
{
var changeType = change.GetType();
var key = (CultRecordKey)(changeType.GetProperty("Key")?.GetValue(change) ?? new CultRecordKey(""));
var schemaId = (string?)changeType.GetProperty("SchemaId")?.GetValue(change) ?? "";
if (request.RecordKeys is { Length: > 0 } && !request.RecordKeys.Contains(key.Value, StringComparer.Ordinal))
return null;
if (request.SchemaIds is { Length: > 0 } && !request.SchemaIds.Contains(schemaId, StringComparer.Ordinal))
return null;
var kind = (CultNetDatabaseChangeKind)(changeType.GetProperty("Kind")?.GetValue(change) ?? CultNetDatabaseChangeKind.Updated);
var document = changeType.GetProperty("Document")?.GetValue(change);
if (kind == CultNetDatabaseChangeKind.Removed || document == null)
{
var previous = changeType.GetProperty("PreviousDocument")?.GetValue(change);
var schemaId = ResolveWireSchemaId(previous, (string?)changeType.GetProperty("SchemaId")?.GetValue(change) ?? "");
if (!MatchesRequestedSchema(request.SchemaIds, previous, schemaId))
return null;
return new CultNetDatabaseChangeRawMessage
{
MessageId = Guid.NewGuid().ToString("N"),
@ -183,15 +184,33 @@ namespace GameCult.Networking
};
}
var raw = CreateRawRecord(key, document);
if (!MatchesRequestedSchema(request.SchemaIds, document, raw.SchemaId))
return null;
return new CultNetDatabaseChangeRawMessage
{
MessageId = Guid.NewGuid().ToString("N"),
SubscriptionId = subscriptionId,
ChangeKind = kind == CultNetDatabaseChangeKind.Added ? "added" : "updated",
Document = CreateRawRecord(key, document)
Document = raw
};
}
private string ResolveWireSchemaId(object? document, string fallback)
{
if (document == null) return fallback;
return _database.Documents.GetByDocumentType(document.GetType())?.SchemaId ?? fallback;
}
private bool MatchesRequestedSchema(string[]? requestedSchemaIds, object? document, string wireSchemaId)
{
if (requestedSchemaIds is not { Length: > 0 }) return true;
if (requestedSchemaIds.Contains(wireSchemaId, StringComparer.Ordinal)) return true;
return document != null && CultNetSchemaAliasMatching.MatchesAny(
requestedSchemaIds,
_database.Cache.Registry.GetRequired(document.GetType()));
}
private CultNetRawDocumentRecord CreateRawRecord(CultRecordKey key, object document)
{
var method = typeof(CultNetDocumentRegistry)

View file

@ -1141,9 +1141,14 @@ namespace GameCult.Networking
/// <summary>
/// Creates a packet carrying the current acknowledgement state.
/// </summary>
public CultNetRudpPacket CreateAck()
public CultNetRudpPacket CreateAck(uint? acknowledgedSequence = null)
{
var (ack, ackMask) = AckState();
if (acknowledgedSequence.HasValue)
{
ack = acknowledgedSequence.Value;
ackMask = 0;
}
return new CultNetRudpPacket
{
PacketType = CultNetRudpPacketType.Ack,
@ -1955,10 +1960,9 @@ namespace GameCult.Networking
delivered = _deliveredFrames.Count > 0 ? _deliveredFrames.Dequeue() : null;
if (packet.PacketType == CultNetRudpPacketType.Accept ||
packet.PacketType == CultNetRudpPacketType.Data ||
delivered != null)
packet.PacketType == CultNetRudpPacketType.Data)
{
SendPacket(_session.CreateAck());
SendPacket(_session.CreateAck(packet.Sequence));
}
return true;
@ -2108,6 +2112,10 @@ namespace GameCult.Networking
/// </summary>
public bool Connected => Session.Connected;
/// <summary>
/// Gets the number of reliable packets still awaiting acknowledgement.
/// </summary>
public int PendingReliablePacketCount => Session.PendingReliableSequences.Count;
/// <summary>
/// Gets the last transport-level remote disconnect reason, if one was received.
/// </summary>
public byte[]? DisconnectReason { get; internal set; }
@ -2349,9 +2357,9 @@ namespace GameCult.Networking
}
delivered = _deliveredFrames.Count > 0 ? _deliveredFrames.Dequeue() : null;
if (packet.PacketType == CultNetRudpPacketType.Data || delivered != null)
if (packet.PacketType == CultNetRudpPacketType.Data)
{
SendPacket(existingPeer.RemoteEndPoint, existingPeer.Session.CreateAck());
SendPacket(existingPeer.RemoteEndPoint, existingPeer.Session.CreateAck(packet.Sequence));
}
return true;