Add TypeScript RUDP packet fixture
This commit is contained in:
parent
8ce89688e2
commit
0bcfe1af8d
4 changed files with 319 additions and 2 deletions
|
|
@ -154,10 +154,58 @@ Current progress:
|
|||
`TcpFramedTransportConnection` with schema-channel `send`, `receive`, and
|
||||
transfer stats. The Rust interop peer advertises and uses that shared port
|
||||
instead of owning raw TCP frame I/O directly.
|
||||
- TypeScript now has the first `cultnet.transport.rudp.v0` packet codec and a
|
||||
deterministic reliable ordered data-packet fixture. This is not the RUDP
|
||||
runtime yet; it is the binary packet language the runtimes must converge on.
|
||||
- The remaining parity work is to add the equivalent port to Kotlin, deepen
|
||||
Python's server-side use of its port, and move each runtime's existing
|
||||
TCP/LiteNetLib/WebSocket bodies behind those ports before implementing
|
||||
`rudp`.
|
||||
TCP/LiteNetLib/WebSocket bodies behind those ports, port the RUDP packet
|
||||
fixture into C#/Rust/Python, then implement `rudp`.
|
||||
|
||||
## RUDP Packet Contract V0
|
||||
|
||||
`cultnet.transport.rudp.v0` packets start with a fixed binary header, followed
|
||||
by a UTF-8 channel id and payload bytes. All integer fields are unsigned
|
||||
big-endian. The first deterministic fixture lives in TypeScript and must be
|
||||
ported byte-for-byte before runtime-specific resend loops are allowed to claim
|
||||
parity.
|
||||
|
||||
| Offset | Size | Field | Notes |
|
||||
| --- | ---: | --- | --- |
|
||||
| 0 | 4 | magic | ASCII `CNR0` |
|
||||
| 4 | 1 | version | `0` |
|
||||
| 5 | 1 | packet type | `1=connect`, `2=accept`, `3=data`, `4=ack`, `5=ping`, `6=pong`, `7=disconnect` |
|
||||
| 6 | 1 | flags | bit 0 reliable, bit 1 ordered, bit 2 sequenced, bit 3 fragmented |
|
||||
| 7 | 1 | header bytes | fixed header plus channel id bytes |
|
||||
| 8 | 4 | connection id | peer/session binding token |
|
||||
| 12 | 4 | sequence | packet sequence number |
|
||||
| 16 | 4 | ack | latest received sequence being acknowledged |
|
||||
| 20 | 4 | ack mask | selective acknowledgement mask for prior packets |
|
||||
| 24 | 2 | fragment id | `0` when not fragmented |
|
||||
| 26 | 2 | fragment index | zero-based fragment index |
|
||||
| 28 | 2 | fragment count | total fragments, `0` when not fragmented |
|
||||
| 30 | 4 | payload bytes | payload length |
|
||||
| 34 | 1 | channel id bytes | UTF-8 byte count, max 255 |
|
||||
| 35 | 1 | reserved | `0` |
|
||||
| 36 | n | channel id | UTF-8 channel id |
|
||||
| 36+n | m | payload | transport-neutral payload bytes |
|
||||
|
||||
Canonical reliable ordered data fixture:
|
||||
|
||||
```text
|
||||
packetType=data
|
||||
connectionId=0x01020304
|
||||
sequence=0x0000002a
|
||||
ack=0x00000029
|
||||
ackMask=0x80000001
|
||||
channelId=schema
|
||||
flags=reliable|ordered|fragmented
|
||||
fragmentId=7
|
||||
fragmentIndex=1
|
||||
fragmentCount=3
|
||||
payload=hello
|
||||
hex=434e523000030b2a010203040000002a0000002980000001000700010003000000050600736368656d6168656c6c6f
|
||||
```
|
||||
|
||||
## Reliable UDP Semantics To Lift From LiteNetLib
|
||||
|
||||
|
|
|
|||
|
|
@ -65,6 +65,14 @@ export {
|
|||
type CultNetTransportStats,
|
||||
type TcpFramedTransportProfileOptions,
|
||||
} from "./transport";
|
||||
export {
|
||||
createRudpTransportProfile,
|
||||
decodeRudpPacket,
|
||||
encodeRudpPacket,
|
||||
type CultNetRudpPacket,
|
||||
type CultNetRudpPacketType,
|
||||
type RudpTransportProfileOptions,
|
||||
} from "./rudp";
|
||||
export {
|
||||
defineJsonSchemaContract,
|
||||
type JsonSchemaContract,
|
||||
|
|
|
|||
201
packages/cultnet-ts/src/rudp.ts
Normal file
201
packages/cultnet-ts/src/rudp.ts
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
import type { CultNetTransportProfile } from "./contracts";
|
||||
|
||||
const RUDP_MAGIC = [0x43, 0x4e, 0x52, 0x30] as const; // CNR0
|
||||
const RUDP_VERSION = 0;
|
||||
const RUDP_FIXED_HEADER_BYTES = 36;
|
||||
const MAX_CHANNEL_ID_BYTES = 255;
|
||||
|
||||
export type CultNetRudpPacketType =
|
||||
| "connect"
|
||||
| "accept"
|
||||
| "data"
|
||||
| "ack"
|
||||
| "ping"
|
||||
| "pong"
|
||||
| "disconnect";
|
||||
|
||||
export interface CultNetRudpPacket {
|
||||
packetType: CultNetRudpPacketType;
|
||||
connectionId: number;
|
||||
sequence: number;
|
||||
ack: number;
|
||||
ackMask: number;
|
||||
channelId: string;
|
||||
reliable?: boolean;
|
||||
ordered?: boolean;
|
||||
sequenced?: boolean;
|
||||
fragmentId?: number;
|
||||
fragmentIndex?: number;
|
||||
fragmentCount?: number;
|
||||
payload?: Uint8Array;
|
||||
}
|
||||
|
||||
export interface RudpTransportProfileOptions {
|
||||
transportId?: string;
|
||||
host?: string;
|
||||
port?: number;
|
||||
maxPayloadBytes?: number;
|
||||
maxFragmentBytes?: number;
|
||||
}
|
||||
|
||||
const packetTypeToCode: Record<CultNetRudpPacketType, number> = {
|
||||
connect: 1,
|
||||
accept: 2,
|
||||
data: 3,
|
||||
ack: 4,
|
||||
ping: 5,
|
||||
pong: 6,
|
||||
disconnect: 7,
|
||||
};
|
||||
|
||||
const packetTypeFromCode = new Map<number, CultNetRudpPacketType>(
|
||||
Object.entries(packetTypeToCode).map(([name, code]) => [code, name as CultNetRudpPacketType]),
|
||||
);
|
||||
|
||||
export function createRudpTransportProfile(
|
||||
runtimeId: string,
|
||||
options: RudpTransportProfileOptions = {},
|
||||
): CultNetTransportProfile {
|
||||
return {
|
||||
schemaVersion: "cultnet.transport_profile.v0",
|
||||
runtimeId,
|
||||
transports: [
|
||||
{
|
||||
transportId: options.transportId ?? "rudp",
|
||||
protocol: "rudp",
|
||||
host: options.host,
|
||||
port: options.port,
|
||||
wireContracts: ["cultnet.schema.v0"],
|
||||
channels: [
|
||||
{
|
||||
channelId: "schema",
|
||||
delivery: "reliable",
|
||||
ordering: "ordered",
|
||||
maxPayloadBytes: options.maxPayloadBytes,
|
||||
maxFragmentBytes: options.maxFragmentBytes,
|
||||
},
|
||||
{
|
||||
channelId: "latest",
|
||||
delivery: "unreliable",
|
||||
ordering: "sequenced",
|
||||
maxPayloadBytes: options.maxPayloadBytes,
|
||||
maxFragmentBytes: options.maxFragmentBytes,
|
||||
},
|
||||
{
|
||||
channelId: "realtime",
|
||||
delivery: "unreliable",
|
||||
ordering: "unordered",
|
||||
maxPayloadBytes: options.maxPayloadBytes,
|
||||
maxFragmentBytes: options.maxFragmentBytes,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
export function encodeRudpPacket(packet: CultNetRudpPacket): Uint8Array {
|
||||
const channelId = new TextEncoder().encode(packet.channelId);
|
||||
if (channelId.length > MAX_CHANNEL_ID_BYTES) {
|
||||
throw new Error("CultNet RUDP channel id cannot exceed 255 UTF-8 bytes.");
|
||||
}
|
||||
|
||||
const payload = packet.payload ?? new Uint8Array();
|
||||
const headerBytes = RUDP_FIXED_HEADER_BYTES + channelId.length;
|
||||
const wire = new Uint8Array(headerBytes + payload.length);
|
||||
const view = new DataView(wire.buffer, wire.byteOffset, wire.byteLength);
|
||||
wire.set(RUDP_MAGIC, 0);
|
||||
view.setUint8(4, RUDP_VERSION);
|
||||
view.setUint8(5, packetTypeToCode[packet.packetType]);
|
||||
view.setUint8(6, encodeFlags(packet));
|
||||
view.setUint8(7, headerBytes);
|
||||
view.setUint32(8, toUint32(packet.connectionId, "connectionId"), false);
|
||||
view.setUint32(12, toUint32(packet.sequence, "sequence"), false);
|
||||
view.setUint32(16, toUint32(packet.ack, "ack"), false);
|
||||
view.setUint32(20, toUint32(packet.ackMask, "ackMask"), false);
|
||||
view.setUint16(24, toUint16(packet.fragmentId ?? 0, "fragmentId"), false);
|
||||
view.setUint16(26, toUint16(packet.fragmentIndex ?? 0, "fragmentIndex"), false);
|
||||
view.setUint16(28, toUint16(packet.fragmentCount ?? 0, "fragmentCount"), false);
|
||||
view.setUint32(30, toUint32(payload.length, "payload length"), false);
|
||||
view.setUint8(34, channelId.length);
|
||||
view.setUint8(35, 0);
|
||||
wire.set(channelId, RUDP_FIXED_HEADER_BYTES);
|
||||
wire.set(payload, headerBytes);
|
||||
return wire;
|
||||
}
|
||||
|
||||
export function decodeRudpPacket(wire: Uint8Array): CultNetRudpPacket {
|
||||
if (wire.length < RUDP_FIXED_HEADER_BYTES) {
|
||||
throw new Error("CultNet RUDP packet is shorter than the fixed header.");
|
||||
}
|
||||
|
||||
const view = new DataView(wire.buffer, wire.byteOffset, wire.byteLength);
|
||||
for (let index = 0; index < RUDP_MAGIC.length; index += 1) {
|
||||
if (view.getUint8(index) !== RUDP_MAGIC[index]) {
|
||||
throw new Error("CultNet RUDP packet has the wrong magic.");
|
||||
}
|
||||
}
|
||||
|
||||
const version = view.getUint8(4);
|
||||
if (version !== RUDP_VERSION) {
|
||||
throw new Error(`Unsupported CultNet RUDP packet version ${version}.`);
|
||||
}
|
||||
|
||||
const packetType = packetTypeFromCode.get(view.getUint8(5));
|
||||
if (!packetType) {
|
||||
throw new Error(`Unsupported CultNet RUDP packet type ${view.getUint8(5)}.`);
|
||||
}
|
||||
|
||||
const headerBytes = view.getUint8(7);
|
||||
const channelIdLength = view.getUint8(34);
|
||||
if (headerBytes !== RUDP_FIXED_HEADER_BYTES + channelIdLength) {
|
||||
throw new Error("CultNet RUDP packet header length does not match the channel id length.");
|
||||
}
|
||||
|
||||
const payloadLength = view.getUint32(30, false);
|
||||
if (wire.length !== headerBytes + payloadLength) {
|
||||
throw new Error("CultNet RUDP packet payload length does not match the packet size.");
|
||||
}
|
||||
|
||||
const flags = view.getUint8(6);
|
||||
return {
|
||||
packetType,
|
||||
connectionId: view.getUint32(8, false),
|
||||
sequence: view.getUint32(12, false),
|
||||
ack: view.getUint32(16, false),
|
||||
ackMask: view.getUint32(20, false),
|
||||
fragmentId: view.getUint16(24, false),
|
||||
fragmentIndex: view.getUint16(26, false),
|
||||
fragmentCount: view.getUint16(28, false),
|
||||
channelId: new TextDecoder().decode(wire.subarray(RUDP_FIXED_HEADER_BYTES, headerBytes)),
|
||||
reliable: (flags & 0b0000_0001) !== 0,
|
||||
ordered: (flags & 0b0000_0010) !== 0,
|
||||
sequenced: (flags & 0b0000_0100) !== 0,
|
||||
payload: wire.subarray(headerBytes),
|
||||
};
|
||||
}
|
||||
|
||||
function encodeFlags(packet: CultNetRudpPacket): number {
|
||||
return (
|
||||
(packet.reliable ? 0b0000_0001 : 0) |
|
||||
(packet.ordered ? 0b0000_0010 : 0) |
|
||||
(packet.sequenced ? 0b0000_0100 : 0) |
|
||||
((packet.fragmentCount ?? 0) > 0 ? 0b0000_1000 : 0)
|
||||
);
|
||||
}
|
||||
|
||||
function toUint32(value: number, fieldName: string): number {
|
||||
if (!Number.isInteger(value) || value < 0 || value > 0xffffffff) {
|
||||
throw new Error(`CultNet RUDP ${fieldName} must fit in uint32.`);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function toUint16(value: number, fieldName: string): number {
|
||||
if (!Number.isInteger(value) || value < 0 || value > 0xffff) {
|
||||
throw new Error(`CultNet RUDP ${fieldName} must fit in uint16.`);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
|
@ -23,8 +23,11 @@ import {
|
|||
cultNetSchemas,
|
||||
cultNetBuiltinSchemaRegistry,
|
||||
createTcpFramedTransportProfile,
|
||||
createRudpTransportProfile,
|
||||
defineCultNetDocumentBinding,
|
||||
decodeRudpPacket,
|
||||
encodeCultNetMessageForWire,
|
||||
encodeRudpPacket,
|
||||
ghostlightAgentStateGeneratedContract,
|
||||
parseCultNetMessage,
|
||||
validateGhostlightAgentStateGenerated,
|
||||
|
|
@ -190,6 +193,63 @@ test("tcp_framed transport carries raw schema channel payloads with stats", asyn
|
|||
right.close();
|
||||
});
|
||||
|
||||
test("rudp packet codec has a deterministic reliable ordered fixture", () => {
|
||||
const encoded = encodeRudpPacket({
|
||||
packetType: "data",
|
||||
connectionId: 0x01020304,
|
||||
sequence: 0x0000002a,
|
||||
ack: 0x00000029,
|
||||
ackMask: 0x80000001,
|
||||
channelId: "schema",
|
||||
reliable: true,
|
||||
ordered: true,
|
||||
fragmentId: 7,
|
||||
fragmentIndex: 1,
|
||||
fragmentCount: 3,
|
||||
payload: Buffer.from("hello", "utf8"),
|
||||
});
|
||||
|
||||
assert.equal(
|
||||
Buffer.from(encoded).toString("hex"),
|
||||
"434e523000030b2a010203040000002a0000002980000001000700010003000000050600736368656d6168656c6c6f",
|
||||
);
|
||||
|
||||
const decoded = decodeRudpPacket(encoded);
|
||||
assert.equal(decoded.packetType, "data");
|
||||
assert.equal(decoded.connectionId, 0x01020304);
|
||||
assert.equal(decoded.sequence, 0x0000002a);
|
||||
assert.equal(decoded.ack, 0x00000029);
|
||||
assert.equal(decoded.ackMask, 0x80000001);
|
||||
assert.equal(decoded.channelId, "schema");
|
||||
assert.equal(decoded.reliable, true);
|
||||
assert.equal(decoded.ordered, true);
|
||||
assert.equal(decoded.sequenced, false);
|
||||
assert.equal(decoded.fragmentId, 7);
|
||||
assert.equal(decoded.fragmentIndex, 1);
|
||||
assert.equal(decoded.fragmentCount, 3);
|
||||
assert.equal(Buffer.from(decoded.payload ?? []).toString("utf8"), "hello");
|
||||
});
|
||||
|
||||
test("rudp transport profile advertises state and realtime channel semantics", () => {
|
||||
const profile = createRudpTransportProfile("node-rudp", {
|
||||
transportId: "public-rudp",
|
||||
host: "127.0.0.1",
|
||||
port: 7777,
|
||||
maxPayloadBytes: 1200,
|
||||
maxFragmentBytes: 1000,
|
||||
});
|
||||
|
||||
assert.equal(profile.transports[0]?.protocol, "rudp");
|
||||
assert.deepEqual(
|
||||
profile.transports[0]?.channels.map((channel) => [channel.channelId, channel.delivery, channel.ordering]),
|
||||
[
|
||||
["schema", "reliable", "ordered"],
|
||||
["latest", "unreliable", "sequenced"],
|
||||
["realtime", "unreliable", "unordered"],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test("CultNet peer can speak through a transport connection", async () => {
|
||||
const { a, b } = createDuplexPair();
|
||||
const leftTransport = new TcpFramedTransportConnection(a, createTcpFramedTransportProfile("left"));
|
||||
|
|
|
|||
Loading…
Reference in a new issue