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

commonMain.ch.softappeal.yass2.transport.Channel.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package ch.softappeal.yass2.transport

import ch.softappeal.yass2.remote.session.*
import ch.softappeal.yass2.serialize.*
import kotlinx.coroutines.io.*

open class BufferConfig(val bufferSize: Int, val serializer: Serializer)

class SessionConfig(
    bufferSize: Int, serializer: Serializer, val sessionFactory: SessionFactory
) : BufferConfig(bufferSize, serializer) {
    constructor(config: BufferConfig, sessionFactory: SessionFactory) :
        this(config.bufferSize, config.serializer, sessionFactory)
}

suspend fun ByteWriteChannel.write(config: BufferConfig, value: Any?) {
    val writer = BytesWriter(config.bufferSize)
    config.serializer.write(writer, value)
    writeInt(writer.current)
    writeFully(writer.buffer, 0, writer.current)
}

suspend fun ByteReadChannel.read(config: BufferConfig): Any? {
    val bufferSize = readInt()
    require(bufferSize <= config.bufferSize) // prevents out-of-memory attack
    val reader = BytesReader(bufferSize)
    readFully(reader.buffer)
    return config.serializer.read(reader)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy