net: enforce 1152B RUDP wire MTU cap, skip self-walker in P1, scrub occluded drop picker

This commit is contained in:
Meta Vens 2026-12-05 09:49:58 +01:00
commit 9081d2bb5c
4 changed files with 102 additions and 20 deletions

View file

@ -113,7 +113,7 @@ namespace RustToDust.Net
RemoteEndPoint = serverEndPoint,
ConnectionId = 0x52544431,
ResendDelayMs = 100,
MaxFragmentBytes = SnapshotCodec.MaxLatestDatagramBytes,
MaxFragmentBytes = SnapshotCodec.MaxLatestFrameBytes,
MaxPayloadBytes = 256 * 1024,
MaxPendingReliablePackets = 2048
});

View file

@ -92,7 +92,7 @@ namespace RustToDust.Net
Socket = _socket,
ConnectionId = 0x52544431,
ResendDelayMs = 100,
MaxFragmentBytes = SnapshotCodec.MaxLatestDatagramBytes,
MaxFragmentBytes = SnapshotCodec.MaxLatestFrameBytes,
MaxPayloadBytes = 256 * 1024,
MaxPendingReliablePackets = 2048
});
@ -704,12 +704,14 @@ namespace RustToDust.Net
}
break;
case DropPickedUpEvent dp:
if (dp.ContenderId == obsId ||
_session.IsEntityInPeerPvs(obsId, true, dp.ContenderId) ||
peer.SentDropIds.Contains(dp.DropId))
bool pickerInPvs = dp.ContenderId == obsId ||
_session.IsEntityInPeerPvs(obsId, true, dp.ContenderId);
if (pickerInPvs || peer.SentDropIds.Contains(dp.DropId))
{
peer.SentDropIds.Remove(dp.DropId);
filtered.Add(dp);
filtered.Add(pickerInPvs
? dp
: new DropPickedUpEvent { DropId = dp.DropId, ContenderId = -1 });
}
break;
case TerrainStampEvent stamp:
@ -818,10 +820,6 @@ namespace RustToDust.Net
if (c.Id == obsId)
{
pvsMask |= (ushort)(1 << c.Id);
if (c.Grid.Spec.IsWalker && _session.TrySampleInterpolatedWalkerPose(true, c.Id, 1f, out var lw))
{
walkerCandidates.Add((0f, new NetWalkerEntry { IsContender = true, EntityId = (byte)c.Id, Snapshot = lw }));
}
continue;
}

View file

@ -133,9 +133,12 @@ namespace RustToDust.Net
public const string SchemaEventsId = "rtd.evt";
public const string BodyMatchId = "m";
// CultNetRudpPacketCodec fixed header (36) + "latest" (6) = 42 bytes
public const int CultNetRudpLatestOverheadBytes = 42;
// CultMeshRealtimeWireProtocol fixed header (37) + "latest" (6) + "rtd.snap" (8) + "m" (1) = 52 bytes
public const int CultMeshLatestOverheadBytes = 52;
public const int MaxLatestPayloadBytes = MaxLatestDatagramBytes - CultMeshLatestOverheadBytes; // 1100 bytes
public const int MaxLatestFrameBytes = MaxLatestDatagramBytes - CultNetRudpLatestOverheadBytes; // 1110 bytes
public const int MaxLatestPayloadBytes = MaxLatestFrameBytes - CultMeshLatestOverheadBytes; // 1058 bytes
public static byte[] WrapLatest(ReadOnlySpan<byte> payload, long producerEpoch, long sequence, string schemaId = SchemaLatestId)
{
@ -150,10 +153,10 @@ namespace RustToDust.Net
Payload = payload.ToArray()
};
byte[] encoded = CultMeshRealtimeWireProtocol.EncodeFrame(frame);
if (encoded.Length > MaxLatestDatagramBytes)
if (encoded.Length > MaxLatestFrameBytes)
{
throw new InvalidOperationException(
$"Latest packet exceeded {MaxLatestDatagramBytes}B MTU cap ({encoded.Length}B).");
$"Latest frame exceeded {MaxLatestFrameBytes}B cap ({encoded.Length}B; UDP wire cap {MaxLatestDatagramBytes}B).");
}
return encoded;
}

