Fix native CLI TCP regression path
This commit is contained in:
1 parent
aa1b74620e
commit
402b8bb1b3
14 files changed
+204
-22
No files matched your search
@@ -28,6 +28,8 @@ import java.net.InetAddress
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = AndroidKtorNetEngine
|
||||
|
||||
actual fun shutdownSystemNetEngine() {}
|
||||
|
||||
private object AndroidKtorNetEngine : LyngNetEngine {
|
||||
private val selectorManager: SelectorManager by lazy { ActorSelectorManager(Dispatchers.IO) }
|
||||
|
||||
|
||||
@@ -97,3 +97,5 @@ internal object UnsupportedLyngNetEngine : LyngNetEngine {
|
||||
}
|
||||
|
||||
expect fun getSystemNetEngine(): LyngNetEngine
|
||||
|
||||
expect fun shutdownSystemNetEngine()
|
||||
@@ -1,8 +1,14 @@
|
||||
package net.sergeych.lyngio.net
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = createNativeKtorNetEngine(
|
||||
private val systemNetEngine: LyngNetEngine = createNativeKtorNetEngine(
|
||||
isSupported = true,
|
||||
isTcpAvailable = true,
|
||||
isTcpServerAvailable = true,
|
||||
isUdpAvailable = true,
|
||||
)
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = systemNetEngine
|
||||
|
||||
actual fun shutdownSystemNetEngine() {
|
||||
shutdownNativeKtorNetEngine(systemNetEngine)
|
||||
}
|
||||
@@ -13,6 +13,8 @@ import org.khronos.webgl.Uint8Array
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = jsNodeNetEngineOrNull ?: UnsupportedLyngNetEngine
|
||||
|
||||
actual fun shutdownSystemNetEngine() {}
|
||||
|
||||
private val jsNodeNetEngineOrNull: LyngNetEngine? by lazy {
|
||||
if (!isNodeRuntime()) return@lazy null
|
||||
val net = requireNodeModule("net") ?: return@lazy null
|
||||
|
||||
@@ -45,6 +45,8 @@ import java.net.InetAddress
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = JvmKtorNetEngine
|
||||
|
||||
actual fun shutdownSystemNetEngine() {}
|
||||
|
||||
private object JvmKtorNetEngine : LyngNetEngine {
|
||||
private val selectorManager: SelectorManager by lazy { ActorSelectorManager(Dispatchers.IO) }
|
||||
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package net.sergeych.lyngio.net
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = createNativeKtorNetEngine(
|
||||
private val systemNetEngine: LyngNetEngine = createNativeKtorNetEngine(
|
||||
isSupported = true,
|
||||
isTcpAvailable = true,
|
||||
isTcpServerAvailable = true,
|
||||
isUdpAvailable = true,
|
||||
)
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = systemNetEngine
|
||||
|
||||
actual fun shutdownSystemNetEngine() {
|
||||
shutdownNativeKtorNetEngine(systemNetEngine)
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
package net.sergeych.lyngio.net
|
||||
|
||||
actual fun getSystemNetEngine(): LyngNetEngine = UnsupportedLyngNetEngine
|
||||
|
||||
actual fun shutdownSystemNetEngine() {}
|
||||
@@ -40,7 +40,10 @@ private class NativeKtorNetEngine(
|
||||
override val isTcpServerAvailable: Boolean,
|
||||
override val isUdpAvailable: Boolean,
|
||||
) : LyngNetEngine {
|
||||
private val selectorManager: SelectorManager by lazy { SelectorManager(Dispatchers.Default) }
|
||||
private var selectorManager: SelectorManager? = null
|
||||
|
||||
private fun selectorManager(): SelectorManager =
|
||||
selectorManager ?: SelectorManager(Dispatchers.Default).also { selectorManager = it }
|
||||
|
||||
override suspend fun resolve(host: String, port: Int): List<LyngSocketAddress> {
|
||||
val rawAddress = InetSocketAddress(host, port).resolveAddress()
|
||||
@@ -62,7 +65,7 @@ private class NativeKtorNetEngine(
|
||||
noDelay: Boolean,
|
||||
): LyngTcpSocket {
|
||||
val connectBlock: suspend () -> Socket = {
|
||||
aSocket(selectorManager).tcp().connect(host, port) {
|
||||
aSocket(selectorManager()).tcp().connect(host, port) {
|
||||
this.noDelay = noDelay
|
||||
}
|
||||
}
|
||||
@@ -77,7 +80,7 @@ private class NativeKtorNetEngine(
|
||||
reuseAddress: Boolean,
|
||||
): LyngTcpServer {
|
||||
val bindHost = host ?: "0.0.0.0"
|
||||
val server = aSocket(selectorManager).tcp().bind(bindHost, port) {
|
||||
val server = aSocket(selectorManager()).tcp().bind(bindHost, port) {
|
||||
backlogSize = backlog
|
||||
this.reuseAddress = reuseAddress
|
||||
}
|
||||
@@ -86,11 +89,16 @@ private class NativeKtorNetEngine(
|
||||
|
||||
override suspend fun udpBind(host: String?, port: Int, reuseAddress: Boolean): LyngUdpSocket {
|
||||
val bindHost = host ?: "0.0.0.0"
|
||||
val socket = aSocket(selectorManager).udp().bind(bindHost, port) {
|
||||
val socket = aSocket(selectorManager()).udp().bind(bindHost, port) {
|
||||
this.reuseAddress = reuseAddress
|
||||
}
|
||||
return NativeLyngUdpSocket(socket)
|
||||
}
|
||||
|
||||
fun shutdown() {
|
||||
selectorManager?.close()
|
||||
selectorManager = null
|
||||
}
|
||||
}
|
||||
|
||||
private class NativeLyngTcpSocket(
|
||||
@@ -214,3 +222,7 @@ private fun ByteArray.toIpHostString(): String = when (size) {
|
||||
}
|
||||
else -> error("Unsupported IP address length: $size")
|
||||
}
|
||||
|
||||
internal fun shutdownNativeKtorNetEngine(engine: LyngNetEngine) {
|
||||
(engine as? NativeKtorNetEngine)?.shutdown()
|
||||
}
|
||||
Reference in new issue
Block a user