From 22359a557068094c2e54a6de56616035bad8756d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 27 May 2023 22:44:30 +0300 Subject: [PATCH] Remove non-blocking mode for ports to avoid CPU clogging. --- .../kotlin/space/kscience/controls/ports/Port.kt | 12 +++++------- .../space/kscience/controls/ports/ChannelPort.kt | 5 +---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt index db19817..1f07251 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt @@ -18,10 +18,10 @@ public interface Port : ContextAware, Socket * A specialized factory for [Port] */ @Type(PortFactory.TYPE) -public interface PortFactory: Factory{ +public interface PortFactory : Factory { public val type: String - public companion object{ + public companion object { public const val TYPE: String = "controls.port" } } @@ -53,11 +53,9 @@ public abstract class AbstractPort( /** * Internal method to receive data synchronously */ - protected fun receive(data: ByteArray) { - scope.launch { - logger.debug { "${this@AbstractPort} RECEIVED: ${data.decodeToString()}" } - incoming.send(data) - } + protected suspend fun receive(data: ByteArray) { + logger.debug { "${this@AbstractPort} RECEIVED: ${data.decodeToString()}" } + incoming.send(data) } private val sendJob = scope.launch { diff --git a/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/ChannelPort.kt b/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/ChannelPort.kt index d0b9e1f..6ddc1ab 100644 --- a/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/ChannelPort.kt +++ b/controls-core/src/jvmMain/kotlin/space/kscience/controls/ports/ChannelPort.kt @@ -87,9 +87,7 @@ public object TcpPort : PortFactory { port: Int, coroutineContext: CoroutineContext = context.coroutineContext, ): ChannelPort = ChannelPort(context,coroutineContext){ - SocketChannel.open(InetSocketAddress(host, port)).apply { - configureBlocking(false) - } + SocketChannel.open(InetSocketAddress(host, port)) } override fun build(context: Context, meta: Meta): ChannelPort { @@ -115,7 +113,6 @@ public object UdpPort : PortFactory { ): ChannelPort = ChannelPort(context,coroutineContext){ DatagramChannel.open().apply { connect(InetSocketAddress(host, port)) - configureBlocking(false) } }