feat(network): migrate TcpTransport to ktor-network (commonMain)#5995
Merged
Conversation
Replaces the java.net.Socket + java.io blocking I/O in TcpTransport with Ktor raw sockets (aSocket/openReadChannel/openWriteChannel), enabling both TcpTransport and TcpRadioTransport to live in commonMain. They were pinned to jvmAndroidMain solely by java.* imports — the framing codec, reconnect logic, and consumer were already platform-agnostic. The inactivity timeout is preserved: withTimeoutOrNull wrapping readAvailable reproduces the old Socket.soTimeout resumable-count pattern. A new spike test confirms that Ktor's read channel survives a timeout cancellation and resumes cleanly on the next iteration (the assumption the whole migration rests on). StreamFrameCodec.frameAndSend lambdas widened to suspend (backward-compat: plain function refs are assignable to suspend types). Atomics migrated to atomicfu (already a commonMain dep). jvmAndroidMain transport/radio dirs are now empty and removed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
🌟 KMP commonization of TCP radio transport
TcpTransportandTcpRadioTransportwere pinned tojvmAndroidMainsolely byjava.net.Socket+java.ioblocking I/O. Everything around them was already common: the START1/START2 framing codec, the reconnect/backoff logic, and the consumer (TcpRadioTransport). This PR replaces the platform socket layer with Ktor raw sockets (ktor-network), moving both classes tocommonMainand making TCP radio connectivity available to all KMP targets.What changed
TcpTransport.ktjvmAndroidMain → commonMain:java.net.Socket+ blocking streams →aSocket().tcp()/openReadChannel/openWriteChannel. Atomics migrated fromjava.util.concurrent.atomicto atomicfu (already acommonMaindep).jvmAndroidMaintransport/radio dirs now empty and removed.TcpRadioTransport.ktjvmAndroidMain → commonMain: pure file move — no edits needed onceTcpTransportis common.StreamFrameCodec.frameAndSendlambdas widened from(ByteArray) -> Unittosuspend (ByteArray) -> Unit(Ktor writes are suspend; backward-compatible — plain function refs are assignable to suspend types).build.gradle.kts: +implementation(libs.ktor.network)incommonMain.TcpTransportTest.kt(new,commonTest): two tests — the inactivity-timeout spike and an end-to-end framed-packet round-trip.The critical risk, validated
The inactivity timeout logic (18 × 5s = 90s before disconnect) relied on
Socket.soTimeoutthrowingSocketTimeoutExceptionon each tick while keeping the stream open. The Ktor equivalent wrapsreadAvailableinwithTimeoutOrNull. The spike test confirms that Ktor'sByteReadChannelsurvives awithTimeoutOrNullcancellation and resumes cleanly on the next iteration — no watchdog fallback needed.Not included (deliberate)
Testing Performed
./gradlew :core:network:allTests— both newTcpTransportTestcases pass (JVM)./gradlew spotlessCheck detekt— clean./gradlew :core:network:assembleDebug :desktopApp:compileKotlin— both downstream consumers compile