Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First working version of the library #54

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build
on: push
jobs:
root:
runs-on: macos-13
runs-on: macos-14-xlarge
steps:
- uses: actions/[email protected]
- uses: ./.github/actions/runGradleTask
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependencyReport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ permissions:
contents: write
jobs:
library:
runs-on: macos-13
runs-on: macos-14-xlarge
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/staticAnalysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/[email protected]
- run: .scripts/check_ktlint.sh
codeql:
runs-on: macos-13
runs-on: macos-14-xlarge
permissions:
actions: read
contents: read
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ kotlin {
macosX64(),
macosArm64(),
iosX64(),
iosArm64(),
// iosArm64(),
iosSimulatorArm64(),
tvosSimulatorArm64(),
tvosX64(),
tvosArm64(),
// tvosArm64(),
).forEach {
it.binaries.framework {
baseName = project.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import kotlinx.cinterop.convert
import kotlinx.cinterop.pin
import kotlinx.cinterop.usePinned
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.withTimeout
import platform.Network.nw_connection_create
import platform.Network.nw_connection_force_cancel
import platform.Network.nw_connection_receive
Expand All @@ -29,44 +28,29 @@ import platform.darwin.dispatch_get_current_queue
import platform.posix.memcpy
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.time.Duration

@OptIn(ExperimentalForeignApi::class)
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
internal actual class NTPUDPSocketOperations {
private var connection: nw_connection_t = null

actual suspend fun prepare(address: String, portNumber: Int, connectTimeout: Duration) {
println("BEFORE CREATESECUREUDP")
actual suspend fun prepare(address: String, portNumber: Int) {
val parameters = nw_parameters_create_secure_udp_disable_protocol()
println("BEFORE CREATEHOST")
println("Endpoint is $address/$portNumber")
val endpoint = nw_endpoint_create_host(address, portNumber.toString())
println("BEFORE CONNCREATE")
connection = nw_connection_create(endpoint, parameters)
println("BEFORE SETQUEUE")
nw_connection_set_queue(connection, dispatch_get_current_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")
assertEquals(nw_connection_state_ready, connectionStateDeferred.await())
}

actual suspend fun exchange(buffer: ByteArray, readTimeout: Duration) {
println("EXCHANGE")
actual suspend fun exchange(buffer: ByteArray) {
val data = buffer.pin().run {
dispatch_data_create(
addressOf(0),
Expand All @@ -75,31 +59,23 @@ internal actual class NTPUDPSocketOperations {
({ unpin() }),
)
}
println("BEFORE SEND")
nw_connection_send_default_context(
connection,
data,
true,
) {
println("SEND CB, ERROR IS $it")
assertNull(it)
}
val connectionReceptionDeferred = CompletableDeferred<dispatch_data_t>()
println("BEFORE RECEIVE")
nw_connection_receive(
connection,
1.convert(),
buffer.size.convert(),
) { content: dispatch_data_t, _, _, error: nw_error_t ->
println("RECEIVE CB, ERROR IS $error")
assertNull(error)
connectionReceptionDeferred.complete(content)
}
println("BEFORE RECEIVE TIMEOUT")
val receivedData = withTimeout(readTimeout) {
connectionReceptionDeferred.await()
}
println("BEFORE USEPINNED FOR RECEIVED")
val receivedData = connectionReceptionDeferred.await()
buffer.usePinned {
dispatch_data_apply(receivedData) { _, offset, src, size ->
memcpy(it.addressOf(offset.toInt()), src, size)
Expand All @@ -109,7 +85,6 @@ internal actual class NTPUDPSocketOperations {
}

actual fun tearDown() {
println("TEARDOWN")
nw_connection_force_cancel(connection)
connection = null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.withTimeout
import kotlin.time.Duration

internal class NTPExchangeCoordinator(
Expand All @@ -15,12 +16,16 @@ internal class NTPExchangeCoordinator(
): NTPExchangeResult? {
val ntpUdpSocketOperations = NTPUDPSocketOperations()
return try {
ntpUdpSocketOperations.prepare(address, NTP_PORT_NUMBER, connectTimeout)
withTimeout(connectTimeout) {
ntpUdpSocketOperations.prepare(address, NTP_PORT_NUMBER)
}
val ntpPacket = NTPPacket(versionNumber = ntpVersion.toInt(), mode = NTP_MODE_CLIENT)
val requestTime = referenceClock.referenceEpochTime
ntpPacket.transmitEpochTimestamp = EpochTimestamp(requestTime).asNTPTimestamp
val buffer = ntpPacketSerializer(ntpPacket)
ntpUdpSocketOperations.exchange(buffer, queryReadTimeout)
withTimeout(queryReadTimeout) {
ntpUdpSocketOperations.exchange(buffer)
}
val returnTime = referenceClock.referenceEpochTime
ntpPacketDeserializer(buffer)?.let { NTPExchangeResult(returnTime, it) }
} catch (_: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.tidal.networktime.internal

import kotlin.time.Duration

@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
internal expect class NTPUDPSocketOperations() {
suspend fun prepare(address: String, portNumber: Int, connectTimeout: Duration)
suspend fun prepare(address: String, portNumber: Int)

suspend fun exchange(buffer: ByteArray, readTimeout: Duration)
suspend fun exchange(buffer: ByteArray)

fun tearDown()
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.withTimeout
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import kotlin.time.Duration
import kotlin.time.DurationUnit

@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING", "BlockingMethodInNonBlockingContext")
internal actual class NTPUDPSocketOperations {
private var datagramSocket: DatagramSocket? = null

actual suspend fun prepare(address: String, portNumber: Int, connectTimeout: Duration) {
actual suspend fun prepare(address: String, portNumber: Int) {
datagramSocket = DatagramSocket()
withTimeout(connectTimeout) {
datagramSocket!!.connect(InetAddress.getByName(address), portNumber)
}
datagramSocket!!.connect(InetAddress.getByName(address), portNumber)
}

actual suspend fun exchange(buffer: ByteArray, readTimeout: Duration) {
actual suspend fun exchange(buffer: ByteArray) {
val exchangePacket = DatagramPacket(buffer, buffer.size)
datagramSocket!!.send(exchangePacket)
datagramSocket!!.soTimeout = readTimeout.toInt(DurationUnit.MILLISECONDS)
datagramSocket!!.receive(exchangePacket)
}

Expand Down
2 changes: 1 addition & 1 deletion samples/multiplatform-kotlin/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ kotlin {
macosX64(),
macosArm64(),
iosX64(),
iosArm64(),
// iosArm64(),
iosSimulatorArm64(),
).forEach {
it.binaries.framework {
Expand Down
Loading