interop: a C# RUDP subscription server mode; the subscription client can name the connection id a fixed-id peer serves
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2917d016dc
commit
1f4bd83ab3
5 changed files with 84 additions and 2 deletions
|
|
@ -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<u32>,
|
||||
) -> Result<CultNetRudpSocketTransportConnection> {
|
||||
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()),
|
||||
|
|
|
|||
|
|
@ -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<u32>,
|
||||
/// 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<CultNetRudpSocketTr
|
|||
options.poll_interval,
|
||||
options.resend_delay_ms,
|
||||
"cultmesh-rudp-subscription-client",
|
||||
options.connection_id,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -484,6 +484,7 @@ fn live_csharp_subscription() {
|
|||
};
|
||||
let mut options = CultMeshRudpSubscriptionOptions::new(SocketAddr::from(([127, 0, 0, 1], port)), "rust-subscriber");
|
||||
options.connect_timeout = Duration::from_secs(10);
|
||||
options.connection_id = Some(0x4355_4c54);
|
||||
let client = CultMeshRudpSubscriptionClient::connect(options).unwrap();
|
||||
client.subscribe("live", Selection::default()).unwrap();
|
||||
// snapshot (empty), then the peer's scripted commits: one record, then two records in one commit.
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ static async Task<int> 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<int> 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;
|
||||
|
|
|
|||
68
tests/GameCult.Networking.InteropPeer/SubscriptionHarness.cs
Normal file
68
tests/GameCult.Networking.InteropPeer/SubscriptionHarness.cs
Normal file
|
|
@ -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 <port>`; runs until stdin closes.
|
||||
public static async Task ServeAsync(uint connectionId)
|
||||
{
|
||||
var cache = new CultCache();
|
||||
cache.Registry.GetRequired<SubscriptionInteropNote>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue