Skip to content

Commit e670778

Browse files
committed
update to ktor 3.0.0
1 parent 6ef4547 commit e670778

File tree

7 files changed

+29
-80
lines changed

7 files changed

+29
-80
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
66
plugins {
77
alias(libs.plugins.multiplatform)
88
alias(libs.plugins.serialization)
9-
alias(libs.plugins.complete.kotlin)
9+
// alias(libs.plugins.complete.kotlin)
1010
alias(libs.plugins.publish)
1111
alias(libs.plugins.versions)
1212
`maven-publish`

gradle/libs.versions.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[versions]
22
complete-kotlin = "1.1.0"
3-
coroutines = "1.8.1"
3+
coroutines = "1.9.0"
44
immutable = "0.3.7"
5-
kotlin = "2.0.0"
6-
ktor = "2.3.12"
5+
kotlin = "2.0.20"
6+
ktor = "3.0.0"
77
publish = "0.29.0"
8-
serialization = "1.7.1"
8+
serialization = "1.7.3"
99
tooling = "1.6.2"
1010
versions = "0.51.0"
1111

src/commonMain/kotlin/dev/datlag/k2k/connect/Connection.kt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import kotlin.properties.Delegates
99

1010
class Connection private constructor(
1111
private val port: Int,
12-
private val immediate: Boolean,
1312
private val scope: CoroutineScope
1413
) : AutoCloseable {
1514

16-
private val client = ConnectionClient(immediate)
17-
private val server = ConnectionServer(immediate)
15+
private val client = ConnectionClient()
16+
private val server = ConnectionServer()
1817

1918
private var sendJob: Job? = null
2019

@@ -50,31 +49,16 @@ class Connection private constructor(
5049

5150
class Builder(private var scope: CoroutineScope = CoroutineScope(Dispatcher.IO)) {
5251
private var port by Delegates.notNull<Int>()
53-
private var immediate: Boolean = false
5452

5553
fun setPort(port: Int) = apply {
5654
this.port = port
5755
}
5856

59-
/**
60-
* Set TCP_NODELAY socket option to disable the Nagle algorithm.
61-
*/
62-
fun noDelay() = apply {
63-
this.immediate = true
64-
}
65-
66-
/**
67-
* Set TCP_DELAY socket option to enable the Nagle algorithm.
68-
*/
69-
fun delay() = apply {
70-
this.immediate = false
71-
}
72-
7357
fun setScope(scope: CoroutineScope) = apply {
7458
this.scope = scope
7559
}
7660

77-
fun build() = Connection(port, immediate, scope)
61+
fun build() = Connection(port, scope)
7862
}
7963
}
8064

src/commonMain/kotlin/dev/datlag/k2k/connect/ConnectionClient.kt

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,10 @@ import io.ktor.network.sockets.tcpNoDelay
1313
import io.ktor.utils.io.close
1414
import io.ktor.utils.io.writeFully
1515

16-
internal class ConnectionClient(
17-
private val immediate: Boolean
18-
) : AutoCloseable {
16+
internal class ConnectionClient : AutoCloseable {
1917

2018
private var socket = scopeCatching {
21-
aSocket(SelectorManager(Dispatcher.IO)).let {
22-
if (immediate) {
23-
it.tcpNoDelay().tcp()
24-
} else {
25-
it.tcp()
26-
}
27-
}
19+
aSocket(SelectorManager(Dispatcher.IO)).tcp()
2820
}.getOrNull()
2921

3022
private var connectedSocket: Socket? = null
@@ -36,22 +28,15 @@ internal class ConnectionClient(
3628
) = suspendCatching {
3729
val socketAddress = InetSocketAddress(host.hostAddress, port)
3830
val useSocket = socket ?: suspendCatching {
39-
aSocket(SelectorManager(Dispatcher.IO)).let {
40-
if (immediate) {
41-
it.tcpNoDelay().tcp()
42-
} else {
43-
it.tcp()
44-
}
45-
}
31+
aSocket(SelectorManager(Dispatcher.IO)).tcp()
4632
}.getOrNull()?.also { socket = it } ?: return@suspendCatching
4733

4834
connectedSocket = useSocket.connect(socketAddress) {
4935
reuseAddress = true
5036
}.also {
5137
val channel = it.openWriteChannel(autoFlush = true)
5238
channel.writeFully(byteArray, 0, byteArray.size)
53-
channel.flush()
54-
channel.close()
39+
channel.flushAndClose()
5540
}
5641
}
5742

@@ -60,13 +45,7 @@ internal class ConnectionClient(
6045
connectedSocket = null
6146

6247
socket = scopeCatching {
63-
aSocket(SelectorManager(Dispatcher.IO)).let {
64-
if (immediate) {
65-
it.tcpNoDelay().tcp()
66-
} else {
67-
it.tcp()
68-
}
69-
}
48+
aSocket(SelectorManager(Dispatcher.IO)).tcp()
7049
}.getOrNull()
7150
}
7251
}

src/commonMain/kotlin/dev/datlag/k2k/connect/ConnectionServer.kt

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.ktor.network.sockets.Socket
1111
import io.ktor.network.sockets.aSocket
1212
import io.ktor.network.sockets.openReadChannel
1313
import io.ktor.network.sockets.tcpNoDelay
14+
import io.ktor.utils.io.availableForRead
1415
import io.ktor.utils.io.core.use
1516
import io.ktor.utils.io.readAvailable
1617
import kotlinx.coroutines.CoroutineScope
@@ -19,18 +20,10 @@ import kotlinx.coroutines.currentCoroutineContext
1920
import kotlinx.coroutines.isActive
2021
import kotlinx.coroutines.launch
2122

22-
internal class ConnectionServer(
23-
private val immediate: Boolean
24-
) : AutoCloseable {
23+
internal class ConnectionServer : AutoCloseable {
2524
private var receiveJob: Job? = null
2625
private var socket = scopeCatching {
27-
aSocket(SelectorManager(Dispatcher.IO)).let {
28-
if (immediate) {
29-
it.tcpNoDelay().tcp()
30-
} else {
31-
it.tcp()
32-
}
33-
}
26+
aSocket(SelectorManager(Dispatcher.IO)).tcp()
3427
}.getOrNull()
3528

3629
private var serverSocket: ServerSocket? = null
@@ -54,13 +47,7 @@ internal class ConnectionServer(
5447

5548
val socketAddress = InetSocketAddress(NetInterface.getLocalAddress(), port)
5649
val useSocket = socket ?: suspendCatching {
57-
aSocket(SelectorManager(Dispatcher.IO)).let {
58-
if (immediate) {
59-
it.tcpNoDelay().tcp()
60-
} else {
61-
it.tcp()
62-
}
63-
}
50+
aSocket(SelectorManager(Dispatcher.IO)).tcp()
6451
}.getOrNull() ?: return@suspendCatching
6552

6653
serverSocket = useSocket.bind(socketAddress) {
@@ -104,13 +91,7 @@ internal class ConnectionServer(
10491
connectedSocket = null
10592

10693
socket = scopeCatching {
107-
aSocket(SelectorManager(Dispatcher.IO)).let {
108-
if (immediate) {
109-
it.tcpNoDelay().tcp()
110-
} else {
111-
it.tcp()
112-
}
113-
}
94+
aSocket(SelectorManager(Dispatcher.IO)).tcp()
11495
}.getOrNull()
11596
}
11697
}

src/commonMain/kotlin/dev/datlag/k2k/discover/DiscoveryClient.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import dev.datlag.k2k.Dispatcher
77
import dev.datlag.k2k.NetInterface
88
import dev.datlag.tooling.async.scopeCatching
99
import dev.datlag.tooling.async.suspendCatching
10+
import io.ktor.network.sockets.Datagram
1011
import io.ktor.network.sockets.InetSocketAddress
1112
import io.ktor.network.sockets.openWriteChannel
1213
import io.ktor.utils.io.close
14+
import io.ktor.utils.io.core.ByteReadPacket
1315
import io.ktor.utils.io.writeFully
1416
import kotlinx.coroutines.CoroutineScope
1517
import kotlinx.coroutines.Job
@@ -53,10 +55,13 @@ internal class DiscoveryClient : AutoCloseable {
5355
reuseAddress = true
5456
}
5557

56-
val output = socketConnection.openWriteChannel(autoFlush = true)
57-
output.writeFully(data, 0, data.size)
58-
output.flush()
59-
output.close()
58+
socketConnection.send(
59+
Datagram(
60+
packet = ByteReadPacket(array = data),
61+
address = socketConnection.remoteAddress
62+
)
63+
)
64+
6065
socketConnection.close()
6166
}
6267

src/commonMain/kotlin/dev/datlag/k2k/discover/DiscoveryServer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
2323
import kotlinx.coroutines.flow.update
2424
import kotlinx.coroutines.isActive
2525
import kotlinx.coroutines.launch
26+
import kotlinx.io.readByteArray
2627
import kotlinx.serialization.decodeFromByteArray
2728

2829
internal class DiscoveryServer : AutoCloseable {
@@ -62,10 +63,9 @@ internal class DiscoveryServer : AutoCloseable {
6263
}
6364

6465
while (currentCoroutineContext().isActive) {
65-
serverSocket.openReadChannel()
6666
serverSocket.incoming.consumeEach { datagram ->
6767
suspendCatching {
68-
val receivedPacket = datagram.packet.readBytes()
68+
val receivedPacket = datagram.packet.readByteArray()
6969
if (receivedPacket.isNotEmpty()) {
7070
val host = Constants.protobuf.decodeFromByteArray<Host>(receivedPacket).apply {
7171
val inetSocketAddress = datagram.address as InetSocketAddress

0 commit comments

Comments
 (0)