WS layer with tests

This commit is contained in:
sergeych committed 2023-11-23 10:58:37 +03:00
1 parent 3a56e67c24
commit 192f7e135f
8 files changed
+87 -11

No files matched your search

@@ -9,6 +9,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import net.sergeych.crypto2.SigningKey
import net.sergeych.crypto2.toDump
import net.sergeych.kiloparsec.KiloInterface
import net.sergeych.kiloparsec.KiloServerConnection
import net.sergeych.mp_logger.LogTag
@@ -34,14 +35,17 @@ fun <S> Application.setupWebsocketServer(
val counter = AtomicCounter()
routing {
webSocket(path) {
println("--------------------------------------------")
val log = LogTag("KWS:${counter.incrementAndGet()}")
log.debug { "opening the connection" }
val input = Channel<UByteArray>(256)
val output = Channel<UByteArray>(256)
launch {
log.debug { "starting output pump" }
while (isActive) {
send(output.receive().toByteArray())
}
log.debug { "closing output pump" }
}
val server = KiloServerConnection(
localInterface,
@@ -50,12 +54,15 @@ fun <S> Application.setupWebsocketServer(
serverKey
)
launch { server.run() }
log.debug { "KSC started, looking for incoming frames" }
for( f in incoming) {
log.debug { "incoming frame: ${f.frameType}" }
if (f is Frame.Binary)
input.send(f.readBytes().toUByteArray())
input.send(f.readBytes().toUByteArray().also {
log.debug { "in frame\n${it.toDump()}" }
})
else
log.warning { "unknown frame type ${f.frameType}, ignoring" }
}
log.debug { "closing the server" }
cancel()