Add Kotlin RUDP interop proof

This commit is contained in:
Meta Vens 2026-06-15 01:17:08 +01:00
commit 13a70db012
3 changed files with 277 additions and 10 deletions

View file

@ -172,15 +172,14 @@ Current progress:
Rust, Python, and Kotlin expose the same transport frame/stats shape through
synchronous socket polling.
- The TypeScript interop harness now proves bidirectional cross-runtime RUDP
socket exchange with C#, Python, and Rust: TypeScript can dial those UDP
peers, C#/Python/Rust can dial a TypeScript UDP peer, and all sides exchange
reliable ordered `schema` frames through the shared handshake and packet
language.
socket exchange with C#, Kotlin, Python, and Rust: TypeScript can dial those
UDP peers, C#/Kotlin/Python/Rust can dial a TypeScript UDP peer, and all sides
exchange reliable ordered `schema` frames through the shared handshake and
packet language.
- The remaining parity work is to deepen Python's server-side use of its port
and move each runtime's existing TCP/LiteNetLib/WebSocket bodies behind those
ports, then broaden RUDP interop into the full loss, reordering,
fragmentation, reconnect, and schema-message matrix. Kotlin still needs a
callable interop peer before it can join the cross-runtime socket harness.
fragmentation, reconnect, and schema-message matrix.
## RUDP Packet Contract V0

View file