View file

@ -5,6 +5,7 @@ using System.Net;
using System.Numerics;
using System.Threading;
using GameCult.Mesh;
using GameCult.Networking;
using RustToDust.Core;
using RustToDust.Net;
using RustToDust.Ymir;
@ -93,12 +94,23 @@ namespace RustToDust.Tests
byte[] pIn = SnapshotCodec.EncodeClientInput(new ClientInputPacket(1, 99, 1234, new PlayerInput(1f, -0.5f, 10f, 20f, true, true, false, 2f, 0.5f)));
byte[] wIn = SnapshotCodec.WrapLatest(pIn, 1, 4, SnapshotCodec.SchemaInputId);
_output.WriteLine($"Max wire bytes: Packet1={w1.Length}B, Packet2={w2.Length}B, Packet3={w3.Length}B, Input={wIn.Length}B");
byte[] udp1 = EncodeRudpLatestDatagram(w1, 1);
byte[] udp2 = EncodeRudpLatestDatagram(w2, 2);
byte[] udp3 = EncodeRudpLatestDatagram(w3, 3);
byte[] udpIn = EncodeRudpLatestDatagram(wIn, 4);
Assert.True(w1.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Packet1 {w1.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
Assert.True(w2.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Packet2 {w2.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
Assert.True(w3.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Packet3 {w3.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
Assert.True(wIn.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Input {wIn.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
_output.WriteLine($"Max frame bytes: Packet1={w1.Length}B, Packet2={w2.Length}B, Packet3={w3.Length}B, Input={wIn.Length}B");
_output.WriteLine($"Max UDP wire bytes: Packet1={udp1.Length}B, Packet2={udp2.Length}B, Packet3={udp3.Length}B, Input={udpIn.Length}B");
Assert.True(w1.Length <= SnapshotCodec.MaxLatestFrameBytes, $"Packet1 frame {w1.Length}B > {SnapshotCodec.MaxLatestFrameBytes}B");
Assert.True(w2.Length <= SnapshotCodec.MaxLatestFrameBytes, $"Packet2 frame {w2.Length}B > {SnapshotCodec.MaxLatestFrameBytes}B");
Assert.True(w3.Length <= SnapshotCodec.MaxLatestFrameBytes, $"Packet3 frame {w3.Length}B > {SnapshotCodec.MaxLatestFrameBytes}B");
Assert.True(wIn.Length <= SnapshotCodec.MaxLatestFrameBytes, $"Input frame {wIn.Length}B > {SnapshotCodec.MaxLatestFrameBytes}B");
Assert.True(udp1.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Packet1 UDP {udp1.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
Assert.True(udp2.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Packet2 UDP {udp2.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
Assert.True(udp3.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Packet3 UDP {udp3.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
Assert.True(udpIn.Length <= SnapshotCodec.MaxLatestDatagramBytes, $"Input UDP {udpIn.Length}B > {SnapshotCodec.MaxLatestDatagramBytes}B");
var decoded = SnapshotCodec.DecodeCorePvs(SnapshotCodec.Unwrap(w1).Payload.Span);
Assert.Equal(12, decoded.Contenders.Count);
@ -669,8 +681,10 @@ namespace RustToDust.Tests
byte[] p2 = SnapshotCodec.EncodeSpectatorOrbitHalf(1, 7, 0, packet2List);
byte[] w2 = SnapshotCodec.WrapLatest(p2, 1, 2);
Assert.True(w1.Length <= SnapshotCodec.MaxLatestDatagramBytes);
Assert.True(w2.Length <= SnapshotCodec.MaxLatestDatagramBytes);
Assert.True(w1.Length <= SnapshotCodec.MaxLatestFrameBytes);
Assert.True(w2.Length <= SnapshotCodec.MaxLatestFrameBytes);
Assert.True(EncodeRudpLatestDatagram(w1, 1).Length <= SnapshotCodec.MaxLatestDatagramBytes);
Assert.True(EncodeRudpLatestDatagram(w2, 2).Length <= SnapshotCodec.MaxLatestDatagramBytes);
var dec1 = SnapshotCodec.DecodeCorePvs(SnapshotCodec.Unwrap(w1).Payload.Span);
var dec2 = SnapshotCodec.DecodeSpectatorOrbitHalf(SnapshotCodec.Unwrap(w2).Payload.Span);
@ -727,6 +741,73 @@ namespace RustToDust.Tests
Assert.Equal(0, host.ConnectedHumanCount);
}
[Fact]
public void Cut5cFix6_Dense12QuadWalkersAndExemplar_OmitsSelfWalkerAndDeliversExemplar()
{
using var host = new ServerMatchHost(IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Desert, maxHumans: 12, autoRestartSec: 0);
var ep = new IPEndPoint(IPAddress.Loopback, host.BoundPort);
using var client = new RemoteArenaSession(ep, "QuadLead");
long nowMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Assert.True(PumpUntil(host, new[] { client }, () => client.IsJoined, ref nowMs, 3000));
int slot = client.LocalContenderId;
// Place all 12 contenders as Bastion quad walkers in a tight open cluster around the Exemplar
var exemplar = host.Session.State.Spectators.Spectators.First(s => s.Tier == SpectatorTier.Exemplar && s.Alive);
exemplar.Stance = SpectatorStance.Provoked;
exemplar.TargetContenderId = slot;
for (int i = 0; i < MatchState.ContenderCount; i++)
{
host.Session.State.Contenders[i].Grid.SwapChassis(Catalog.Bastion);
float angle = i * (MathF.PI * 2f / MatchState.ContenderCount);
float dist = i == slot ? 12f : 28f + i * 2f;
host.Session.SetContenderPose(i, exemplar.X + MathF.Cos(angle) * dist, exemplar.Z + MathF.Sin(angle) * dist, angle + MathF.PI);
}
DecodedCorePvsSnapshot? lastCore = null;
int maxUdpBytes = 0;
client.DebugInboundLatestDropFilter = payload =>
{
if (SnapshotCodec.TryDecodeCorePvs(payload, out var core))
{
lastCore = core;
byte[] frame = SnapshotCodec.WrapLatest(payload, 1, core.SnapshotSeq);
maxUdpBytes = Math.Max(maxUdpBytes, EncodeRudpLatestDatagram(frame, core.SnapshotSeq).Length);
}
return false;
};
for (int t = 0; t < 20; t++)
{
nowMs += 16;
host.StepOnce(nowMs);
client.BeginRenderFrame(1.0 / 60.0, 1f);
Thread.Sleep(1);
}
byte exemplarIdx = (byte)host.Session.State.Spectators.Spectators.Select((s, idx) => (s, idx)).First(x => ReferenceEquals(x.s, exemplar)).idx;
Assert.NotNull(lastCore);
_output.WriteLine($"Cut5cFix6: maxUdpBytes={maxUdpBytes}B, walkers=[{string.Join(",", lastCore!.Walkers.Select(w => $"{(w.IsContender ? 'C' : 'S')}{w.EntityId}"))}]");
Assert.True(maxUdpBytes > 0 && maxUdpBytes <= SnapshotCodec.MaxLatestDatagramBytes,
$"Packet 1 UDP datagram must be <= {SnapshotCodec.MaxLatestDatagramBytes}B, got {maxUdpBytes}B");
Assert.DoesNotContain(lastCore.Walkers, w => w.IsContender && w.EntityId == slot);
Assert.Contains(lastCore.Walkers, w => !w.IsContender && w.EntityId == exemplarIdx);
Assert.True(client.TrySampleInterpolatedWalkerPose(false, exemplar.Id, 1f, out _),
"Client must receive Exemplar walker snapshot even under dense 12 quad-walker contention");
}
private static byte[] EncodeRudpLatestDatagram(byte[] frameBytes, uint sequence) =>
CultNetRudpPacketCodec.Encode(new CultNetRudpPacket
{
PacketType = CultNetRudpPacketType.Data,
ConnectionId = 0x52544431,
Sequence = sequence,
ChannelId = SnapshotCodec.ChannelLatest,
Sequenced = true,
Payload = frameBytes
});
private static bool PumpUntil(
ServerMatchHost host,
RemoteArenaSession[] clients,