commonMain.ch.softappeal.yass2.transport.Channel.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
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