diff --git a/.github/workflows/apple.yml b/.github/workflows/apple.yml index 5a387e1..38c1ccd 100644 --- a/.github/workflows/apple.yml +++ b/.github/workflows/apple.yml @@ -49,13 +49,14 @@ jobs: - name: The shared Kotlin as an XCFramework run: ./gradlew :shared:assembleDModemSharedReleaseXCFramework + # arm64 only: the shared Kotlin's XCFramework has no x86_64 simulator slice - name: The iOS app (simulator) run: | brew install xcodegen cd ios/DModem && xcodegen generate xcodebuild -project DModem.xcodeproj -scheme DModem \ -destination 'generic/platform=iOS Simulator' \ - CODE_SIGNING_ALLOWED=NO build + ARCHS=arm64 CODE_SIGNING_ALLOWED=NO build - uses: actions/upload-artifact@v4 with: diff --git a/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt b/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt index dcf44fd..62a935c 100644 --- a/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt +++ b/app/src/androidTest/java/com/neodesktop/dmodem/NativeModemTest.kt @@ -37,10 +37,15 @@ class NativeModemTest { NativeModem.listener = null } - /** a SIP account at nowhere (no call is made), the modem's data in the app's files */ + /** + * a SIP account at nowhere (no call is made), the modem's data in the + * app's files; SIP's log at level 4, which names its addresses + * ("SIP on ...") before start returns: at 3 a modem with no call and + * no registrar has nothing to say in the test's time + */ private fun config() = NativeModem.Config( user = "test", password = "x", domain = "127.0.0.1:5999", sipPort = 15060, - dns = listOf("127.0.0.1"), logLevel = 1, sipLogLevel = 3, + dns = listOf("127.0.0.1"), logLevel = 1, sipLogLevel = 4, dataFile = File(InstrumentationRegistry.getInstrumentation().targetContext.filesDir, "test_dsp").path, ) diff --git a/ios/DModemKit/Package.swift b/ios/DModemKit/Package.swift index 4f46149..54381d7 100644 --- a/ios/DModemKit/Package.swift +++ b/ios/DModemKit/Package.swift @@ -20,11 +20,15 @@ let engineLinking: [LinkerSetting] = [ ]), .linkedLibrary("dmodem_engine"), .linkedLibrary("dmodem_sip"), .linkedLibrary("re"), .linkedLibrary("ssl"), .linkedLibrary("crypto"), .linkedLibrary("stdc++"), .linkedLibrary("dl"), - .linkedLibrary("m"), + .linkedLibrary("m"), .linkedLibrary("resolv"), ] #else let engine: Target = .binaryTarget(name: "DModemEngine", path: "../../build-apple/DModemEngine.xcframework") -let engineLinking: [LinkerSetting] = [.linkedLibrary("c++")] +let engineLinking: [LinkerSetting] = [ + .linkedLibrary("c++"), .linkedLibrary("resolv"), + // libre asks the system for its DNS servers (srv.c, SCDynamicStore) + .linkedFramework("SystemConfiguration"), .linkedFramework("CoreFoundation"), +] #endif let package = Package( diff --git a/ios/DModemSmoke/Package.swift b/ios/DModemSmoke/Package.swift index 9039447..c6934f9 100644 --- a/ios/DModemSmoke/Package.swift +++ b/ios/DModemSmoke/Package.swift @@ -14,7 +14,11 @@ let package = Package( .testTarget( name: "DModemSmokeTests", dependencies: ["DModemEngine"], - linkerSettings: [.linkedLibrary("c++")] + linkerSettings: [ + .linkedLibrary("c++"), .linkedLibrary("resolv"), + // libre asks macOS for its DNS servers (srv.c, SCDynamicStore) + .linkedFramework("SystemConfiguration"), .linkedFramework("CoreFoundation"), + ] ), ] ) diff --git a/ios/build-native.sh b/ios/build-native.sh index 476a62e..3c157fb 100755 --- a/ios/build-native.sh +++ b/ios/build-native.sh @@ -32,7 +32,10 @@ build() { -DCMAKE_OSX_DEPLOYMENT_TARGET="$min" \ -DDMODEM_CRYPTO=mbedtls if [ "$system" = iOS ]; then - set -- "$@" -DCMAKE_SYSTEM_NAME=iOS + # (no app bundles: D-Modem-RE's dmodemd, which is never built + # here, is an executable that CMake would make a bundle on iOS, + # and its install rule has no BUNDLE DESTINATION for one) + set -- "$@" -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_MACOSX_BUNDLE=OFF fi cmake "$@" cmake --build "$dir/build" --target dmodem_engine diff --git a/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt b/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt index ce8469b..f829973 100644 --- a/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt +++ b/shared/src/commonMain/kotlin/com/neodesktop/dmodem/io/Io.kt @@ -2,10 +2,12 @@ // Copyright (C) 2026 Neo-Desktop package com.neodesktop.dmodem.io +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withTimeoutOrNull +import kotlinx.coroutines.selects.onTimeout +import kotlinx.coroutines.selects.select import kotlin.time.TimeSource /** @@ -84,11 +86,21 @@ class ByteQueue { ch.trySend(b) } - /** the next chunk, waiting up to [ms]; null if none came (the reader's thread) */ + /** + * the next chunk, waiting up to [ms]; null if none came (the reader's + * thread). (A select, not receive() in withTimeoutOrNull: a chunk sent + * just as that timed out was taken by the cancelled receive and lost.) + */ + @OptIn(ExperimentalCoroutinesApi::class) fun poll(ms: Long): ByteArray? { poll()?.let { return it } if (ms <= 0) return null - return runBlocking { withTimeoutOrNull(ms) { ch.receive() } } + return runBlocking { + select { + ch.onReceive { it } + onTimeout(ms) { null } + } + } } /** the next chunk if one waits, else null */ diff --git a/shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt new file mode 100644 index 0000000..f3e6fe2 --- /dev/null +++ b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/io/ByteQueueTest.kt @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package com.neodesktop.dmodem.io + +import kotlin.concurrent.Volatile +import kotlin.test.Test +import kotlin.test.assertEquals + +/** A ByteQueue between two threads loses nothing, whatever its reads' timeouts. */ +class ByteQueueTest { + private class Flag { @Volatile var on = false } + + /** chunks put while the reader's short waits keep timing out all come, in order */ + @Test + fun nothingLostToATimeout() { + val q = ByteQueue() + val n = 3000 + val done = Flag() + background("writer") { + for (i in 0 until n) { + q.put(byteArrayOf((i shr 8).toByte(), i.toByte())) + // (now and then just as the reader's wait runs out) + if (i % 3 == 0) sleepMs(1) + } + done.on = true + } + var next = 0 + val end = monotonicMs() + 30_000 + while (next < n && monotonicMs() < end) { + val b = q.poll(1) ?: continue + val i = ((b[0].toInt() and 0xff) shl 8) or (b[1].toInt() and 0xff) + assertEquals(next, i, "chunk $next") + next++ + } + assertEquals(n, next) + } +} diff --git a/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt index 214b39c..13ccd1a 100644 --- a/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt +++ b/shared/src/commonTest/kotlin/com/neodesktop/dmodem/peer/PeerLoopbackTest.kt @@ -34,11 +34,11 @@ class PeerLoopbackTest { /** a pipe's end: reads [inw], writes [outw] */ private class End(private val inw: Way, private val outw: Way, override val name: String) : Duplex { override val input = object : Source { - /** the next of what came the other way, into [b] (waited for): how much, -1 once closed */ + /** the next of what came the other way, into [b] (waited for): how much, -1 once closed and read to its end */ override fun read(b: ByteArray, off: Int, len: Int): Int { while (inw.at >= inw.cur.size) { - if (inw.closed) return -1 - inw.cur = inw.q.poll(50) ?: continue + // (what was written before the close still comes, as on a socket: a DENIED, say) + inw.cur = inw.q.poll() ?: if (inw.closed) return -1 else inw.q.poll(50) ?: continue inw.at = 0 } val n = minOf(len, inw.cur.size - inw.at)