Ports the fragment layer of the AC UDP protocol. A UDP packet's body is
zero or more message fragments back-to-back; a logical GameMessage that
doesn't fit in ~448 bytes gets split across multiple fragments sharing
the same Id with differing Index values. The assembler handles
reassembly across arbitrary arrival ordering and duplicate fragments.
Added (all reimplemented from ACE's AGPL reference, see NOTICE.md):
- Packets/MessageFragmentHeader.cs: 16-byte fragment header struct
with Pack/Unpack, constants for MaxFragmentSize (464) and
MaxFragmentDataSize (448). Bit-layout doc comment documents what
each field is for.
- Packets/MessageFragment.cs: readonly record struct bundling a
header with its payload bytes; TryParse(source) parses one fragment
from the start of a buffer and returns (fragment, consumed) for
incremental parsing of multi-fragment packets. Refuses to parse
fragments with impossible TotalSize (too small for header, too
large for the 464-byte max, or larger than the source buffer).
- Packets/FragmentAssembler.cs: buffers partial messages keyed by
fragment Id. Ingest(frag, out queue) returns the assembled byte[]
when the last fragment arrives, null while still waiting. Key
correctness properties, all tested:
* Single-fragment (Count=1) shortcut releases with no buffering
* Out-of-order arrival (e.g. 2, 0, 1) releases on last arrival
and assembles in INDEX order, not arrival order
* Duplicate-fragment idempotence (re-sending same index is a no-op)
* Missing fragments stay buffered; DropAll() forcibly clears them
* Two independent messages can be assembled in parallel without
interfering
* messageQueue captured from first-arriving fragment (it's a
property of the logical message, not individual fragments)
Tests (17 new, 37 total in net project, 114 across both test projects):
- MessageFragmentHeader (4): pack/unpack round-trip, little-endian
wire format, constants, size-check throw
- MessageFragment (6): complete parse, insufficient header, oversized
TotalSize, undersized TotalSize, incomplete body, two-back-to-back
incremental parse
- FragmentAssembler (7): single-fragment, in-order 3-fragment,
out-of-order 3-fragment (tests index-order assembly), duplicate
idempotence, missing-fragment buffered, two parallel messages,
DropAll
Phase 4.4 (GameMessage reader + opcode handlers) next.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using AcDream.Core.Net.Packets;
|
|
|
|
namespace AcDream.Core.Net.Tests.Packets;
|
|
|
|
public class MessageFragmentHeaderTests
|
|
{
|
|
[Fact]
|
|
public void PackUnpack_RoundTrip_PreservesAllFields()
|
|
{
|
|
var original = new MessageFragmentHeader
|
|
{
|
|
Sequence = 0x11223344u,
|
|
Id = 0x80000010u,
|
|
Count = 3,
|
|
TotalSize = 64,
|
|
Index = 2,
|
|
Queue = 0x0A,
|
|
};
|
|
|
|
Span<byte> buf = stackalloc byte[MessageFragmentHeader.Size];
|
|
original.Pack(buf);
|
|
var decoded = MessageFragmentHeader.Unpack(buf);
|
|
|
|
Assert.Equal(original.Sequence, decoded.Sequence);
|
|
Assert.Equal(original.Id, decoded.Id);
|
|
Assert.Equal(original.Count, decoded.Count);
|
|
Assert.Equal(original.TotalSize, decoded.TotalSize);
|
|
Assert.Equal(original.Index, decoded.Index);
|
|
Assert.Equal(original.Queue, decoded.Queue);
|
|
}
|
|
|
|
[Fact]
|
|
public void Pack_WritesLittleEndianWireFormat()
|
|
{
|
|
var h = new MessageFragmentHeader
|
|
{
|
|
Sequence = 0x04030201u, // 01 02 03 04
|
|
Id = 0x08070605u, // 05 06 07 08
|
|
Count = 0x0A09, // 09 0A
|
|
TotalSize = 0x0C0B, // 0B 0C
|
|
Index = 0x0E0D, // 0D 0E
|
|
Queue = 0x100F, // 0F 10
|
|
};
|
|
Span<byte> buf = stackalloc byte[16];
|
|
h.Pack(buf);
|
|
|
|
byte[] expected =
|
|
{
|
|
0x01, 0x02, 0x03, 0x04,
|
|
0x05, 0x06, 0x07, 0x08,
|
|
0x09, 0x0A,
|
|
0x0B, 0x0C,
|
|
0x0D, 0x0E,
|
|
0x0F, 0x10,
|
|
};
|
|
Assert.Equal(expected, buf.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void Constants_MatchAcProtocolLimits()
|
|
{
|
|
Assert.Equal(16, MessageFragmentHeader.Size);
|
|
Assert.Equal(464, MessageFragmentHeader.MaxFragmentSize);
|
|
Assert.Equal(448, MessageFragmentHeader.MaxFragmentDataSize);
|
|
}
|
|
|
|
[Fact]
|
|
public void Unpack_InsufficientData_Throws()
|
|
{
|
|
Assert.Throws<ArgumentException>(() => MessageFragmentHeader.Unpack(new byte[15]));
|
|
}
|
|
}
|