From 4d8d43621de224bf9c39801140820f04fb90eda3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 12 Jul 2026 12:05:31 +0100 Subject: [PATCH] Stream database subscriptions through schema servers --- .../CultNetDatabaseSubscriptionServer.cs | 177 ++++++++++++++++++ .../NetworkingTests.cs | 55 ++++++ 2 files changed, 232 insertions(+) create mode 100644 src/GameCult.Networking/CultNetDatabaseSubscriptionServer.cs diff --git a/src/GameCult.Networking/CultNetDatabaseSubscriptionServer.cs b/src/GameCult.Networking/CultNetDatabaseSubscriptionServer.cs new file mode 100644 index 00000000..785b35aa --- /dev/null +++ b/src/GameCult.Networking/CultNetDatabaseSubscriptionServer.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using GameCult.Caching; +using R3; + +namespace GameCult.Networking +{ + /// + /// Publishes live database changes through any schema-v0 server transport. + /// + public sealed class CultNetDatabaseSubscriptionServer : IDisposable + { + private readonly ICultNetSchemaServer _server; + private readonly CultNetDatabase _database; + private readonly Func _subscribe; + private readonly Func _unsubscribe; + private readonly ConcurrentDictionary _subscriptions = + new ConcurrentDictionary(SubscriptionKeyComparer.Instance); + private bool _disposed; + + /// Attaches live database subscription handlers to a schema server. + public CultNetDatabaseSubscriptionServer(ICultNetSchemaServer server, CultNetDatabase database) + { + _server = server ?? throw new ArgumentNullException(nameof(server)); + _database = database ?? throw new ArgumentNullException(nameof(database)); + _subscribe = HandleSubscribeAsync; + _unsubscribe = HandleUnsubscribeAsync; + _server.OnCultNet(_subscribe); + _server.OnCultNet(_unsubscribe); + } + + /// Detaches handlers and releases all active watches. + public void Dispose() + { + if (_disposed) return; + _disposed = true; + _server.RemoveCultNetMessageListener(_subscribe); + _server.RemoveCultNetMessageListener(_unsubscribe); + foreach (var subscription in _subscriptions.Values) subscription.Dispose(); + _subscriptions.Clear(); + } + + private Task HandleSubscribeAsync(CultNetDatabaseSubscribeMessage request, ICultNetSchemaServerPeer peer) + { + var subscriptionId = string.IsNullOrWhiteSpace(request.SubscriptionId) + ? request.MessageId + : request.SubscriptionId; + if (string.IsNullOrWhiteSpace(subscriptionId)) + { + peer.SendCultNet(new CultNetErrorMessage { Error = "Database subscription requires a subscriptionId or messageId." }); + return Task.CompletedTask; + } + + var key = new SubscriptionKey(peer, subscriptionId); + _subscriptions.AddOrUpdate( + key, + _ => Watch(request, subscriptionId, peer), + (_, current) => + { + current.Dispose(); + return Watch(request, subscriptionId, peer); + }); + if (request.IncludeSnapshot) + { + peer.SendCultNet(_database.Documents.CreateRawSnapshotResponse( + _database.Cache, + request.MessageId, + new CultNetSnapshotRequestMessage + { + MessageId = request.MessageId, + SchemaIds = request.SchemaIds, + RecordKeys = request.RecordKeys + })); + } + return Task.CompletedTask; + } + + private Task HandleUnsubscribeAsync(CultNetDatabaseUnsubscribeMessage request, ICultNetSchemaServerPeer peer) + { + var subscriptionId = string.IsNullOrWhiteSpace(request.SubscriptionId) + ? request.MessageId + : request.SubscriptionId; + if (!string.IsNullOrWhiteSpace(subscriptionId) && + _subscriptions.TryRemove(new SubscriptionKey(peer, subscriptionId), out var subscription)) + subscription.Dispose(); + return Task.CompletedTask; + } + + private IDisposable Watch( + CultNetDatabaseSubscribeMessage request, + string subscriptionId, + ICultNetSchemaServerPeer peer) + { + return _database.WatchAllChanges().Subscribe(change => + { + var message = CreateChange(change, request, subscriptionId); + if (message != null) peer.SendCultNet(message); + }); + } + + private CultNetDatabaseChangeRawMessage? CreateChange( + object change, + CultNetDatabaseSubscribeMessage request, + string subscriptionId) + { + 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) + { + return new CultNetDatabaseChangeRawMessage + { + MessageId = Guid.NewGuid().ToString("N"), + SubscriptionId = subscriptionId, + ChangeKind = "removed", + RecordKey = key.Value, + SchemaId = schemaId + }; + } + + return new CultNetDatabaseChangeRawMessage + { + MessageId = Guid.NewGuid().ToString("N"), + SubscriptionId = subscriptionId, + ChangeKind = kind == CultNetDatabaseChangeKind.Added ? "added" : "updated", + Document = CreateRawRecord(key, document) + }; + } + + private CultNetRawDocumentRecord CreateRawRecord(CultRecordKey key, object document) + { + var method = typeof(CultNetDocumentRegistry) + .GetMethods(BindingFlags.Public | BindingFlags.Instance) + .Single(candidate => candidate.Name == nameof(CultNetDocumentRegistry.CreateRawDocumentPutMessage) && + candidate.IsGenericMethodDefinition); + var documentType = document.GetType(); + var handle = Activator.CreateInstance( + typeof(CultRecordHandle<>).MakeGenericType(documentType), + new object[] { key }); + var put = method.MakeGenericMethod(documentType) + .Invoke(_database.Documents, new[] { Guid.NewGuid().ToString("N"), handle, document, null }); + return ((CultNetDocumentPutRawMessage)put!).Document; + } + + private sealed class SubscriptionKey + { + public SubscriptionKey(ICultNetSchemaServerPeer peer, string id) + { + Peer = peer; + Id = id; + } + public ICultNetSchemaServerPeer Peer { get; } + public string Id { get; } + } + + private sealed class SubscriptionKeyComparer : IEqualityComparer + { + public static SubscriptionKeyComparer Instance { get; } = new SubscriptionKeyComparer(); + public bool Equals(SubscriptionKey? x, SubscriptionKey? y) => + x != null && y != null && ReferenceEquals(x.Peer, y.Peer) && string.Equals(x.Id, y.Id, StringComparison.Ordinal); + public int GetHashCode(SubscriptionKey value) => + (RuntimeHelpers.GetHashCode(value.Peer) * 397) ^ StringComparer.Ordinal.GetHashCode(value.Id); + } + } +} diff --git a/tests/GameCult.Networking.Tests/NetworkingTests.cs b/tests/GameCult.Networking.Tests/NetworkingTests.cs index 53de18cf..82f777f5 100644 --- a/tests/GameCult.Networking.Tests/NetworkingTests.cs +++ b/tests/GameCult.Networking.Tests/NetworkingTests.cs @@ -1307,6 +1307,61 @@ namespace GameCult.Networking.Tests cancellation.Cancel(); } + [Test] + public async Task DatabaseSubscriptionServer_StreamsRecordChangesOverRudp() + { + var cache = new CultCache(); + var database = new CultNetDatabase(cache); + using var server = new RudpCultNetSchemaServer(new RudpCultNetSchemaServerOptions + { + RuntimeId = "database-subscription-server", + Socket = BindUdpSocket() + }); + using var subscriptions = new CultNetDatabaseSubscriptionServer(server, database); + using var cancellation = new CancellationTokenSource(); + var serverThread = new Thread(() => + { + while (!cancellation.IsCancellationRequested) + { + _ = server.PollOnceAsync().GetAwaiter().GetResult(); + Thread.Sleep(1); + } + }) { IsBackground = true }; + serverThread.Start(); + + using var client = CultNetSchemaClients.CreateRudp("database-subscription-client"); + var subscribed = new TaskCompletionSource( + TaskCreationOptions.RunContinuationsAsynchronously); + var changed = new TaskCompletionSource( + TaskCreationOptions.RunContinuationsAsynchronously); + client.OnCultNet(message => subscribed.TrySetResult(message)); + client.OnCultNet(message => changed.TrySetResult(message)); + client.Connect("127.0.0.1", server.LocalEndPoint.Port); + await WaitUntilAsync(() => client.Connected, TimeSpan.FromSeconds(2)); + const string recordKey = "tests:subscription:note"; + client.SendCultNet(new CultNetDatabaseSubscribeMessage + { + MessageId = "subscribe-note", + SubscriptionId = "note", + RecordKeys = [recordKey], + IncludeSnapshot = true + }); + await AwaitWithTimeout(subscribed.Task, TimeSpan.FromSeconds(2)); + + await database.PutAsync(new CultRecordKey(recordKey), new NetworkSchemaNote + { + Schema = "tests.networking_note.v1", + Text = "live" + }); + var update = await AwaitWithTimeout(changed.Task, TimeSpan.FromSeconds(2)); + + cancellation.Cancel(); + Assert.That(update.SubscriptionId, Is.EqualTo("note")); + Assert.That(update.ChangeKind, Is.EqualTo("added")); + Assert.That(update.Document, Is.Not.Null); + Assert.That(update.Document!.RecordKey, Is.EqualTo(recordKey)); + } + [Test] public async Task RudpCultNetSchemaServer_DeliversLargeSnapshotResponse() {