Skip to content

Commit

Permalink
Add workaround for nw_parameters_create_secure_udp
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Mar 29, 2024
1 parent 3a1098b commit d89a042
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ kotlin {
binaryOption("bundleId", "com.tidal.networktime")
isStatic = true
}
it.compilations.configureEach {
cinterops.create("cinterop") {
defFile(project.file("src/appleMain/cinterop/network_framework.def"))
}
}
}

applyDefaultHierarchyTemplate()
Expand Down
14 changes: 14 additions & 0 deletions library/src/appleMain/cinterop/network_framework.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package = com.tidal.networktime.network_framework
language = Objective-C

---
// https://stackoverflow.com/a/63050804

#include <Network/Network.h>

NW_RETURNS_RETAINED nw_parameters_t nw_parameters_create_secure_udp_workaround() {
return nw_parameters_create_secure_udp(
withTls ? NW_PARAMETERS_DEFAULT_CONFIGURATION : NW_PARAMETERS_DISABLE_PROTOCOL,
NW_PARAMETERS_DEFAULT_CONFIGURATION
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import kotlinx.cinterop.set
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.withTimeout
import platform.Network.NW_CONNECTION_FINAL_MESSAGE_CONTEXT
import platform.Network.NW_PARAMETERS_DEFAULT_CONFIGURATION
import platform.Network.NW_PARAMETERS_DISABLE_PROTOCOL
import platform.Network.nw_connection_create
import platform.Network.nw_connection_force_cancel
import platform.Network.nw_connection_receive
Expand All @@ -27,12 +25,11 @@ import platform.Network.nw_connection_state_t
import platform.Network.nw_connection_t
import platform.Network.nw_endpoint_create_host
import platform.Network.nw_error_t
import platform.Network.nw_parameters_create_secure_udp
import platform.darwin._dispatch_data_destructor_free
import platform.darwin.dispatch_data_apply
import platform.darwin.dispatch_data_create
import platform.darwin.dispatch_data_t
import platform.darwin.dispatch_get_current_queue
import platform.darwin.dispatch_get_main_queue
import platform.posix.memcpy
import kotlin.test.assertEquals
import kotlin.test.assertNull
Expand All @@ -43,28 +40,37 @@ internal actual class NTPUDPSocketOperations {
private var connection: nw_connection_t = null

actual suspend fun prepare(address: String, portNumber: Int, connectTimeout: Duration) {
val parameters = nw_parameters_create_secure_udp(
NW_PARAMETERS_DISABLE_PROTOCOL,
NW_PARAMETERS_DEFAULT_CONFIGURATION,
)
val endpoint = nw_endpoint_create_host(address, portNumber.toString())
connection = nw_connection_create(endpoint, parameters)
nw_connection_set_queue(connection, dispatch_get_current_queue())
println("BEFORE CREATESECUREUDP")
val parameters = nw_parameters_create_secure_udp_workaround()
println("BEFORE CREATEHOST")
println("Endpoint is $address/$portNumber")
val endpoint = nw_endpoint_create_host("localhost", "123")
println("BEFORE CONNCREATE")
connection = nw_connection_create(endpoint, null)
println("BEFORE SETQUEUE")
nw_connection_set_queue(connection, dispatch_get_main_queue())
val connectionStateDeferred = CompletableDeferred<nw_connection_state_t>()
println("BEFORE SETHANDLER")
nw_connection_set_state_changed_handler(connection) { state: nw_connection_state_t, _ ->
println("State is $state")
when (state) {
nw_connection_state_ready, nw_connection_state_failed, nw_connection_state_cancelled ->
connectionStateDeferred.complete(state)
}
}
println("Print test")
println("BEFORE START")
nw_connection_start(connection)
println("BEFORE WITHTIMEOUT")
withTimeout(connectTimeout) {
assertEquals(nw_connection_state_ready, connectionStateDeferred.await())
}
println("AFTER WITHTIMEOUT")
}

@OptIn(ExperimentalForeignApi::class)
actual suspend fun exchange(buffer: ByteArray, readTimeout: Duration) {
println("EXCHANGE")
val toSendData = memScoped {
val cArray = allocArray<ByteVar>(buffer.size)
buffer.forEachIndexed { i, it ->
Expand Down Expand Up @@ -99,6 +105,7 @@ internal actual class NTPUDPSocketOperations {
}

actual fun tearDown() {
println("TEARDOWN")
nw_connection_force_cancel(connection)
connection = null
}
Expand Down

0 comments on commit d89a042

Please sign in to comment.