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>
100 lines
3.2 KiB
C#
100 lines
3.2 KiB
C#
using AcDream.Core.Net.Packets;
|
|
|
|
namespace AcDream.Core.Net.Tests.Packets;
|
|
|
|
public class MessageFragmentTests
|
|
{
|
|
[Fact]
|
|
public void TryParse_CompleteFragment_ReturnsFragmentAndConsumedBytes()
|
|
{
|
|
// Build a synthetic fragment: 16-byte header + 4-byte payload = totalSize 20
|
|
var header = new MessageFragmentHeader
|
|
{
|
|
Sequence = 1, Id = 0x80000001u, Count = 1,
|
|
TotalSize = 20, Index = 0, Queue = 5,
|
|
};
|
|
byte[] buf = new byte[20];
|
|
header.Pack(buf);
|
|
buf[16] = 0xAA; buf[17] = 0xBB; buf[18] = 0xCC; buf[19] = 0xDD;
|
|
|
|
var (frag, consumed) = MessageFragment.TryParse(buf);
|
|
|
|
Assert.NotNull(frag);
|
|
Assert.Equal(20, consumed);
|
|
Assert.Equal(4, frag!.Value.Payload.Length);
|
|
Assert.Equal(new byte[] { 0xAA, 0xBB, 0xCC, 0xDD }, frag.Value.Payload);
|
|
Assert.Equal(5, frag.Value.Header.Queue);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_InsufficientSourceForHeader_ReturnsNull()
|
|
{
|
|
var (frag, consumed) = MessageFragment.TryParse(new byte[15]);
|
|
Assert.Null(frag);
|
|
Assert.Equal(0, consumed);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_TotalSizeTooLarge_ReturnsNull()
|
|
{
|
|
// TotalSize claims 500 bytes (> MaxFragmentSize 464).
|
|
var header = new MessageFragmentHeader { TotalSize = 500, Count = 1 };
|
|
byte[] buf = new byte[16];
|
|
header.Pack(buf);
|
|
|
|
var (frag, consumed) = MessageFragment.TryParse(buf);
|
|
|
|
Assert.Null(frag);
|
|
Assert.Equal(0, consumed);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_TotalSizeSmallerThanHeader_ReturnsNull()
|
|
{
|
|
var header = new MessageFragmentHeader { TotalSize = 10 };
|
|
byte[] buf = new byte[16];
|
|
header.Pack(buf);
|
|
|
|
var (frag, consumed) = MessageFragment.TryParse(buf);
|
|
|
|
Assert.Null(frag);
|
|
Assert.Equal(0, consumed);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_IncompleteBody_ReturnsNull()
|
|
{
|
|
// Header says 40 total bytes but buffer only has 20.
|
|
var header = new MessageFragmentHeader { TotalSize = 40, Count = 1 };
|
|
byte[] buf = new byte[20];
|
|
header.Pack(buf);
|
|
|
|
var (frag, consumed) = MessageFragment.TryParse(buf);
|
|
|
|
Assert.Null(frag);
|
|
Assert.Equal(0, consumed);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_ConsumesExactlyOneFragment_LeavesRemainderForCaller()
|
|
{
|
|
// Two back-to-back fragments in one buffer. First call should consume
|
|
// exactly the first fragment, leaving the second intact.
|
|
var h1 = new MessageFragmentHeader { Id = 1, Count = 1, TotalSize = 18, Index = 0 };
|
|
var h2 = new MessageFragmentHeader { Id = 2, Count = 1, TotalSize = 17, Index = 0 };
|
|
byte[] buf = new byte[35]; // 18 + 17
|
|
h1.Pack(buf.AsSpan(0, 16));
|
|
buf[16] = 0xAA; buf[17] = 0xBB;
|
|
h2.Pack(buf.AsSpan(18, 16));
|
|
buf[34] = 0xCC;
|
|
|
|
var (frag1, consumed1) = MessageFragment.TryParse(buf);
|
|
Assert.NotNull(frag1);
|
|
Assert.Equal(18, consumed1);
|
|
|
|
var (frag2, consumed2) = MessageFragment.TryParse(buf.AsSpan(consumed1));
|
|
Assert.NotNull(frag2);
|
|
Assert.Equal(17, consumed2);
|
|
Assert.Equal(new byte[] { 0xCC }, frag2!.Value.Payload);
|
|
}
|
|
}
|