@ -491,9 +491,105 @@ private fun uint16(value: Int, fieldName: String): Int {
private fun nowMs(): Long = Instant.now().toEpochMilli()
fun main() {
rudpPacketCodecUsesDeterministicReliableOrderedFixture()
rudpSocketTransportHandshakesAndCarriesReliableOrderedSchemaFrames()
fun main(args: Array<String>) {
if (args.isEmpty()) {
rudpPacketCodecUsesDeterministicReliableOrderedFixture()
rudpSocketTransportHandshakesAndCarriesReliableOrderedSchemaFrames()
return
}
val options = parseArgs(args.drop(1))
when (args[0]) {
"rudp-serve-once" -> rudpServeOnce(options)
"rudp-dial-once" -> rudpDialOnce(options)
else -> error("Unknown mode ${args[0]}")
}
}
private fun rudpServeOnce(options: Map<String, String>) {
val bindHost = options["bind-host"] ?: "127.0.0.1"
val bindPort = options["bind-port"]?.toInt() ?: 0
val socket = DatagramSocket(bindPort, InetAddress.getByName(bindHost)).also { it.soTimeout = 20 }
CultNetRudpSocketTransportConnection(
socket = socket,
mode = CultNetRudpSocketMode.Server,
runtimeId = "kotlin-rudp-interop",
connectionId = 0x446688aaL,
initialSequence = 100,
resendDelayMs = 25,
).use { transport ->
println("""{"status":"ready","port":${socket.localPort}}""")
val deadline = System.nanoTime() + 5_000_000_000L
while (System.nanoTime() < deadline) {
val frame = transport.receiveOnce()
if (frame != null) {
requireRudpFrame(frame, "schema", "ts-kotlin-client-state")
transport.send("schema", "kotlin-server-state".toByteArray(StandardCharsets.UTF_8))
println("""{"status":"ok"}""")
return
}
transport.pollResends()
Thread.sleep(5)
}
}
error("Timed out waiting for TypeScript RUDP frame")
}
private fun rudpDialOnce(options: Map<String, String>) {
val targetHost = options.getValue("target-host")
val targetPort = options.getValue("target-port").toInt()
val loopback = InetAddress.getByName("127.0.0.1")
val socket = DatagramSocket(0, loopback).also { it.soTimeout = 20 }
CultNetRudpSocketTransportConnection(
socket = socket,
mode = CultNetRudpSocketMode.Client,
runtimeId = "kotlin-rudp-client-interop",
connectionId = 0xaa886644L,
remoteAddress = InetSocketAddress(InetAddress.getByName(targetHost), targetPort),
initialSequence = 1,
resendDelayMs = 25,
).use { transport ->
transport.connect("kotlin-join".toByteArray(StandardCharsets.UTF_8))
var sent = false
val deadline = System.nanoTime() + 5_000_000_000L
while (System.nanoTime() < deadline) {
val frame = transport.receiveOnce()
if (frame != null) {
requireRudpFrame(frame, "schema", "ts-kotlin-server-state")
println("""{"status":"ok"}""")
return
}
transport.pollResends()
if (transport.connected && !sent) {
transport.send("schema", "kotlin-client-state".toByteArray(StandardCharsets.UTF_8))
sent = true
}
Thread.sleep(5)
}
}
error("Timed out waiting for TypeScript RUDP response")
}
private fun requireRudpFrame(frame: CultNetTransportFrame, expectedChannelId: String, expectedPayload: String) {
val expectedBytes = expectedPayload.toByteArray(StandardCharsets.UTF_8)
if (frame.channelId != expectedChannelId || !frame.payload.contentEquals(expectedBytes)) {
error("Unexpected RUDP frame: channel=${frame.channelId}, payload=${String(frame.payload, StandardCharsets.UTF_8)}")
}
}
private fun parseArgs(args: List<String>): Map<String, String> {
val parsed = linkedMapOf<String, String>()
var index = 0
while (index < args.size) {
val token = args[index]
if (!token.startsWith("--")) {
index += 1
continue
}
parsed[token.removePrefix("--")] = args.getOrNull(index + 1) ?: error("Missing value for $token")
index += 2
}
return parsed
}
private fun rudpPacketCodecUsesDeterministicReliableOrderedFixture() {

View file

@ -6,7 +6,7 @@ import { once } from "node:events";
import { existsSync, rmSync } from "node:fs";
import { connect as connectTcp, createServer } from "node:net";
import { homedir, networkInterfaces, tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { delimiter, dirname, join, resolve } from "node:path";
import type { Readable } from "node:stream";
import { test } from "node:test";
import { setTimeout as delay } from "node:timers/promises";
@ -25,6 +25,13 @@ const cultNetTsRoot = resolve(__dirname, "../../..");
const cultLibRoot = findAncestor(cultNetTsRoot, "CultLib.sln") ?? resolve(cultNetTsRoot, "..", "CultLib");
const cultcachePyRoot = resolve(cultLibRoot, "packages", "cultcache-py");
const cultcachePySrc = resolve(cultcachePyRoot, "src");
const cultmeshKotlinRoot = resolve(cultLibRoot, "packages", "cultmesh-kotlin");
const kotlinHome = process.env.KOTLIN_HOME ?? "C:\\Program Files\\Android\\Android Studio\\plugins\\Kotlin\\kotlinc";
const kotlinJavaHome = process.env.KOTLIN_JAVA_HOME ?? "C:\\Program Files\\Android\\Android Studio\\jbr";
const kotlinJavaCommand = process.env.JAVA ?? join(kotlinJavaHome, "bin", process.platform === "win32" ? "java.exe" : "java");
const kotlinStdlibPath = join(kotlinHome, "lib", "kotlin-stdlib.jar");
const kotlinJarPath = resolve(cultLibRoot, "artifacts", "cultmesh-kotlin", "cultmesh-kotlin.jar");
const kotlinClasspath = [kotlinJarPath, kotlinStdlibPath].join(delimiter);
const cultnetRsRoot = existsSync(resolve(cultLibRoot, "packages", "cultnet-rs"))
? resolve(cultLibRoot, "packages", "cultnet-rs")
: resolve(cultNetTsRoot, "..", "cultnet-rs");
@ -59,6 +66,7 @@ const rustBinaryPath = resolve(
const discoveryGroup = "239.77.44.11";
let rustInteropPeerBuild: Promise<void> | undefined;
let csharpInteropPeerBuild: Promise<void> | undefined;
let kotlinInteropPeerBuild: Promise<void> | undefined;
const pythonRudpPeerScript = String.raw`
import socket
@ -904,6 +912,82 @@ test("CultNet C# and TypeScript exchange schema frames when C# dials a TypeScrip
}
});
test("CultNet TypeScript and Kotlin exchange schema frames over the shared RUDP socket transport", async () => {
await buildKotlinInteropPeer();
const kotlinPeer = spawnKotlinRudpServer();
const clientSocket = await bindUdpSocket();
let client: CultNetRudpSocketTransportConnection | undefined;
try {
const kotlinPort = await kotlinPeer.ready;
client = new CultNetRudpSocketTransportConnection({
runtimeId: "ts-kotlin-rudp-interop",
socket: clientSocket,
mode: "client",
remoteHost: "127.0.0.1",
remotePort: kotlinPort,
connectionId: 0x446688aa,
initialSequence: 1,
resendDelayMs: 25,
resendPollMs: 5,
});
const receivedFrame = once(client, "frame");
client.connect(Buffer.from("ts-kotlin-join"));
await waitFor(() => client?.connected === true, "TypeScript RUDP client to complete Kotlin handshake");
client.send("schema", Buffer.from("ts-kotlin-client-state"));
const [frame] = await withTimeout(receivedFrame, 2_000, "Kotlin RUDP response frame");
assert.equal(frame.channelId, "schema");
assert.deepEqual(Buffer.from(frame.payload), Buffer.from("kotlin-server-state"));
const [exitCode] = await withTimeout(once(kotlinPeer.child, "exit"), 2_000, "Kotlin RUDP peer exit");
assert.equal(exitCode, 0, kotlinPeer.stderr.join(""));
} finally {
if (client) {
client.close();
} else {
clientSocket.close();
}
if (kotlinPeer.child.exitCode === null && !kotlinPeer.child.killed) {
kotlinPeer.child.kill("SIGTERM");
await once(kotlinPeer.child, "exit").catch(() => undefined);
}
}
});
test("CultNet Kotlin and TypeScript exchange schema frames when Kotlin dials a TypeScript RUDP server", async () => {
await buildKotlinInteropPeer();
const serverSocket = await bindUdpSocket();
const server = new CultNetRudpSocketTransportConnection({
runtimeId: "ts-kotlin-rudp-server-interop",
socket: serverSocket,
mode: "server",
connectionId: 0xaa886644,
initialSequence: 100,
resendDelayMs: 25,
resendPollMs: 5,
});
const kotlinClient = spawnKotlinRudpClient(udpSocketPort(serverSocket));
try {
const receivedFrame = once(server, "frame");
const [frame] = await withTimeout(receivedFrame, 2_000, "Kotlin RUDP client frame");
assert.equal(frame.channelId, "schema");
assert.deepEqual(Buffer.from(frame.payload), Buffer.from("kotlin-client-state"));
server.send("schema", Buffer.from("ts-kotlin-server-state"));
const [exitCode] = await withTimeout(once(kotlinClient.child, "exit"), 2_000, "Kotlin RUDP client exit");
assert.equal(exitCode, 0, kotlinClient.stderr.join(""));
} finally {
server.close();
if (kotlinClient.child.exitCode === null && !kotlinClient.child.killed) {
kotlinClient.child.kill("SIGTERM");
await once(kotlinClient.child, "exit").catch(() => undefined);
}
}
});
async function buildInteropPeers(): Promise<void> {
await buildRustInteropPeer();
await buildCSharpInteropPeer();
@ -923,6 +1007,18 @@ async function buildCSharpInteropPeer(): Promise<void> {
await csharpInteropPeerBuild;
}
async function buildKotlinInteropPeer(): Promise<void> {
kotlinInteropPeerBuild ??= execFileAsync(
"powershell",
["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", resolve(cultmeshKotlinRoot, "build.ps1")],
{
cwd: cultLibRoot,
timeout: 180_000,
},
).then(() => undefined);
await kotlinInteropPeerBuild;
}
function cleanInteropStores(runtimeIds: readonly string[]): void {
for (const runtimeId of runtimeIds) {
rmSync(join(tmpdir(), `cultnet-rs-interop-${runtimeId}.msgpack`), { force: true });
@ -1192,6 +1288,61 @@ function spawnCSharpRudpServer(): RunningPythonRudpPeer {
return { child, ready, stderr };
}
function spawnKotlinRudpServer(): RunningPythonRudpPeer {
const child = spawn(kotlinJavaCommand, [
"-cp", kotlinClasspath,
"org.gamecult.cultmesh.CultMeshKt",
"rudp-serve-once",
"--bind-host", "127.0.0.1",
], {
cwd: cultLibRoot,
env: process.env,
stdio: ["ignore", "pipe", "pipe"],
});
const stderr: string[] = [];
let stdoutBuffer = "";
const ready = new Promise<number>((resolveReady, rejectReady) => {
child.stdout.setEncoding("utf8");
child.stdout.on("data", (chunk: string) => {
stdoutBuffer += chunk;
while (true) {
const newline = stdoutBuffer.indexOf("\n");
if (newline === -1) {
break;
}
const line = stdoutBuffer.slice(0, newline).trim();
stdoutBuffer = stdoutBuffer.slice(newline + 1);
if (!line) {
continue;
}
try {
const parsed = JSON.parse(line) as { status?: string; port?: number };
if (parsed.status === "ready" && typeof parsed.port === "number") {
resolveReady(parsed.port);
} else if (parsed.status !== "ok") {
rejectReady(new Error(`Kotlin RUDP peer emitted unexpected stdout: ${line}`));
}
} catch (error) {
rejectReady(new Error(`Kotlin RUDP peer emitted non-JSON stdout: ${line}`));
}
}
});
child.stderr.setEncoding("utf8");
child.stderr.on("data", (chunk: string) => {
stderr.push(chunk);
});
child.once("exit", (code, signal) => {
rejectReady(new Error(`Kotlin RUDP peer exited before publishing a port (code=${code}, signal=${signal}).\n${stderr.join("")}`));
});
child.once("error", rejectReady);
});
return { child, ready, stderr };
}
function spawnPythonRudpClient(remotePort: number): Omit<RunningPythonRudpPeer, "ready"> {
const child = spawn(pythonCommand, ["-c", pythonRudpClientScript, String(remotePort)], {
cwd: cultcachePyRoot,
@ -1207,6 +1358,27 @@ function spawnPythonRudpClient(remotePort: number): Omit<RunningPythonRudpPeer,
return { child, stderr };
}
function spawnKotlinRudpClient(remotePort: number): Omit<RunningPythonRudpPeer, "ready"> {
const child = spawn(kotlinJavaCommand, [
"-cp", kotlinClasspath,
"org.gamecult.cultmesh.CultMeshKt",
"rudp-dial-once",
"--target-host", "127.0.0.1",
"--target-port", String(remotePort),
], {
cwd: cultLibRoot,
env: process.env,
stdio: ["ignore", "pipe", "pipe"],
});
const stderr: string[] = [];
child.stderr.setEncoding("utf8");
child.stderr.on("data", (chunk: string) => {
stderr.push(chunk);
});
return { child, stderr };
}
function spawnCSharpRudpClient(remotePort: number): Omit<RunningPythonRudpPeer, "ready"> {
const child = spawn(dotnetCommand, [
csharpDllPath,