Merge origin/main into hands/ack-cutD: Cut 3 flow window, RUDP client bind
Import lists in cultnet-ts index.ts and operation-service.ts and the cultnet-py test block conflicted; both sides are kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
2e94dc3aa3
25 changed files with 2260 additions and 87 deletions
|
|
@ -537,8 +537,12 @@ namespace GameCult.Mesh
|
|||
/// </summary>
|
||||
public sealed class CultMeshRudpSocketOptions
|
||||
{
|
||||
/// <summary>Gets or sets the local bind host.</summary>
|
||||
public string BindHost { get; set; } = "0.0.0.0";
|
||||
/// <summary>
|
||||
/// Gets or sets the local bind host. Unset, a server binds every IPv4 interface, and a
|
||||
/// client binds loopback for a loopback endpoint and the unspecified address of the
|
||||
/// endpoint's family otherwise, so it can reach a remote host.
|
||||
/// </summary>
|
||||
public string? BindHost { get; set; }
|
||||
/// <summary>Gets or sets the local bind port.</summary>
|
||||
public int BindPort { get; set; }
|
||||
/// <summary>Gets or sets a caller-owned bound socket.</summary>
|
||||
|
|
@ -2933,7 +2937,7 @@ namespace GameCult.Mesh
|
|||
return new CultNetRudpSocketTransportConnection(new CultNetRudpSocketTransportOptions
|
||||
{
|
||||
RuntimeId = runtimeId,
|
||||
Socket = options.Socket ?? BindRudpSocket(options.BindHost, options.BindPort),
|
||||
Socket = options.Socket ?? BindRudpSocket(ResolveRudpAddress(options.BindHost ?? "0.0.0.0"), options.BindPort),
|
||||
Mode = CultNetRudpSocketMode.Server,
|
||||
ConnectionId = connectionId,
|
||||
InitialSequence = options.InitialSequence,
|
||||
|
|
@ -2957,12 +2961,16 @@ namespace GameCult.Mesh
|
|||
{
|
||||
if (endpoint == null) throw new ArgumentNullException(nameof(endpoint));
|
||||
options ??= new CultMeshRudpSocketOptions();
|
||||
var remote = ResolveRudpAddress(endpoint.Host);
|
||||
var bindAddress = options.BindHost == null
|
||||
? RudpClientBindAddress(remote)
|
||||
: ResolveRudpAddress(options.BindHost);
|
||||
return new CultNetRudpSocketTransportConnection(new CultNetRudpSocketTransportOptions
|
||||
{
|
||||
RuntimeId = runtimeId,
|
||||
Socket = options.Socket ?? BindRudpSocket(options.BindHost, options.BindPort),
|
||||
Socket = options.Socket ?? BindRudpSocket(bindAddress, options.BindPort),
|
||||
Mode = CultNetRudpSocketMode.Client,
|
||||
RemoteEndPoint = new IPEndPoint(ResolveRudpAddress(endpoint.Host), endpoint.Port),
|
||||
RemoteEndPoint = new IPEndPoint(remote, endpoint.Port),
|
||||
ConnectionId = connectionId,
|
||||
InitialSequence = options.InitialSequence,
|
||||
ResendDelayMs = options.ResendDelayMs,
|
||||
|
|
@ -3234,9 +3242,21 @@ namespace GameCult.Mesh
|
|||
return CultNetLocal.ConnectClient(host, port, security, configureClient);
|
||||
}
|
||||
|
||||
private static Socket BindRudpSocket(string host, int port)
|
||||
// A socket bound to loopback cannot send off the host on Windows, so a client keeps
|
||||
// loopback only when its peer is on loopback.
|
||||
private static IPAddress RudpClientBindAddress(IPAddress remote)
|
||||
{
|
||||
var ipv6 = remote.AddressFamily == AddressFamily.InterNetworkV6;
|
||||
if (IPAddress.IsLoopback(remote))
|
||||
{
|
||||
return ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback;
|
||||
}
|
||||
|
||||
return ipv6 ? IPAddress.IPv6Any : IPAddress.Any;
|
||||
}
|
||||
|
||||
private static Socket BindRudpSocket(IPAddress address, int port)
|
||||
{
|
||||
var address = ResolveRudpAddress(host);
|
||||
var socket = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
|
||||
socket.Bind(new IPEndPoint(address, port));
|
||||
socket.ReceiveTimeout = 20;
|
||||
|
|
|
|||
|
|
@ -893,6 +893,10 @@ namespace GameCult.Networking
|
|||
{
|
||||
/// <summary>Maximum reliable packets admitted to the wire before acknowledgements advance the window.</summary>
|
||||
public const int ReliableSendWindowPackets = 32;
|
||||
// Flow window: a reliable packet is admitted only while its sequence is at most FlowWindowSequences above the
|
||||
// lowest unacked one and the payload above that sequence stays within FlowWindowBytes.
|
||||
private const uint FlowWindowSequences = 1023;
|
||||
private const int FlowWindowBytes = 4 * 1024 * 1024;
|
||||
private const int ReceivedSequenceWindow = 4096;
|
||||
// How long a client keeps retransmitting a Connect nobody answered before it abandons that
|
||||
// attempt for a fresh one. A Connect the server answers with an Ack, never an Accept, is one
|
||||
|
|
@ -943,6 +947,9 @@ namespace GameCult.Networking
|
|||
private readonly Dictionary<string, uint> _latestSequencedByChannel = new Dictionary<string, uint>(StringComparer.Ordinal);
|
||||
private readonly object _pendingReliableGate = new object();
|
||||
private readonly Dictionary<uint, PendingReliablePacket> _pendingReliable = new Dictionary<uint, PendingReliablePacket>();
|
||||
// Payload sizes of acknowledged reliable sequences above the lowest unacknowledged one. The receiver still
|
||||
// holds those bytes behind the gap, so the flow window keeps counting them until the lowest sequence passes them.
|
||||
private readonly Dictionary<uint, int> _ackedAboveLowest = new Dictionary<uint, int>();
|
||||
private readonly Queue<CultNetRudpPacket> _queuedReliable = new Queue<CultNetRudpPacket>();
|
||||
// Every reliable sequence up to and including this one has been received since the peer state
|
||||
// was last reset. Only the handshake seeds it: the peer's Connect on the accepting side, the
|
||||
|
|
@ -1164,7 +1171,7 @@ namespace GameCult.Networking
|
|||
options ??= new CultNetRudpSendOptions();
|
||||
lock (_pendingReliableGate)
|
||||
{
|
||||
if (options.Reliable && _pendingReliable.Count >= ReliableSendWindowPackets)
|
||||
if (options.Reliable && (_queuedReliable.Count > 0 || !WindowAdmits(_nextSequence, payload.Length)))
|
||||
throw new InvalidOperationException("RUDP reliable send window is full; receive acknowledgements before sending.");
|
||||
}
|
||||
return SendMany(channelId, payload, options).First();
|
||||
|
|
@ -1428,6 +1435,7 @@ namespace GameCult.Networking
|
|||
lock (_pendingReliableGate)
|
||||
{
|
||||
_pendingReliable.Clear();
|
||||
_ackedAboveLowest.Clear();
|
||||
_queuedReliable.Clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -1628,17 +1636,39 @@ namespace GameCult.Networking
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a reliable packet may go on the wire now: the window has a slot, and its sequence and the
|
||||
/// payload above the lowest unacked sequence stay inside the flow window. With nothing pending, any
|
||||
/// packet is admissible. Callers hold the pending gate.
|
||||
/// </summary>
|
||||
private bool WindowAdmits(uint sequence, int payloadLength)
|
||||
{
|
||||
if (_pendingReliable.Count >= ReliableSendWindowPackets)
|
||||
return false;
|
||||
if (_pendingReliable.Count == 0)
|
||||
return true;
|
||||
var lowest = _pendingReliable.Keys.Min();
|
||||
long bytesAbove = 0;
|
||||
foreach (var pending in _pendingReliable)
|
||||
{
|
||||
if (pending.Key > lowest)
|
||||
bytesAbove += pending.Value.Packet.Payload.Length;
|
||||
}
|
||||
foreach (var length in _ackedAboveLowest.Values)
|
||||
bytesAbove += length;
|
||||
return sequence - lowest <= FlowWindowSequences && bytesAbove + payloadLength <= FlowWindowBytes;
|
||||
}
|
||||
|
||||
private IReadOnlyList<CultNetRudpPacket> AdmitReliablePackets(
|
||||
IReadOnlyList<CultNetRudpPacket> packets,
|
||||
long nowMs)
|
||||
{
|
||||
lock (_pendingReliableGate)
|
||||
{
|
||||
var available = Math.Max(0, ReliableSendWindowPackets - _pendingReliable.Count);
|
||||
var ready = new List<CultNetRudpPacket>(Math.Min(available, packets.Count));
|
||||
var ready = new List<CultNetRudpPacket>();
|
||||
foreach (var packet in packets)
|
||||
{
|
||||
if (ready.Count < available)
|
||||
if (_queuedReliable.Count == 0 && WindowAdmits(packet.Sequence, packet.Payload.Length))
|
||||
{
|
||||
_pendingReliable[packet.Sequence] = new PendingReliablePacket
|
||||
{
|
||||
|
|
@ -1660,9 +1690,9 @@ namespace GameCult.Networking
|
|||
{
|
||||
lock (_pendingReliableGate)
|
||||
{
|
||||
var available = Math.Max(0, ReliableSendWindowPackets - _pendingReliable.Count);
|
||||
var ready = new List<CultNetRudpPacket>(Math.Min(available, _queuedReliable.Count));
|
||||
while (ready.Count < available && _queuedReliable.Count > 0)
|
||||
var ready = new List<CultNetRudpPacket>();
|
||||
while (_queuedReliable.Count > 0
|
||||
&& WindowAdmits(_queuedReliable.Peek().Sequence, _queuedReliable.Peek().Payload.Length))
|
||||
{
|
||||
var packet = _queuedReliable.Dequeue();
|
||||
_pendingReliable[packet.Sequence] = new PendingReliablePacket
|
||||
|
|
@ -1696,17 +1726,36 @@ namespace GameCult.Networking
|
|||
{
|
||||
lock (_pendingReliableGate)
|
||||
{
|
||||
_pendingReliable.Remove(packet.Ack);
|
||||
Acknowledge(packet.Ack);
|
||||
for (var bit = 0; bit < 32; bit++)
|
||||
{
|
||||
if ((packet.AckMask & (1u << bit)) != 0 && packet.Ack > bit)
|
||||
{
|
||||
_pendingReliable.Remove(packet.Ack - (uint)bit - 1);
|
||||
Acknowledge(packet.Ack - (uint)bit - 1);
|
||||
}
|
||||
}
|
||||
// Acknowledged sizes stop counting once the lowest unacknowledged sequence passes them, and all
|
||||
// of them stop when nothing is unacknowledged.
|
||||
if (_pendingReliable.Count == 0)
|
||||
{
|
||||
_ackedAboveLowest.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
var lowest = _pendingReliable.Keys.Min();
|
||||
foreach (var sequence in _ackedAboveLowest.Keys.Where(sequence => sequence <= lowest).ToArray())
|
||||
_ackedAboveLowest.Remove(sequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Callers hold the pending gate.
|
||||
private void Acknowledge(uint sequence)
|
||||
{
|
||||
if (_pendingReliable.Remove(sequence, out var pending))
|
||||
_ackedAboveLowest[sequence] = pending.Packet.Payload.Length;
|
||||
}
|
||||
|
||||
private static bool PacketAcknowledges(CultNetRudpPacket packet, uint sequence)
|
||||
{
|
||||
if (packet.Ack == sequence)
|
||||
|
|
|
|||
Loading…
Reference in a new issue