2026-06-17 14:48:53 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2026-07-12 10:53:05 +01:00
|
|
|
using System.Collections.Generic;
|
2026-06-17 14:48:53 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Threading.Tasks;
|
2026-09-23 07:15:43 +02:00
|
|
|
using GameCult.Logging;
|
2026-06-17 14:48:53 +01:00
|
|
|
|
|
|
|
|
namespace GameCult.Networking
|
|
|
|
|
{
|
2026-07-14 03:26:43 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Result of a bounded non-blocking RUDP schema-server drain.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly struct RudpCultNetSchemaPollResult
|
|
|
|
|
{
|
|
|
|
|
public RudpCultNetSchemaPollResult(int transportItemsConsumed, int messagesDispatched)
|
|
|
|
|
{
|
|
|
|
|
TransportItemsConsumed = transportItemsConsumed;
|
|
|
|
|
MessagesDispatched = messagesDispatched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets transport work consumed, including ACK and connection control packets.</summary>
|
|
|
|
|
public int TransportItemsConsumed { get; }
|
|
|
|
|
/// <summary>Gets application schema messages dispatched.</summary>
|
|
|
|
|
public int MessagesDispatched { get; }
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:48:53 +01:00
|
|
|
/// <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>
|
2026-09-30 09:11:52 +02:00
|
|
|
/// Gets or sets the first local packet sequence for each accepted peer. Unset, each peer's
|
|
|
|
|
/// session draws its own at random.
|
2026-06-17 14:48:53 +01:00
|
|
|
/// </summary>
|
2026-09-30 09:11:52 +02:00
|
|
|
public uint? InitialSequence { get; set; }
|
2026-06-17 14:48:53 +01:00
|
|
|
/// <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>
|
2026-07-19 18:50:22 +01:00
|
|
|
public sealed class RudpCultNetSchemaServerPeer : ICultNetSchemaServerPeer, ICultNetSchemaServerPeerLocation
|
2026-06-17 14:48:53 +01:00
|
|
|
{
|
|
|
|
|
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>
|
2026-07-19 06:40:05 +01:00
|
|
|
public sealed class RudpCultNetSchemaServer : ICultNetSchemaServer, ICultNetSchemaServerPeerLifecycle, IDisposable
|
2026-06-17 14:48:53 +01:00
|
|
|
{
|
|
|
|
|
private readonly Socket _socket;
|
|
|
|
|
private readonly CultNetRudpSocketTransportServer _transport;
|
|
|
|
|
private readonly ConcurrentDictionary<Type, Delegate> _handlers = new();
|
2026-06-21 16:31:50 +01:00
|
|
|
private readonly ConcurrentDictionary<Delegate, Delegate> _schemaPeerAdapters = new();
|
2026-06-17 14:48:53 +01:00
|
|
|
private bool _disposed;
|
2026-09-23 07:15:43 +02:00
|
|
|
private ILogger _logger = new NullLogger();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the logger used by the server. R-AM: the dispatch backstop in DispatchAsync
|
|
|
|
|
/// logs every untyped fault through this instead of swallowing it silently.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ILogger Logger
|
|
|
|
|
{
|
|
|
|
|
get => _logger;
|
|
|
|
|
set => _logger = value ?? new NullLogger();
|
|
|
|
|
}
|
2026-06-17 14:48:53 +01:00
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
});
|
2026-07-19 06:40:05 +01:00
|
|
|
_transport.PeerDisconnected += OnTransportPeerDisconnected;
|
2026-06-17 14:48:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
2026-07-12 10:53:05 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the currently tracked RUDP peers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyCollection<CultNetRudpSocketServerPeer> Peers => _transport.Peers;
|
|
|
|
|
|
2026-07-19 06:40:05 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public event Action<ICultNetSchemaServerPeer>? PeerDisconnected;
|
|
|
|
|
|
2026-06-17 14:48:53 +01:00
|
|
|
/// <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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 16:31:50 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void OnCultNet<TMessage>(Func<TMessage, ICultNetSchemaServerPeer, Task> callback)
|
|
|
|
|
where TMessage : ICultNetSchemaMessage
|
|
|
|
|
{
|
|
|
|
|
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
|
|
|
|
Func<TMessage, RudpCultNetSchemaServerPeer, Task> adapter = (message, peer) => callback(message, peer);
|
|
|
|
|
_schemaPeerAdapters[callback] = adapter;
|
|
|
|
|
OnCultNet(adapter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void RemoveCultNetMessageListener<TMessage>(Delegate callback)
|
|
|
|
|
where TMessage : ICultNetSchemaMessage
|
|
|
|
|
{
|
|
|
|
|
if (_schemaPeerAdapters.TryRemove(callback, out var adapter))
|
|
|
|
|
{
|
|
|
|
|
callback = adapter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_handlers.TryGetValue(typeof(TMessage), out var current))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var next = Delegate.Remove(current, callback);
|
|
|
|
|
if (next == null)
|
|
|
|
|
{
|
|
|
|
|
_handlers.TryRemove(typeof(TMessage), out _);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_handlers[typeof(TMessage)] = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 14:48:53 +01:00
|
|
|
/// <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()
|
|
|
|
|
{
|
2026-07-14 03:26:43 +01:00
|
|
|
if (!_transport.TryReceiveOnce(out var delivered) || delivered == null)
|
2026-06-17 14:48:53 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 03:26:43 +01:00
|
|
|
return await DispatchAsync(delivered).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Drains up to <paramref name="maxTransportItems"/> immediately available transport work items.
|
|
|
|
|
/// Control packets count as progress so callers only idle when the transport is actually empty.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<RudpCultNetSchemaPollResult> PollAvailableAsync(int maxTransportItems)
|
|
|
|
|
{
|
|
|
|
|
if (maxTransportItems <= 0) throw new ArgumentOutOfRangeException(nameof(maxTransportItems));
|
|
|
|
|
var consumed = 0;
|
|
|
|
|
var dispatched = 0;
|
|
|
|
|
while (consumed < maxTransportItems && _transport.TryReceiveOnce(out var delivered))
|
|
|
|
|
{
|
|
|
|
|
consumed++;
|
|
|
|
|
if (delivered != null && await DispatchAsync(delivered).ConfigureAwait(false))
|
|
|
|
|
dispatched++;
|
|
|
|
|
}
|
|
|
|
|
return new RudpCultNetSchemaPollResult(consumed, dispatched);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> DispatchAsync(CultNetRudpSocketServerFrame delivered)
|
|
|
|
|
{
|
|
|
|
|
if (!string.Equals(delivered.Frame.ChannelId, "schema", StringComparison.Ordinal))
|
|
|
|
|
return false;
|
|
|
|
|
|
2026-06-17 14:48:53 +01:00
|
|
|
var message = CultNetSchemaMessageSerialization.Deserialize(delivered.Frame.Payload);
|
|
|
|
|
if (!_handlers.TryGetValue(message.GetType(), out var handler) || handler == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
R-AG/R-AK: checked C# cursor-length arithmetic, a dispatch backstop, and the redundant guard
R-AG. ReadLengthPrefixedFields tested pos + length > bytes.Length in int
math; a length prefix near int.MaxValue overflowed the unchecked add,
slipped past the guard, and Encoding.UTF8.GetString threw
ArgumentOutOfRangeException instead of this method returning null the
way every other malformed shape already does. Promotes to long before
the add, which cannot itself overflow (both operands stay far under
long's range), so the guard sees the true sum.
CultNetRudpSchemaServer.DispatchAsync gains a catch around the handler
invocation: HandleSnapshotRequestV1Async already answers
cursor_invalid/selection_invalid/reference_outside_target for its three
typed exceptions, but nothing stopped an untyped one from an
unanticipated bug reaching DynamicInvoke's caller and stalling the poll
loop for every other peer. Answers nothing on the wire (an exception
this generic carries no typed refusal to report) and drops the one
malformed dispatch, the same as an unregistered handler already does.
Hostile-cursor tests (CultNetSelectionFixBatchTests.cs) feed
CultNetSelectionCursor.Parse a length prefix near int.MaxValue (C#'s own
proven overflow), near usize::MAX (Rust's proven overflow, fed here too
per the ruling), wider than any integer type, and negative-looking -
each asserted to throw the typed CultNetSelectionCursorException.
R-AK. Deletes ReferenceMembers's `!member.IsReference` guard.
CultCache.ReferencesOf (CultCache.cs:262-278) already searches only its
own reference members for a role match and returns empty for anything
else; index aliases are unique per descriptor, so a data member's own
role string can never coincide with a reference member's. Verified
before deleting: traced ReferencesOf's role lookup directly rather than
trusting the ruling's premise. A second copy of the same exclusion here
was duplicate authority, not a second decision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 04:44:09 +02:00
|
|
|
// R-AG: a handler is expected to catch its own typed refusals (HandleSnapshotRequestV1Async
|
|
|
|
|
// catches the three selection exceptions and answers cursor_invalid/selection_invalid/
|
2026-09-23 07:15:43 +02:00
|
|
|
// reference_outside_target on the wire, R-AM) - this catch is the backstop for whatever a
|
R-AG/R-AK: checked C# cursor-length arithmetic, a dispatch backstop, and the redundant guard
R-AG. ReadLengthPrefixedFields tested pos + length > bytes.Length in int
math; a length prefix near int.MaxValue overflowed the unchecked add,
slipped past the guard, and Encoding.UTF8.GetString threw
ArgumentOutOfRangeException instead of this method returning null the
way every other malformed shape already does. Promotes to long before
the add, which cannot itself overflow (both operands stay far under
long's range), so the guard sees the true sum.
CultNetRudpSchemaServer.DispatchAsync gains a catch around the handler
invocation: HandleSnapshotRequestV1Async already answers
cursor_invalid/selection_invalid/reference_outside_target for its three
typed exceptions, but nothing stopped an untyped one from an
unanticipated bug reaching DynamicInvoke's caller and stalling the poll
loop for every other peer. Answers nothing on the wire (an exception
this generic carries no typed refusal to report) and drops the one
malformed dispatch, the same as an unregistered handler already does.
Hostile-cursor tests (CultNetSelectionFixBatchTests.cs) feed
CultNetSelectionCursor.Parse a length prefix near int.MaxValue (C#'s own
proven overflow), near usize::MAX (Rust's proven overflow, fed here too
per the ruling), wider than any integer type, and negative-looking -
each asserted to throw the typed CultNetSelectionCursorException.
R-AK. Deletes ReferenceMembers's `!member.IsReference` guard.
CultCache.ReferencesOf (CultCache.cs:262-278) already searches only its
own reference members for a role match and returns empty for anything
else; index aliases are unique per descriptor, so a data member's own
role string can never coincide with a reference member's. Verified
before deleting: traced ReferencesOf's role lookup directly rather than
trusting the ruling's premise. A second copy of the same exclusion here
was duplicate authority, not a second decision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 04:44:09 +02:00
|
|
|
// handler does not, so an untyped exception from a malformed/hostile message can never
|
2026-09-23 07:15:43 +02:00
|
|
|
// reach the poll loop and stall the server for every other peer. It still answers nothing
|
|
|
|
|
// on the wire (an exception this generic carries no typed refusal to report) and drops the
|
|
|
|
|
// one malformed dispatch, exactly as an unregistered handler already does just above -
|
|
|
|
|
// R-AM: but it always logs. A backstop that swallows silently is a compensator, and an
|
|
|
|
|
// untested, unlogged one hid the SM-6 ArgumentNullException regression for a whole batch.
|
R-AG/R-AK: checked C# cursor-length arithmetic, a dispatch backstop, and the redundant guard
R-AG. ReadLengthPrefixedFields tested pos + length > bytes.Length in int
math; a length prefix near int.MaxValue overflowed the unchecked add,
slipped past the guard, and Encoding.UTF8.GetString threw
ArgumentOutOfRangeException instead of this method returning null the
way every other malformed shape already does. Promotes to long before
the add, which cannot itself overflow (both operands stay far under
long's range), so the guard sees the true sum.
CultNetRudpSchemaServer.DispatchAsync gains a catch around the handler
invocation: HandleSnapshotRequestV1Async already answers
cursor_invalid/selection_invalid/reference_outside_target for its three
typed exceptions, but nothing stopped an untyped one from an
unanticipated bug reaching DynamicInvoke's caller and stalling the poll
loop for every other peer. Answers nothing on the wire (an exception
this generic carries no typed refusal to report) and drops the one
malformed dispatch, the same as an unregistered handler already does.
Hostile-cursor tests (CultNetSelectionFixBatchTests.cs) feed
CultNetSelectionCursor.Parse a length prefix near int.MaxValue (C#'s own
proven overflow), near usize::MAX (Rust's proven overflow, fed here too
per the ruling), wider than any integer type, and negative-looking -
each asserted to throw the typed CultNetSelectionCursorException.
R-AK. Deletes ReferenceMembers's `!member.IsReference` guard.
CultCache.ReferencesOf (CultCache.cs:262-278) already searches only its
own reference members for a role match and returns empty for anything
else; index aliases are unique per descriptor, so a data member's own
role string can never coincide with a reference member's. Verified
before deleting: traced ReferencesOf's role lookup directly rather than
trusting the ruling's premise. A second copy of the same exclusion here
was duplicate authority, not a second decision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 04:44:09 +02:00
|
|
|
try
|
2026-06-17 14:48:53 +01:00
|
|
|
{
|
R-AG/R-AK: checked C# cursor-length arithmetic, a dispatch backstop, and the redundant guard
R-AG. ReadLengthPrefixedFields tested pos + length > bytes.Length in int
math; a length prefix near int.MaxValue overflowed the unchecked add,
slipped past the guard, and Encoding.UTF8.GetString threw
ArgumentOutOfRangeException instead of this method returning null the
way every other malformed shape already does. Promotes to long before
the add, which cannot itself overflow (both operands stay far under
long's range), so the guard sees the true sum.
CultNetRudpSchemaServer.DispatchAsync gains a catch around the handler
invocation: HandleSnapshotRequestV1Async already answers
cursor_invalid/selection_invalid/reference_outside_target for its three
typed exceptions, but nothing stopped an untyped one from an
unanticipated bug reaching DynamicInvoke's caller and stalling the poll
loop for every other peer. Answers nothing on the wire (an exception
this generic carries no typed refusal to report) and drops the one
malformed dispatch, the same as an unregistered handler already does.
Hostile-cursor tests (CultNetSelectionFixBatchTests.cs) feed
CultNetSelectionCursor.Parse a length prefix near int.MaxValue (C#'s own
proven overflow), near usize::MAX (Rust's proven overflow, fed here too
per the ruling), wider than any integer type, and negative-looking -
each asserted to throw the typed CultNetSelectionCursorException.
R-AK. Deletes ReferenceMembers's `!member.IsReference` guard.
CultCache.ReferencesOf (CultCache.cs:262-278) already searches only its
own reference members for a role match and returns empty for anything
else; index aliases are unique per descriptor, so a data member's own
role string can never coincide with a reference member's. Verified
before deleting: traced ReferencesOf's role lookup directly rather than
trusting the ruling's premise. A second copy of the same exclusion here
was duplicate authority, not a second decision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 04:44:09 +02:00
|
|
|
var result = handler.DynamicInvoke(message, new RudpCultNetSchemaServerPeer(this, delivered.Peer));
|
|
|
|
|
if (result is Task task)
|
|
|
|
|
{
|
|
|
|
|
await task.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-23 07:15:43 +02:00
|
|
|
catch (Exception ex)
|
R-AG/R-AK: checked C# cursor-length arithmetic, a dispatch backstop, and the redundant guard
R-AG. ReadLengthPrefixedFields tested pos + length > bytes.Length in int
math; a length prefix near int.MaxValue overflowed the unchecked add,
slipped past the guard, and Encoding.UTF8.GetString threw
ArgumentOutOfRangeException instead of this method returning null the
way every other malformed shape already does. Promotes to long before
the add, which cannot itself overflow (both operands stay far under
long's range), so the guard sees the true sum.
CultNetRudpSchemaServer.DispatchAsync gains a catch around the handler
invocation: HandleSnapshotRequestV1Async already answers
cursor_invalid/selection_invalid/reference_outside_target for its three
typed exceptions, but nothing stopped an untyped one from an
unanticipated bug reaching DynamicInvoke's caller and stalling the poll
loop for every other peer. Answers nothing on the wire (an exception
this generic carries no typed refusal to report) and drops the one
malformed dispatch, the same as an unregistered handler already does.
Hostile-cursor tests (CultNetSelectionFixBatchTests.cs) feed
CultNetSelectionCursor.Parse a length prefix near int.MaxValue (C#'s own
proven overflow), near usize::MAX (Rust's proven overflow, fed here too
per the ruling), wider than any integer type, and negative-looking -
each asserted to throw the typed CultNetSelectionCursorException.
R-AK. Deletes ReferenceMembers's `!member.IsReference` guard.
CultCache.ReferencesOf (CultCache.cs:262-278) already searches only its
own reference members for a role match and returns empty for anything
else; index aliases are unique per descriptor, so a data member's own
role string can never coincide with a reference member's. Verified
before deleting: traced ReferencesOf's role lookup directly rather than
trusting the ruling's premise. A second copy of the same exclusion here
was duplicate authority, not a second decision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 04:44:09 +02:00
|
|
|
{
|
2026-09-23 07:15:43 +02:00
|
|
|
// DynamicInvoke wraps a synchronously-thrown handler fault in TargetInvocationException,
|
|
|
|
|
// whose own .Message is the generic "Exception has been thrown by the target of an
|
|
|
|
|
// invocation." - log the real fault, not that wrapper.
|
|
|
|
|
var reported = ex is System.Reflection.TargetInvocationException { InnerException: { } inner } ? inner : ex;
|
|
|
|
|
Logger.LogError($"CultNet RUDP schema dispatch failed for {message.GetType().Name}: {reported.Message}");
|
R-AG/R-AK: checked C# cursor-length arithmetic, a dispatch backstop, and the redundant guard
R-AG. ReadLengthPrefixedFields tested pos + length > bytes.Length in int
math; a length prefix near int.MaxValue overflowed the unchecked add,
slipped past the guard, and Encoding.UTF8.GetString threw
ArgumentOutOfRangeException instead of this method returning null the
way every other malformed shape already does. Promotes to long before
the add, which cannot itself overflow (both operands stay far under
long's range), so the guard sees the true sum.
CultNetRudpSchemaServer.DispatchAsync gains a catch around the handler
invocation: HandleSnapshotRequestV1Async already answers
cursor_invalid/selection_invalid/reference_outside_target for its three
typed exceptions, but nothing stopped an untyped one from an
unanticipated bug reaching DynamicInvoke's caller and stalling the poll
loop for every other peer. Answers nothing on the wire (an exception
this generic carries no typed refusal to report) and drops the one
malformed dispatch, the same as an unregistered handler already does.
Hostile-cursor tests (CultNetSelectionFixBatchTests.cs) feed
CultNetSelectionCursor.Parse a length prefix near int.MaxValue (C#'s own
proven overflow), near usize::MAX (Rust's proven overflow, fed here too
per the ruling), wider than any integer type, and negative-looking -
each asserted to throw the typed CultNetSelectionCursorException.
R-AK. Deletes ReferenceMembers's `!member.IsReference` guard.
CultCache.ReferencesOf (CultCache.cs:262-278) already searches only its
own reference members for a role match and returns empty for anything
else; index aliases are unique per descriptor, so a data member's own
role string can never coincide with a reference member's. Verified
before deleting: traced ReferencesOf's role lookup directly rather than
trusting the ruling's premise. A second copy of the same exclusion here
was duplicate authority, not a second decision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 04:44:09 +02:00
|
|
|
return false;
|
2026-06-17 14:48:53 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2026-07-19 06:40:05 +01:00
|
|
|
_transport.PeerDisconnected -= OnTransportPeerDisconnected;
|
2026-06-17 14:48:53 +01:00
|
|
|
_transport.Dispose();
|
|
|
|
|
}
|
2026-07-19 06:40:05 +01:00
|
|
|
|
|
|
|
|
private void OnTransportPeerDisconnected(CultNetRudpSocketServerPeer peer)
|
|
|
|
|
{
|
|
|
|
|
PeerDisconnected?.Invoke(new RudpCultNetSchemaServerPeer(this, peer));
|
|
|
|
|
}
|
2026-06-17 14:48:53 +01:00
|
|
|
}
|
|
|
|
|
}
|