jvmMain.ch.softappeal.yass2.transport.ktor.Socket.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yass2-jvm Show documentation
Show all versions of yass2-jvm Show documentation
Yet Another Service Solution
package ch.softappeal.yass2.transport.ktor
import ch.softappeal.yass2.remote.*
import ch.softappeal.yass2.transport.*
import io.ktor.network.sockets.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
fun socketTunnel(config: BufferConfig, socketConnector: suspend () -> Socket): Tunnel = { request ->
socketConnector().use { socket ->
val writeChannel = socket.openWriteChannel()
writeChannel.write(config, request)
writeChannel.flush()
socket.openReadChannel().read(config) as Reply
}
}
class SocketCce(val socket: Socket) : AbstractCoroutineContextElement(SocketCce) {
companion object Key : CoroutineContext.Key
}
suspend fun Socket.handleRequest(config: BufferConfig, tunnel: Tunnel) = use {
withContext(SocketCce(this)) {
openWriteChannel().write(config, tunnel(openReadChannel().read(config) as Request))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy