Add C# RUDP schema service host
This commit is contained in:
parent
1c85eabefb
commit
f06914d7c5
4 changed files with 304 additions and 1 deletions
|
|
@ -296,6 +296,12 @@ Current progress:
|
|||
server substrate cut: UDP/session ownership is no longer limited to the
|
||||
single-peer interop/client helper, but authentication and game-session
|
||||
authority still remain above the transport.
|
||||
- C# now also has `RudpCultNetSchemaServer`, a multi-peer schema-v0 service
|
||||
host over that listener. Service code can register `OnCultNet<T>` handlers
|
||||
and reply through `peer.SendCultNet(...)` without owning UDP demux or RUDP
|
||||
session state. Database, Verse discovery, peer exchange, and simulation
|
||||
bridges still need to adopt the shared schema-server peer port before the
|
||||
old LiteNetLib `Server` bridge can be demoted.
|
||||
- C# `GameCult.Mesh` now mirrors the branded RUDP facade shape used by
|
||||
TypeScript, Python, Kotlin, and Rust: it parses `rudp://host:port` endpoints,
|
||||
creates RUDP client/server transports from endpoint or peer-card contact
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ CultMesh.
|
|||
|
||||
| Runtime | Role | Claimed parity | Not claimed |
|
||||
| --- | --- | --- | --- |
|
||||
| C# | Reference implementation and production game/server substrate | CultCache typed documents and SoA, CultNet schema-v0, LiteNetLib production path with adapter profile, shared transport profiles, shared RUDP socket transport, C# multi-peer RUDP listener substrate, C# interop RUDP serve/dial path, connected RUDP CultMesh client helpers, RUDP-backed schema-client selection for `rudp://` service endpoints, RUDP reconnect policy/controller and reconnect-loop helper, LiteNetLib client reconnect-controller adoption, schema-client transport port for service request/response clients, CultMesh node/session/catalog/stream/simulation/RUDP surfaces, authorized peer lookup | No need to abandon LiteNetLib before the CultNet adapter fully owns production service adoption |
|
||||
| C# | Reference implementation and production game/server substrate | CultCache typed documents and SoA, CultNet schema-v0, LiteNetLib production path with adapter profile, shared transport profiles, shared RUDP socket transport, C# multi-peer RUDP listener and schema service host, C# interop RUDP serve/dial path, connected RUDP CultMesh client helpers, RUDP-backed schema-client selection for `rudp://` service endpoints, RUDP reconnect policy/controller and reconnect-loop helper, LiteNetLib client reconnect-controller adoption, schema-client transport port for service request/response clients, CultMesh node/session/catalog/stream/simulation/RUDP surfaces, authorized peer lookup | No need to abandon LiteNetLib before the CultNet adapter fully owns production service adoption |
|
||||
| Python | Package runtime, local daemon endpoint, raw interop peer, typed state participant | CultCache/CultNet/CultMesh package surface, branded CultMesh node/schema/shard/Verse/peer/authority/stream/RUDP ergonomics, authorized peer lookup, shard catalogs/logs, schema-transport factory for raw client request/subscription flows, local TCP and RUDP CultMesh server listeners, Python interop RUDP serve/dial path, session/prediction/simulation fact helpers, RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, benchmark evidence against C# cache paths | Universal C# throughput, full C# daemon/server stack, and RUDP as the only public service pipe |
|
||||
| TypeScript | Node/browser-adjacent projection and interop runtime | CultCache parity tests, CultNet schema-v0 peer, advertised schema/shard catalog wire contracts plus request/sync helpers, transport profiles, TCP-framed transport port and peer helper, shared RUDP socket transport, RUDP interop serve/dial path, RUDP reconnect policy/controller, RUDP reconnect-loop helper, branded CultMesh node/schema/shard/Verse/peer/authority/stream/RUDP ergonomics, authorized peer lookup | Full C# game session/server stack |
|
||||
| Rust | Low-level cache/transport/runtime substrate | CultCache/CultNet interop lane, schema and shard catalog helpers, transport profiles, TCP-framed port, shared RUDP codec/session/socket transport, Rust interop RUDP serve/dial path, connected RUDP CultMesh client helpers, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, small CultMesh facade for schema registry, shard catalog, peer cards, authority leases, authorized peer lookup, RUDP endpoint parsing, and peer-card/authorized-peer RUDP client construction | Full CultMesh game-session/server facade, stream catalog, or production game-session server |
|
||||
|
|
|
|||
225
src/GameCult.Networking/CultNetRudpSchemaServer.cs
Normal file
225
src/GameCult.Networking/CultNetRudpSchemaServer.cs
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameCult.Networking
|
||||
{
|
||||
/// <summary>
|
||||
/// Options for hosting schema-v0 messages over the native CultNet RUDP listener.
|
||||
/// </summary>
|
||||
public sealed class RudpCultNetSchemaServerOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the runtime id advertised by the RUDP listener.
|
||||
/// </summary>
|
||||
public string RuntimeId { get; set; } = "csharp-rudp-schema-server";
|
||||
/// <summary>
|
||||
/// Gets or sets the UDP socket used by the listener. When omitted, a loopback socket is created.
|
||||
/// </summary>
|
||||
public Socket? Socket { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the accepted RUDP connection id.
|
||||
/// </summary>
|
||||
public uint ConnectionId { get; set; } = 0x43554c54;
|
||||
/// <summary>
|
||||
/// Gets or sets the first local packet sequence for each accepted peer.
|
||||
/// </summary>
|
||||
public uint InitialSequence { get; set; } = 100;
|
||||
/// <summary>
|
||||
/// Gets or sets the resend delay in milliseconds.
|
||||
/// </summary>
|
||||
public long ResendDelayMs { get; set; } = 25;
|
||||
/// <summary>
|
||||
/// Gets or sets the advertised transport id.
|
||||
/// </summary>
|
||||
public string TransportId { get; set; } = "schema-rudp";
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum fragment size for schema-channel payloads.
|
||||
/// </summary>
|
||||
public int? MaxFragmentBytes { get; set; } = 1024;
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum pending reliable packet count.
|
||||
/// </summary>
|
||||
public int? MaxPendingReliablePackets { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the payload sent with accept packets.
|
||||
/// </summary>
|
||||
public byte[]? AcceptPayload { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Peer context for a schema-v0 RUDP server.
|
||||
/// </summary>
|
||||
public sealed class RudpCultNetSchemaServerPeer
|
||||
{
|
||||
private readonly RudpCultNetSchemaServer _server;
|
||||
|
||||
internal RudpCultNetSchemaServerPeer(
|
||||
RudpCultNetSchemaServer server,
|
||||
CultNetRudpSocketServerPeer transportPeer)
|
||||
{
|
||||
_server = server;
|
||||
TransportPeer = transportPeer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the underlying RUDP transport peer.
|
||||
/// </summary>
|
||||
public CultNetRudpSocketServerPeer TransportPeer { get; }
|
||||
/// <summary>
|
||||
/// Gets the UDP endpoint for this peer.
|
||||
/// </summary>
|
||||
public EndPoint RemoteEndPoint => TransportPeer.RemoteEndPoint;
|
||||
|
||||
/// <summary>
|
||||
/// Sends a schema-v0 response to this peer.
|
||||
/// </summary>
|
||||
public void SendCultNet<TMessage>(TMessage message)
|
||||
where TMessage : ICultNetSchemaMessage
|
||||
{
|
||||
_server.SendCultNet(TransportPeer, message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multi-peer schema-v0 host over the native CultNet RUDP transport.
|
||||
/// </summary>
|
||||
public sealed class RudpCultNetSchemaServer : IDisposable
|
||||
{
|
||||
private readonly Socket _socket;
|
||||
private readonly CultNetRudpSocketTransportServer _transport;
|
||||
private readonly ConcurrentDictionary<Type, Delegate> _handlers = new();
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a schema-v0 RUDP server.
|
||||
/// </summary>
|
||||
public RudpCultNetSchemaServer(RudpCultNetSchemaServerOptions? options = null)
|
||||
{
|
||||
options ??= new RudpCultNetSchemaServerOptions();
|
||||
var socket = options.Socket;
|
||||
if (socket == null)
|
||||
{
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
}
|
||||
|
||||
_socket = socket;
|
||||
_transport = new CultNetRudpSocketTransportServer(new CultNetRudpSocketTransportServerOptions
|
||||
{
|
||||
RuntimeId = options.RuntimeId,
|
||||
Socket = socket,
|
||||
ConnectionId = options.ConnectionId,
|
||||
InitialSequence = options.InitialSequence,
|
||||
ResendDelayMs = options.ResendDelayMs,
|
||||
TransportId = options.TransportId,
|
||||
MaxFragmentBytes = options.MaxFragmentBytes,
|
||||
MaxPendingReliablePackets = options.MaxPendingReliablePackets,
|
||||
AcceptPayload = options.AcceptPayload
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the underlying RUDP listener profile.
|
||||
/// </summary>
|
||||
public CultNetTransportProfile Profile => _transport.Profile;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the local UDP endpoint.
|
||||
/// </summary>
|
||||
public IPEndPoint LocalEndPoint => (IPEndPoint)_socket.LocalEndPoint!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a snapshot of the current transfer counters.
|
||||
/// </summary>
|
||||
public CultNetTransportStats Stats => _transport.Stats;
|
||||
|
||||
/// <summary>
|
||||
/// Registers a schema-v0 handler.
|
||||
/// </summary>
|
||||
public void OnCultNet<TMessage>(Func<TMessage, RudpCultNetSchemaServerPeer, Task> callback)
|
||||
where TMessage : ICultNetSchemaMessage
|
||||
{
|
||||
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
||||
_handlers.AddOrUpdate(
|
||||
typeof(TMessage),
|
||||
_ => callback,
|
||||
(type, current) =>
|
||||
{
|
||||
var combined = Delegate.Combine(current, callback) as Func<TMessage, RudpCultNetSchemaServerPeer, Task>;
|
||||
return combined ?? throw new InvalidOperationException($"Failed to combine delegates for {type.Name}.");
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a schema-v0 handler.
|
||||
/// </summary>
|
||||
public void OnCultNet<TMessage>(Action<TMessage, RudpCultNetSchemaServerPeer> callback)
|
||||
where TMessage : ICultNetSchemaMessage
|
||||
{
|
||||
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
||||
OnCultNet<TMessage>((message, peer) =>
|
||||
{
|
||||
callback(message, peer);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a schema-v0 message to a transport peer.
|
||||
/// </summary>
|
||||
public void SendCultNet<TMessage>(CultNetRudpSocketServerPeer peer, TMessage message)
|
||||
where TMessage : ICultNetSchemaMessage
|
||||
{
|
||||
_transport.SendSchemaMessage(peer, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Polls one packet/frame and dispatches at most one schema-v0 message.
|
||||
/// </summary>
|
||||
public async Task<bool> PollOnceAsync()
|
||||
{
|
||||
var delivered = _transport.ReceiveOnce();
|
||||
_transport.PollResends();
|
||||
if (delivered == null || !string.Equals(delivered.Frame.ChannelId, "schema", StringComparison.Ordinal))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var message = CultNetSchemaMessageSerialization.Deserialize(delivered.Frame.Payload);
|
||||
if (!_handlers.TryGetValue(message.GetType(), out var handler) || handler == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var result = handler.DynamicInvoke(message, new RudpCultNetSchemaServerPeer(this, delivered.Peer));
|
||||
if (result is Task task)
|
||||
{
|
||||
await task.ConfigureAwait(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends any reliable packets whose resend timers are due.
|
||||
/// </summary>
|
||||
public void PollResends()
|
||||
{
|
||||
_transport.PollResends();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
_transport.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1115,6 +1115,78 @@ namespace GameCult.Networking.Tests
|
|||
Assert.That(server.Stats.FramesSent, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RudpCultNetSchemaServer_Dispatches_SchemaMessages()
|
||||
{
|
||||
using var server = new RudpCultNetSchemaServer(new RudpCultNetSchemaServerOptions
|
||||
{
|
||||
RuntimeId = "csharp-rudp-schema-host",
|
||||
Socket = BindUdpSocket(),
|
||||
ConnectionId = 0x43554c54,
|
||||
InitialSequence = 100,
|
||||
ResendDelayMs = 25,
|
||||
MaxFragmentBytes = 1024
|
||||
});
|
||||
var serverDone = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
server.OnCultNet<CultNetSchemaCatalogRequestMessage>((request, peer) =>
|
||||
{
|
||||
peer.SendCultNet(new CultNetSchemaCatalogResponseMessage
|
||||
{
|
||||
MessageId = request.MessageId,
|
||||
Schemas =
|
||||
[
|
||||
new CultNetSchemaDescriptor
|
||||
{
|
||||
SchemaId = "rudp.schema.host",
|
||||
Kind = "wire_message",
|
||||
SchemaVersion = "rudp.schema.host.v0",
|
||||
WireContracts = ["cultnet.schema.v0"]
|
||||
}
|
||||
]
|
||||
});
|
||||
serverDone.TrySetResult();
|
||||
});
|
||||
|
||||
var serverThread = new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!serverDone.Task.IsCompleted)
|
||||
{
|
||||
_ = server.PollOnceAsync().GetAwaiter().GetResult();
|
||||
Thread.Sleep(5);
|
||||
}
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
serverDone.TrySetException(error);
|
||||
}
|
||||
})
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
serverThread.Start();
|
||||
|
||||
using var client = CultNetSchemaClients.CreateRudp(runtimeId: "csharp-rudp-schema-host-client");
|
||||
var responseCompletion = new TaskCompletionSource<CultNetSchemaCatalogResponseMessage>(
|
||||
TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
client.OnCultNet<CultNetSchemaCatalogResponseMessage>(response => responseCompletion.TrySetResult(response));
|
||||
client.Connect("127.0.0.1", server.LocalEndPoint.Port);
|
||||
await WaitUntilAsync(() => client.Connected, TimeSpan.FromSeconds(2));
|
||||
client.SendCultNet(new CultNetSchemaCatalogRequestMessage
|
||||
{
|
||||
MessageId = "rudp-schema-host-test",
|
||||
IncludeSchemaJson = false
|
||||
});
|
||||
|
||||
var response = await AwaitWithTimeout(responseCompletion.Task, TimeSpan.FromSeconds(2));
|
||||
await AwaitWithTimeout(serverDone.Task, TimeSpan.FromSeconds(2));
|
||||
|
||||
Assert.That(response.MessageId, Is.EqualTo("rudp-schema-host-test"));
|
||||
Assert.That(response.Schemas.Single().SchemaId, Is.EqualTo("rudp.schema.host"));
|
||||
Assert.That(server.Profile.Transports[0].Protocol, Is.EqualTo("rudp"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RudpSocketTransport_ErgonomicHelpersCarryNamedChannels()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue