Files
ferretirc-client/src/main/kotlin/com/shr4pnel/ferretirc/net/MessageWriter.kt
T

29 lines
1.0 KiB
Kotlin

package com.shr4pnel.ferretirc.net
import com.shr4pnel.ferretirc.net.messages.MessageIO
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.slf4j.logger
import io.ktor.network.sockets.Socket
import io.ktor.network.sockets.openWriteChannel
import io.ktor.utils.io.ByteWriteChannel
import io.ktor.utils.io.writeFully
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.slf4j.helpers.NOPLogger
class MessageWriter(socket: Socket, scope: CoroutineScope, enableLogging: Boolean = false) : MessageIO(socket, scope) {
private lateinit var send: ByteWriteChannel
private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
override fun start() =
scope.launch {
send = socket.openWriteChannel()
for (msg in outgoingMessages) {
logger.debug { "Sending: ${msg.toWireIntermediate()}" }
send.writeFully(msg.toWire())
send.flush()
}
}
}