Don't depend on messageIO (keep own channels)

This commit is contained in:
2026-10-05 12:49:30 +01:00
parent 736b7b37df
commit 96d0c084b1
2 changed files with 10 additions and 7 deletions
@@ -1,6 +1,5 @@
package com.shr4pnel.ferretirc.net package com.shr4pnel.ferretirc.net
import com.shr4pnel.ferretirc.net.messages.MessageIO
import com.shr4pnel.ferretirc.net.messages.ServerMessage import com.shr4pnel.ferretirc.net.messages.ServerMessage
import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.slf4j.logger import io.github.oshai.kotlinlogging.slf4j.logger
@@ -9,13 +8,15 @@ import io.ktor.network.sockets.openReadChannel
import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.readLineStrict import io.ktor.utils.io.readLineStrict
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.slf4j.helpers.NOPLogger import org.slf4j.helpers.NOPLogger
class MessageReader(socket: Socket, scope: CoroutineScope, enableLogging: Boolean = false) : MessageIO(socket, scope) { class MessageReader(val socket: Socket, val scope: CoroutineScope, enableLogging: Boolean = false) {
private lateinit var receive: ByteReadChannel private lateinit var receive: ByteReadChannel
val incomingMessages = Channel<String>(Channel.BUFFERED)
private val messageBuffer = MutableSharedFlow<ServerMessage>(0, 64) private val messageBuffer = MutableSharedFlow<ServerMessage>(0, 64)
val sharedMessageBuffer = messageBuffer.asSharedFlow() val sharedMessageBuffer = messageBuffer.asSharedFlow()
val parser = MessageParser(incomingMessages, enableLogging) val parser = MessageParser(incomingMessages, enableLogging)
@@ -31,7 +32,7 @@ class MessageReader(socket: Socket, scope: CoroutineScope, enableLogging: Boolea
} }
} }
override fun start() { fun start() {
scope.launch { scope.launch {
receive = socket.openReadChannel() receive = socket.openReadChannel()
while (true) { while (true) {
@@ -1,6 +1,6 @@
package com.shr4pnel.ferretirc.net package com.shr4pnel.ferretirc.net
import com.shr4pnel.ferretirc.net.messages.MessageIO import com.shr4pnel.ferretirc.net.messages.ClientMessage
import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.slf4j.logger import io.github.oshai.kotlinlogging.slf4j.logger
import io.ktor.network.sockets.Socket import io.ktor.network.sockets.Socket
@@ -8,19 +8,21 @@ import io.ktor.network.sockets.openWriteChannel
import io.ktor.utils.io.ByteWriteChannel import io.ktor.utils.io.ByteWriteChannel
import io.ktor.utils.io.writeFully import io.ktor.utils.io.writeFully
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.slf4j.helpers.NOPLogger import org.slf4j.helpers.NOPLogger
class MessageWriter(socket: Socket, scope: CoroutineScope, enableLogging: Boolean = false) : MessageIO(socket, scope) { class MessageWriter(val socket: Socket, val scope: CoroutineScope, enableLogging: Boolean = false) {
private lateinit var send: ByteWriteChannel private lateinit var send: ByteWriteChannel
private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER) private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
val outgoingMessages = Channel<ClientMessage>(Channel.BUFFERED)
override fun start() = fun start() =
scope.launch { scope.launch {
send = socket.openWriteChannel() send = socket.openWriteChannel()
for (msg in outgoingMessages) { for (msg in outgoingMessages) {
logger.debug { "Sending: ${msg.toWireIntermediate()}" } logger.debug { "[C->S]: Sending ${msg.toWireIntermediate()}" }
send.writeFully(msg.toWire()) send.writeFully(msg.toWire())
send.flush() send.flush()
} }