diff --git a/docs/cultnet-transport-parity.md b/docs/cultnet-transport-parity.md index 49c7e780..ba14281e 100644 --- a/docs/cultnet-transport-parity.md +++ b/docs/cultnet-transport-parity.md @@ -334,7 +334,9 @@ Current progress: schema and shard catalogs. The helpers send standard schema-v0 requests, require the matching response schema, and apply through the caller's catalog, giving JVM/Android callers the same client ergonomics Python already had - without making the transport own catalog truth. + without making the transport own catalog truth. Both adapters implement the + shared `CultNetSchemaMessageTransport` shape so the helper logic has one + owner across Kotlin stream and RUDP paths. - Kotlin authority lease catalogs now also expose watch callbacks, so JVM and Android clients can react to lease changes without treating peer-card contact hints as trust. diff --git a/docs/runtime-parity-scope.md b/docs/runtime-parity-scope.md index f0f9416b..cc8ca167 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 and reconnect-loop helper, 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, schema/shard catalog request/sync helpers, transport profiles, TCP-framed transport port and peer helper, 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 and reconnect-loop helper, 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 with transport fetch/sync helpers, authority leases, stream catalog, branded `CultMesh` facade, WebSocket transport profile/connection helper, 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 | +| Kotlin | JVM/Android client substrate | Typed MessagePack cache, schema/shard catalogs with shared schema-message transport fetch/sync helpers, authority leases, stream catalog, branded `CultMesh` facade, WebSocket transport profile/connection helper, 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 @@ -70,7 +70,9 @@ CultMesh. C#/TypeScript/Rust/Python/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. + backfilling C# server responsibilities it should not own. Kotlin WebSocket + and RUDP adapters now share one `CultNetSchemaMessageTransport` helper shape + for schema/shard catalog request and sync flows. - Keep performance claims evidence-bound: C# owns SoA; Python has benchmark comparison gates; other runtimes should advertise measured native hot paths only when those paths exist. diff --git a/packages/cultmesh-kotlin/README.md b/packages/cultmesh-kotlin/README.md index f3204d61..d928cce8 100644 --- a/packages/cultmesh-kotlin/README.md +++ b/packages/cultmesh-kotlin/README.md @@ -101,10 +101,13 @@ transport.syncSchemaCatalog( The WebSocket adapter advertises a `websocket` transport profile with one reliable ordered `schema` channel and exposes transfer stats. `sendSchema`, `sendSchemaMessage`, `receiveSchema`, and `receiveSchemaMessage` keep the -schema lane at the same ergonomic level as RUDP. `fetchSchemaCatalog`, -`syncSchemaCatalog`, `fetchShardCatalog`, and `syncShardCatalog` mirror the -Python client helpers without creating a second catalog owner. It is the stream -adapter; RUDP remains the portable realtime UDP path. +schema lane at the same ergonomic level as RUDP. WebSocket and RUDP both +implement `CultNetSchemaMessageTransport`, so `fetchSchemaCatalog`, +`syncSchemaCatalog`, `fetchShardCatalog`, and `syncShardCatalog` are shared +schema-message transport helpers rather than duplicated adapter opinions. The +catalogs still own imported state; the transport only performs the standard +request/response hop. WebSocket is the stream adapter; RUDP remains the +portable realtime UDP path. Schema catalogs are also first-class, so Kotlin peers can publish and consume descriptor responses instead of hand-assembling maps: @@ -215,8 +218,9 @@ harness. `sendSchema`, `sendLatest`, and `sendRealtime` select the shared channel semantics; they do not create a Kotlin-only dialect. For a remote peer that already has its own receive loop, use `connectAndWait(...)`; for two same-process transports, use `pumpRudpPairUntilConnected(...)` to drive both -sides through the handshake. RUDP transports expose the same catalog helpers as -WebSocket, with timeout-aware receive loops: `fetchSchemaCatalog`, +sides through the handshake. Because RUDP also implements +`CultNetSchemaMessageTransport`, it uses the same catalog helpers as WebSocket, +with timeout-aware receive loops: `fetchSchemaCatalog`, `fetchSchemaDescriptors`, `syncSchemaCatalog`, `fetchShardCatalog`, `fetchShardDescriptors`, and `syncShardCatalog`. 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 75a1c610..ff9f3035 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 @@ -391,6 +391,15 @@ data class CultNetTransportStats( data class CultNetTransportFrame(val channelId: String, val payload: ByteArray) +interface CultNetSchemaMessageTransport : AutoCloseable { + val profile: CultNetTransportProfile + val stats: CultNetTransportStats + + fun sendSchemaMessage(message: CultNetMessage) + + fun receiveSchemaMessage(timeoutMs: Long = 1_000, pollIntervalMs: Long = 5): CultNetMessage? +} + data class CultNetRudpEndpoint(val host: String, val port: Int) { val uri: String get() = "rudp://$host:$port" } @@ -2352,7 +2361,7 @@ class CultNetRudpSocketTransportConnection( resendDelayMs: Long = 250, private val maxFragmentBytes: Int? = null, maxPendingReliablePackets: Int? = null, -) : AutoCloseable { +) : CultNetSchemaMessageTransport { private val session = CultNetRudpSession(CultNetRudpSessionOptions(connectionId, initialSequence, resendDelayMs, maxPendingReliablePackets)) private var remote = remoteAddress private val delivered = ArrayDeque() @@ -2364,7 +2373,7 @@ class CultNetRudpSocketTransportConnection( var disconnectReason: ByteArray? = null private set - val profile: CultNetTransportProfile = createRudpTransportProfile( + override val profile: CultNetTransportProfile = createRudpTransportProfile( runtimeId = runtimeId, host = socket.localAddress.hostAddress, port = socket.localPort, @@ -2373,7 +2382,7 @@ class CultNetRudpSocketTransportConnection( ) val connected: Boolean get() = session.connected val localPort: Int get() = socket.localPort - val stats: CultNetTransportStats get() = CultNetTransportStats(bytesReceived, bytesSent, framesReceived, framesSent) + override val stats: CultNetTransportStats get() = CultNetTransportStats(bytesReceived, bytesSent, framesReceived, framesSent) fun connect(payload: ByteArray = ByteArray(0)) { if (mode != CultNetRudpSocketMode.Client) throw IOException("Only a client RUDP socket transport can initiate connect") @@ -2412,7 +2421,7 @@ class CultNetRudpSocketTransportConnection( fun sendSchema(payload: String) = sendSchema(payload.toByteArray(StandardCharsets.UTF_8)) - fun sendSchemaMessage(message: CultNetMessage) = sendSchema(message.toBytes()) + override fun sendSchemaMessage(message: CultNetMessage) = sendSchema(message.toBytes()) fun sendLatest(payload: ByteArray) = send("latest", payload) @@ -2452,7 +2461,7 @@ class CultNetRudpSocketTransportConnection( fun receiveSchema(timeoutMs: Long, pollIntervalMs: Long = 5): ByteArray? = receiveUntil(timeoutMs, pollIntervalMs) { it.channelId == "schema" }?.payload - fun receiveSchemaMessage(timeoutMs: Long, pollIntervalMs: Long = 5): CultNetMessage? = + override fun receiveSchemaMessage(timeoutMs: Long, pollIntervalMs: Long): CultNetMessage? = receiveSchema(timeoutMs, pollIntervalMs)?.let { parseCultNetMessage(it) } fun receiveOnce(): CultNetTransportFrame? { @@ -3927,6 +3936,18 @@ class CultNetWebSocketClient private constructor( return CultNetFrame(opcode, payload) } + fun readFrame(timeoutMs: Long): CultNetFrame? { + val previousTimeout = socket.soTimeout + socket.soTimeout = timeoutMs.coerceAtLeast(1).coerceAtMost(Int.MAX_VALUE.toLong()).toInt() + return try { + readFrame() + } catch (_: SocketTimeoutException) { + null + } finally { + socket.soTimeout = previousTimeout + } + } + @Synchronized fun sendBinary(payload: ByteArray) { val mask = ByteArray(4) @@ -4031,9 +4052,9 @@ private fun writeUnmaskedWebSocketBinaryPayload(output: OutputStream, payload: B class CultNetWebSocketTransportConnection( private val client: CultNetWebSocketClient, - val profile: CultNetTransportProfile, -) : AutoCloseable { - var stats: CultNetTransportStats = CultNetTransportStats() + override val profile: CultNetTransportProfile, +) : CultNetSchemaMessageTransport { + override var stats: CultNetTransportStats = CultNetTransportStats() private set companion object { @@ -4068,7 +4089,7 @@ class CultNetWebSocketTransportConnection( fun sendSchema(payload: String) = sendSchema(payload.toByteArray(StandardCharsets.UTF_8)) - fun sendSchemaMessage(message: CultNetMessage) = sendSchema(message.toBytes()) + override fun sendSchemaMessage(message: CultNetMessage) = sendSchema(message.toBytes()) fun receive(): CultNetTransportFrame? { val frame = client.readFrame() @@ -4077,75 +4098,28 @@ class CultNetWebSocketTransportConnection( return CultNetTransportFrame("schema", frame.payload) } + fun receive(timeoutMs: Long): CultNetTransportFrame? { + val frame = client.readFrame(timeoutMs) ?: return null + if (frame.opcode != 2) return null + stats = stats.copy(bytesReceived = stats.bytesReceived + frame.payload.size, framesReceived = stats.framesReceived + 1) + return CultNetTransportFrame("schema", frame.payload) + } + fun receiveSchema(): ByteArray? = receive()?.takeIf { it.channelId == "schema" }?.payload + fun receiveSchema(timeoutMs: Long): ByteArray? = receive(timeoutMs)?.takeIf { it.channelId == "schema" }?.payload + fun receiveSchemaMessage(): CultNetMessage? = receiveSchema()?.let { parseCultNetMessage(it) } + override fun receiveSchemaMessage(timeoutMs: Long, pollIntervalMs: Long): CultNetMessage? = + receiveSchema(timeoutMs)?.let { parseCultNetMessage(it) } + override fun close() { client.close() } } -fun CultNetWebSocketTransportConnection.fetchSchemaCatalog( - messageId: String = "kotlin-schema-catalog", - includeSchemaJson: Boolean = false, - schemaIds: List = emptyList(), - kinds: List = emptyList(), -): CultNetMessage { - sendSchemaMessage(cultNetSchemaCatalogRequest(messageId, includeSchemaJson, schemaIds, kinds)) - return requireSchemaResponse( - receiveSchemaMessage(), - "cultnet.schema_catalog_response.v0", - "WebSocket schema catalog", - ) -} - -fun CultNetWebSocketTransportConnection.fetchSchemaDescriptors( - messageId: String = "kotlin-schema-catalog", - includeSchemaJson: Boolean = false, - schemaIds: List = emptyList(), - kinds: List = emptyList(), -): List = - CultNetSchemaCatalog().applyResponse(fetchSchemaCatalog(messageId, includeSchemaJson, schemaIds, kinds)) - -fun CultNetWebSocketTransportConnection.syncSchemaCatalog( - catalog: CultNetSchemaCatalog, - messageId: String = "kotlin-schema-catalog", - includeSchemaJson: Boolean = false, - schemaIds: List = emptyList(), - kinds: List = emptyList(), -): List = - catalog.applyResponse(fetchSchemaCatalog(messageId, includeSchemaJson, schemaIds, kinds)) - -fun CultNetWebSocketTransportConnection.fetchShardCatalog( - messageId: String = "kotlin-shard-catalog", - schemaIds: List = emptyList(), - recordKeys: List = emptyList(), -): CultNetMessage { - sendSchemaMessage(cultNetShardCatalogRequest(messageId, schemaIds, recordKeys)) - return requireSchemaResponse( - receiveSchemaMessage(), - "cultnet.shard_catalog_response.v0", - "WebSocket shard catalog", - ) -} - -fun CultNetWebSocketTransportConnection.fetchShardDescriptors( - messageId: String = "kotlin-shard-catalog", - schemaIds: List = emptyList(), - recordKeys: List = emptyList(), -): List = - CultNetShardCatalog().applyResponse(fetchShardCatalog(messageId, schemaIds, recordKeys)) - -fun CultNetWebSocketTransportConnection.syncShardCatalog( - catalog: CultNetShardCatalog, - messageId: String = "kotlin-shard-catalog", - schemaIds: List = emptyList(), - recordKeys: List = emptyList(), -): List = - catalog.applyResponse(fetchShardCatalog(messageId, schemaIds, recordKeys)) - -fun CultNetRudpSocketTransportConnection.fetchSchemaCatalog( +fun CultNetSchemaMessageTransport.fetchSchemaCatalog( messageId: String = "kotlin-schema-catalog", includeSchemaJson: Boolean = false, schemaIds: List = emptyList(), @@ -4157,7 +4131,83 @@ fun CultNetRudpSocketTransportConnection.fetchSchemaCatalog( return requireSchemaResponse( receiveSchemaMessage(timeoutMs, pollIntervalMs), "cultnet.schema_catalog_response.v0", - "RUDP schema catalog", + "${schemaTransportLabel()} schema catalog", + ) +} + +fun CultNetSchemaMessageTransport.fetchSchemaDescriptors( + messageId: String = "kotlin-schema-catalog", + includeSchemaJson: Boolean = false, + schemaIds: List = emptyList(), + kinds: List = emptyList(), + timeoutMs: Long = 1_000, + pollIntervalMs: Long = 5, +): List = + CultNetSchemaCatalog().applyResponse(fetchSchemaCatalog(messageId, includeSchemaJson, schemaIds, kinds, timeoutMs, pollIntervalMs)) + +fun CultNetSchemaMessageTransport.syncSchemaCatalog( + catalog: CultNetSchemaCatalog, + messageId: String = "kotlin-schema-catalog", + includeSchemaJson: Boolean = false, + schemaIds: List = emptyList(), + kinds: List = emptyList(), + timeoutMs: Long = 1_000, + pollIntervalMs: Long = 5, +): List = + catalog.applyResponse(fetchSchemaCatalog(messageId, includeSchemaJson, schemaIds, kinds, timeoutMs, pollIntervalMs)) + +fun CultNetSchemaMessageTransport.fetchShardCatalog( + messageId: String = "kotlin-shard-catalog", + schemaIds: List = emptyList(), + recordKeys: List = emptyList(), + timeoutMs: Long = 1_000, + pollIntervalMs: Long = 5, +): CultNetMessage { + sendSchemaMessage(cultNetShardCatalogRequest(messageId, schemaIds, recordKeys)) + return requireSchemaResponse( + receiveSchemaMessage(timeoutMs, pollIntervalMs), + "cultnet.shard_catalog_response.v0", + "${schemaTransportLabel()} shard catalog", + ) +} + +fun CultNetSchemaMessageTransport.fetchShardDescriptors( + messageId: String = "kotlin-shard-catalog", + schemaIds: List = emptyList(), + recordKeys: List = emptyList(), + timeoutMs: Long = 1_000, + pollIntervalMs: Long = 5, +): List = + CultNetShardCatalog().applyResponse(fetchShardCatalog(messageId, schemaIds, recordKeys, timeoutMs, pollIntervalMs)) + +fun CultNetSchemaMessageTransport.syncShardCatalog( + catalog: CultNetShardCatalog, + messageId: String = "kotlin-shard-catalog", + schemaIds: List = emptyList(), + recordKeys: List = emptyList(), + timeoutMs: Long = 1_000, + pollIntervalMs: Long = 5, +): List = + catalog.applyResponse(fetchShardCatalog(messageId, schemaIds, recordKeys, timeoutMs, pollIntervalMs)) + +private fun CultNetSchemaMessageTransport.schemaTransportLabel(): String = + profile.transports.firstOrNull()?.protocol ?: "CultNet schema-message transport" + +fun CultNetRudpSocketTransportConnection.fetchSchemaCatalog( + messageId: String = "kotlin-schema-catalog", + includeSchemaJson: Boolean = false, + schemaIds: List = emptyList(), + kinds: List = emptyList(), + timeoutMs: Long = 1_000, + pollIntervalMs: Long = 5, +): CultNetMessage { + return (this as CultNetSchemaMessageTransport).fetchSchemaCatalog( + messageId, + includeSchemaJson, + schemaIds, + kinds, + timeoutMs, + pollIntervalMs, ) } @@ -4169,7 +4219,7 @@ fun CultNetRudpSocketTransportConnection.fetchSchemaDescriptors( timeoutMs: Long = 1_000, pollIntervalMs: Long = 5, ): List = - CultNetSchemaCatalog().applyResponse(fetchSchemaCatalog(messageId, includeSchemaJson, schemaIds, kinds, timeoutMs, pollIntervalMs)) + (this as CultNetSchemaMessageTransport).fetchSchemaDescriptors(messageId, includeSchemaJson, schemaIds, kinds, timeoutMs, pollIntervalMs) fun CultNetRudpSocketTransportConnection.syncSchemaCatalog( catalog: CultNetSchemaCatalog, @@ -4180,7 +4230,7 @@ fun CultNetRudpSocketTransportConnection.syncSchemaCatalog( timeoutMs: Long = 1_000, pollIntervalMs: Long = 5, ): List = - catalog.applyResponse(fetchSchemaCatalog(messageId, includeSchemaJson, schemaIds, kinds, timeoutMs, pollIntervalMs)) + (this as CultNetSchemaMessageTransport).syncSchemaCatalog(catalog, messageId, includeSchemaJson, schemaIds, kinds, timeoutMs, pollIntervalMs) fun CultNetRudpSocketTransportConnection.fetchShardCatalog( messageId: String = "kotlin-shard-catalog", @@ -4189,11 +4239,12 @@ fun CultNetRudpSocketTransportConnection.fetchShardCatalog( timeoutMs: Long = 1_000, pollIntervalMs: Long = 5, ): CultNetMessage { - sendSchemaMessage(cultNetShardCatalogRequest(messageId, schemaIds, recordKeys)) - return requireSchemaResponse( - receiveSchemaMessage(timeoutMs, pollIntervalMs), - "cultnet.shard_catalog_response.v0", - "RUDP shard catalog", + return (this as CultNetSchemaMessageTransport).fetchShardCatalog( + messageId, + schemaIds, + recordKeys, + timeoutMs, + pollIntervalMs, ) } @@ -4204,7 +4255,7 @@ fun CultNetRudpSocketTransportConnection.fetchShardDescriptors( timeoutMs: Long = 1_000, pollIntervalMs: Long = 5, ): List = - CultNetShardCatalog().applyResponse(fetchShardCatalog(messageId, schemaIds, recordKeys, timeoutMs, pollIntervalMs)) + (this as CultNetSchemaMessageTransport).fetchShardDescriptors(messageId, schemaIds, recordKeys, timeoutMs, pollIntervalMs) fun CultNetRudpSocketTransportConnection.syncShardCatalog( catalog: CultNetShardCatalog, @@ -4214,7 +4265,7 @@ fun CultNetRudpSocketTransportConnection.syncShardCatalog( timeoutMs: Long = 1_000, pollIntervalMs: Long = 5, ): List = - catalog.applyResponse(fetchShardCatalog(messageId, schemaIds, recordKeys, timeoutMs, pollIntervalMs)) + (this as CultNetSchemaMessageTransport).syncShardCatalog(catalog, messageId, schemaIds, recordKeys, timeoutMs, pollIntervalMs) private fun requireSchemaResponse(message: CultNetMessage?, expectedSchemaVersion: String, label: String): CultNetMessage { val response = message ?: throw IOException("$label request did not receive a schema message")