diff --git a/packages/cultmesh-rs/src/lib.rs b/packages/cultmesh-rs/src/lib.rs index 5b4981b5..3f97cf6a 100644 --- a/packages/cultmesh-rs/src/lib.rs +++ b/packages/cultmesh-rs/src/lib.rs @@ -818,6 +818,7 @@ pub fn request_raw_snapshot_from_rudp_catalog( options.poll_interval, options.resend_delay_ms, "cultmesh-rudp-snapshot-client", + None, )?; let request = cultnet_rs::CultNetMessage::SnapshotRequest { @@ -942,6 +943,7 @@ pub fn publish_cultnet_messages_to_rudp_catalog( options.poll_interval, options.resend_delay_ms, "cultmesh-rudp-document-publisher", + None, )?; let mut receipts = Vec::with_capacity(messages.len()); @@ -1009,6 +1011,7 @@ fn connect_rudp_client( poll_interval: Duration, resend_delay_ms: u64, transport_id: &str, + connection_id: Option, ) -> Result { let bind_addr = if target.is_ipv4() { SocketAddr::from(([0, 0, 0, 0], 0)) @@ -1024,7 +1027,7 @@ fn connect_rudp_client( socket, mode: cultnet_rs::CultNetRudpSocketMode::Client, remote_addr: Some(target), - connection_id: fresh_rudp_connection_epoch(), + connection_id: connection_id.unwrap_or_else(fresh_rudp_connection_epoch), initial_sequence: None, resend_delay_ms, transport_id: Some(transport_id.to_string()), diff --git a/packages/cultmesh-rs/src/rudp_subscription.rs b/packages/cultmesh-rs/src/rudp_subscription.rs index 3449bfa8..0d054087 100644 --- a/packages/cultmesh-rs/src/rudp_subscription.rs +++ b/packages/cultmesh-rs/src/rudp_subscription.rs @@ -39,6 +39,10 @@ pub struct CultMeshRudpSubscriptionOptions { /// The pump's cadence: how long it waits on the socket, and so how soon it sees a command. pub poll_interval: Duration, pub resend_delay_ms: u64, + /// The RUDP connection id every packet carries. `None` picks a fresh one per connection, which a CultMesh + /// catalog adopts; a peer that serves one fixed id (the C# `RudpCultNetSchemaServer` serves `0x43554c54` unless + /// configured) is reached by naming it. + pub connection_id: Option, /// The most documents and changes that may sit undrained before the subscription faults. pub max_undrained: usize, } @@ -51,6 +55,7 @@ impl CultMeshRudpSubscriptionOptions { connect_timeout: Duration::from_secs(5), poll_interval: Duration::from_millis(5), resend_delay_ms: 50, + connection_id: None, max_undrained: 1_000_000, } } @@ -166,6 +171,7 @@ fn open(options: &CultMeshRudpSubscriptionOptions) -> Result ProgramMainAsync(string[] args) { if (args.Length == 0) { - throw new InvalidOperationException("Expected mode: serve | probe | dial | rudp-serve-once | rudp-dial-once | rudp-serve-message-once | rudp-dial-message-once | quic-realtime-serve | receipt-serve | receipt-relay"); + throw new InvalidOperationException("Expected mode: serve | probe | dial | rudp-serve-once | rudp-dial-once | rudp-serve-message-once | rudp-dial-message-once | quic-realtime-serve | receipt-serve | receipt-relay | rudp-subscription-serve"); } var mode = args[0]; @@ -56,6 +56,10 @@ static async Task ProgramMainAsync(string[] args) case "receipt-serve": await GameCult.Networking.InteropPeer.ReceiptHarness.ServeAsync(); return 0; + case "rudp-subscription-serve": + await GameCult.Networking.InteropPeer.SubscriptionHarness.ServeAsync( + options.TryGetValue("connection-id", out var connectionId) ? uint.Parse(connectionId, CultureInfo.InvariantCulture) : 0x43554c54); + return 0; case "receipt-relay": await GameCult.Networking.InteropPeer.ReceiptHarness.RelayAsync(int.Parse(options["port"], CultureInfo.InvariantCulture)); return 0; diff --git a/tests/GameCult.Networking.InteropPeer/SubscriptionHarness.cs b/tests/GameCult.Networking.InteropPeer/SubscriptionHarness.cs new file mode 100644 index 00000000..faf4dafe --- /dev/null +++ b/tests/GameCult.Networking.InteropPeer/SubscriptionHarness.cs @@ -0,0 +1,68 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using GameCult.Caching; +using GameCult.Networking; + +// A real C# subscription server on RUDP for the Rust subscription client (packages/cultmesh-rs/tests/rudp_subscription.rs, +// live_csharp_subscription). It serves a CultNetDatabase with one document type; once the first subscribe is active it +// commits one record, then two records in one commit, so the client sees a snapshot, a single change and a two-change commit. +namespace GameCult.Networking.InteropPeer +{ + [CultDocument("subscription.interop_note", SubscriptionHarness.SchemaId)] + [MessagePack.MessagePackObject] + public sealed class SubscriptionInteropNote + { + [MessagePack.Key(0)] + public string Text { get; set; } = string.Empty; + } + + internal static class SubscriptionHarness + { + public const string SchemaId = "subscription.interop_note.v1"; + + // stdout: `listening `; runs until stdin closes. + public static async Task ServeAsync(uint connectionId) + { + var cache = new CultCache(); + cache.Registry.GetRequired(); + using var database = new CultNetDatabase(cache); + using var server = new RudpCultNetSchemaServer(new RudpCultNetSchemaServerOptions + { + RuntimeId = "csharp-subscription-peer", + ConnectionId = connectionId + }); + using var subscriptions = new CultNetDatabaseSubscriptionServer(server, database); + var committed = 0; + subscriptions.DemandChanged += demand => + { + if (!demand.Active || Interlocked.Exchange(ref committed, 1) == 1) return; + _ = Task.Run(async () => + { + await Task.Delay(300).ConfigureAwait(false); + cache.Commit(batch => batch.Upsert(typeof(SubscriptionInteropNote), new SubscriptionInteropNote { Text = "one" }, new CultRecordKey("one"))); + await Task.Delay(100).ConfigureAwait(false); + cache.Commit(batch => + { + batch.Upsert(typeof(SubscriptionInteropNote), new SubscriptionInteropNote { Text = "two" }, new CultRecordKey("two")); + batch.Upsert(typeof(SubscriptionInteropNote), new SubscriptionInteropNote { Text = "three" }, new CultRecordKey("three")); + }); + }); + }; + using var stop = new CancellationTokenSource(); + var pump = Task.Run(async () => + { + while (!stop.IsCancellationRequested) + { + var worked = (await server.PollAvailableAsync(64).ConfigureAwait(false)).TransportItemsConsumed > 0; + server.PollResends(); + if (!worked) await Task.Delay(1).ConfigureAwait(false); + } + }); + lock (Console.Out) Console.Out.WriteLine($"listening {server.LocalEndPoint.Port}"); + await Task.Run(() => Console.In.ReadToEnd()); + stop.Cancel(); + await pump.ConfigureAwait(false); + } + } +}