All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.ch.softappeal.yass2.transport.ktor.Socket.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
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