Remove non-blocking mode for ports to avoid CPU clogging.

This commit is contained in:
Alexander Nozik 2023-05-27 22:44:30 +03:00
parent ced42779be
commit 22359a5570
2 changed files with 6 additions and 11 deletions

View File

@ -18,10 +18,10 @@ public interface Port : ContextAware, Socket<ByteArray>
* A specialized factory for [Port] * A specialized factory for [Port]
*/ */
@Type(PortFactory.TYPE) @Type(PortFactory.TYPE)
public interface PortFactory: Factory<Port>{ public interface PortFactory : Factory<Port> {
public val type: String public val type: String
public companion object{ public companion object {
public const val TYPE: String = "controls.port" public const val TYPE: String = "controls.port"
} }
} }
@ -53,12 +53,10 @@ public abstract class AbstractPort(
/** /**
* Internal method to receive data synchronously * Internal method to receive data synchronously
*/ */
protected fun receive(data: ByteArray) { protected suspend fun receive(data: ByteArray) {
scope.launch {
logger.debug { "${this@AbstractPort} RECEIVED: ${data.decodeToString()}" } logger.debug { "${this@AbstractPort} RECEIVED: ${data.decodeToString()}" }
incoming.send(data) incoming.send(data)
} }
}
private val sendJob = scope.launch { private val sendJob = scope.launch {
for (data in outgoing) { for (data in outgoing) {

View File

@ -87,9 +87,7 @@ public object TcpPort : PortFactory {
port: Int, port: Int,
coroutineContext: CoroutineContext = context.coroutineContext, coroutineContext: CoroutineContext = context.coroutineContext,
): ChannelPort = ChannelPort(context,coroutineContext){ ): ChannelPort = ChannelPort(context,coroutineContext){
SocketChannel.open(InetSocketAddress(host, port)).apply { SocketChannel.open(InetSocketAddress(host, port))
configureBlocking(false)
}
} }
override fun build(context: Context, meta: Meta): ChannelPort { override fun build(context: Context, meta: Meta): ChannelPort {
@ -115,7 +113,6 @@ public object UdpPort : PortFactory {
): ChannelPort = ChannelPort(context,coroutineContext){ ): ChannelPort = ChannelPort(context,coroutineContext){
DatagramChannel.open().apply { DatagramChannel.open().apply {
connect(InetSocketAddress(host, port)) connect(InetSocketAddress(host, port))
configureBlocking(false)
} }
} }