diff --git a/docs/cultnet-transport-parity.md b/docs/cultnet-transport-parity.md index 0aec5081..b9fc4336 100644 --- a/docs/cultnet-transport-parity.md +++ b/docs/cultnet-transport-parity.md @@ -243,6 +243,10 @@ Current progress: transport schedules the next attempt with the portable policy, opens a fresh caller-provided transport when the controller says it may retry, and resets attempt state when the caller marks the connection established. +- Kotlin now has the same RUDP reconnect-loop shape for JVM/Android callers: + the app-owned receive loop reports closure, `CultNetRudpReconnectLoop` + schedules retry through `CultNetReconnectController`, and the caller-provided + factory opens the next `CultNetRudpSocketTransportConnection`. - Kotlin now mirrors the C#/TypeScript/Python local authority lease gate: peer cards may advertise `authorityLeaseId`, but `CultMeshAuthorityLeaseCatalog` owns role/shard/time authorization so discovery contact cannot impersonate diff --git a/docs/runtime-parity-scope.md b/docs/runtime-parity-scope.md index e453d38a..037eed46 100644 --- a/docs/runtime-parity-scope.md +++ b/docs/runtime-parity-scope.md @@ -34,7 +34,7 @@ CultMesh. | Python | Package runtime, local daemon endpoint, raw interop peer, typed state participant | CultCache/CultNet/CultMesh package surface, schema catalogs, shard catalogs/logs, local TCP CultMesh server, session/prediction/simulation fact helpers, authority leases, streams, RUDP codec/session/socket transport, RUDP reconnect policy/controller, schema-v0 MessagePack over RUDP evidence, benchmark evidence against C# cache paths | Universal C# throughput, full C# daemon/server stack, and RUDP as the only public service pipe | | TypeScript | Node/browser-adjacent projection and interop runtime | CultCache parity tests, CultNet schema-v0 peer, transport profiles, TCP-framed transport port, shared RUDP socket transport, RUDP reconnect policy/controller, RUDP reconnect-loop helper, local CultMesh node/catalog/authority/stream ergonomics | Full C# game session/server stack | | Rust | Low-level cache/transport/runtime substrate | CultCache/CultNet interop lane, transport profiles, TCP-framed port, shared RUDP codec/session/socket transport, RUDP reconnect policy/controller, schema-v0 MessagePack over RUDP evidence, small CultMesh facade for peer cards, authority leases, RUDP endpoint parsing, and peer-card RUDP client construction | Full CultMesh game-session/server facade, stream catalog, or production game-session server | -| Kotlin | JVM/Android client substrate | Typed MessagePack cache, schema/shard catalogs, authority leases, stream catalog, branded `CultMesh` facade, RUDP client/server sugar, RUDP reconnect policy/controller, schema-v0 MessagePack over RUDP evidence | C# SoA store, full C# server daemon, or Unity hot-loop memory layout | +| Kotlin | JVM/Android client substrate | Typed MessagePack cache, schema/shard catalogs, authority leases, stream catalog, branded `CultMesh` facade, RUDP client/server sugar, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence | C# SoA store, full C# server daemon, or Unity hot-loop memory layout | ## Evidence Gates @@ -63,8 +63,8 @@ CultMesh. - Extend RUDP interop evidence through daemon/socket reconnect loops and the wider schema-message matrix. RUDP profiles now advertise the portable reconnect policy, runtime-local tests prove the shared controller across the - targeted runtimes, and the C# LiteNetLib client plus TypeScript RUDP - reconnect loop use that controller; broader service adoption remains a + targeted runtimes, and the C# LiteNetLib client plus TypeScript/Kotlin RUDP + reconnect loops use that controller; broader service adoption remains a separate claim. - Keep Kotlin ergonomic parity moving with Android/JVM-shaped APIs instead of backfilling C# server responsibilities it should not own. diff --git a/packages/cultmesh-kotlin/README.md b/packages/cultmesh-kotlin/README.md index 1f970e63..1dbae2ac 100644 --- a/packages/cultmesh-kotlin/README.md +++ b/packages/cultmesh-kotlin/README.md @@ -186,8 +186,10 @@ val nextDelayMs = computeReconnectDelayMs(policy, attempt = 3, jitterMs = 17) The helper publishes `cultnet.reconnect_policy.v0`, matches the C#, TypeScript, Rust, and Python defaults, and is advertised by Kotlin RUDP -transport profiles as `reconnectPolicy`. It is policy language, not an -automatic reconnect loop. +transport profiles as `reconnectPolicy`. `CultNetRudpReconnectLoop` consumes +the same controller for caller-owned RUDP client factories: the receive loop +decides when a transport is closed, then the reconnect loop schedules the next +attempt and opens a fresh transport through the supplied factory. `EveDashboardStateDocument` mirrors the CultUI-shaped dashboard surface contract: `surface.root` is the retained UI tree, `surface.assets` carries cacheable media diff --git a/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt b/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt index c0ca7ab7..a190a07c 100644 --- a/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt +++ b/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt @@ -360,6 +360,16 @@ object CultMesh { ?: throw IOException("Peer ${peer.peerId} does not advertise a RUDP endpoint") return createRudpClient(runtimeId, connectionId, endpoint, bindHost, bindPort, tuning) } + + fun createRudpReconnectLoop( + reconnectPolicy: CultNetReconnectPolicy = createReconnectPolicy(), + connectPayload: ByteArray = ByteArray(0), + createTransport: () -> CultNetRudpSocketTransportConnection, + ): CultNetRudpReconnectLoop = CultNetRudpReconnectLoop( + reconnectPolicy = reconnectPolicy, + connectPayload = connectPayload, + createTransport = createTransport, + ) } data class CultNetFrame(val opcode: Int, val payload: ByteArray) @@ -482,6 +492,77 @@ class CultNetReconnectController( } } +class CultNetRudpReconnectLoop( + reconnectPolicy: CultNetReconnectPolicy = createReconnectPolicy(), + private val connectPayload: ByteArray = ByteArray(0), + private val createTransport: () -> CultNetRudpSocketTransportConnection, + private val nowMsProvider: () -> Long = { nowMs() }, + private val jitterMsProvider: () -> Long = { 0 }, + private val scheduler: (delayMs: Long, callback: () -> Unit) -> AutoCloseable = { delayMs, callback -> + val thread = Thread { + try { + Thread.sleep(delayMs) + callback() + } catch (_: InterruptedException) { + // Timer cancelled. + } + } + thread.isDaemon = true + thread.start() + AutoCloseable { thread.interrupt() } + }, +) { + val reconnectController = CultNetReconnectController(reconnectPolicy) + var transport: CultNetRudpSocketTransportConnection? = null + private set + private var timer: AutoCloseable? = null + private var stopped = true + + fun start(): CultNetRudpSocketTransportConnection { + stopped = false + reconnectController.reset() + return openTransport() + } + + fun stop() { + stopped = true + timer?.close() + timer = null + transport?.close() + transport = null + reconnectController.reset() + } + + fun markConnected() { + reconnectController.reset() + } + + fun handleClosed(): CultNetReconnectDecision? { + transport = null + return scheduleReconnect() + } + + private fun openTransport(): CultNetRudpSocketTransportConnection { + val next = createTransport() + transport = next + next.connect(connectPayload) + return next + } + + private fun scheduleReconnect(): CultNetReconnectDecision? { + if (stopped || timer != null) return null + val decision = reconnectController.recordFailure(nowMsProvider(), jitterMsProvider()) + if (!decision.shouldRetry) return decision + timer = scheduler(decision.delayMs) { + timer = null + if (!stopped && reconnectController.canAttempt(nowMsProvider())) { + openTransport() + } + } + return decision + } +} + data class CultNetTransportProfile( val schemaVersion: String = "cultnet.transport_profile.v0", val runtimeId: String, @@ -2457,6 +2538,7 @@ fun main(args: Array) { cultNetSchemaMessagesUseMessagePackMaps() cultNetReconnectPolicyExposesPortableDelayContract() cultNetReconnectControllerSchedulesAttemptsAndReset() + cultNetRudpReconnectLoopConsumesSharedController() cultNetSchemaCatalogsRoundTripDescriptors() cultMeshCatalogsRoundTripDiscoveryMessages() cultMeshAuthorityLeasesGatePeerTrust() @@ -2689,6 +2771,52 @@ private fun cultNetReconnectControllerSchedulesAttemptsAndReset() { check(controller.canAttempt(99_000)) } +private fun cultNetRudpReconnectLoopConsumesSharedController() { + var now = 10_000L + var capturedTimer: (() -> Unit)? = null + val created = mutableListOf() + val loop = CultNetRudpReconnectLoop( + reconnectPolicy = createReconnectPolicy(maxAttempts = 2), + connectPayload = "join".toByteArray(StandardCharsets.UTF_8), + createTransport = { + CultMesh.createRudpClient( + runtimeId = "kotlin-reconnect-client-${created.size}", + connectionId = 0x20304050, + remoteHost = "127.0.0.1", + remotePort = 9, + tuning = CultNetRudpSocketTuning(resendDelayMs = 25, maxPendingReliablePackets = 16), + ).also { created.add(it) } + }, + nowMsProvider = { now }, + jitterMsProvider = { 17 }, + scheduler = { delayMs, callback -> + check(delayMs == 1_017L) + capturedTimer = callback + AutoCloseable { capturedTimer = null } + }, + ) + + val first = loop.start() + check(created.size == 1) + check(loop.transport === first) + first.close() + val decision = loop.handleClosed() + check(decision?.attempt == 1) + check(decision?.delayMs == 1_017L) + check(loop.reconnectController.nextAttemptAtMs == 11_017L) + check(capturedTimer != null) + + now = 11_017L + capturedTimer?.invoke() + check(created.size == 2) + check(loop.transport === created[1]) + + loop.markConnected() + check(loop.reconnectController.attempt == 0) + loop.stop() + check(loop.transport == null) +} + private fun cultNetSchemaCatalogsRoundTripDescriptors() { val catalog = CultNetSchemaCatalog() var watched: CultNetSchemaDescriptor? = null