framed

fun framed(source: Source, sink: Sink, maxFrameSize: Int = DEFAULT_MAX_FRAME_SIZE): Connection

Adapt a kotlinx-io Source/Sink byte-stream into a message Connection using a 4-byte big-endian length prefix per frame.

  • Framing: each send writes a 4-byte int (big-endian) followed by frame.size bytes.

  • Reassembly: Source.readByteArray blocks the collecting coroutine until all bytes arrive — the stream side is pull-based. Real transports (TCP) collect on an IO dispatcher.

  • Oversize protection: the prefix is validated against maxFrameSize before any allocation. A hostile prefix throws FrameTooLargeException.

  • Clean EOF: an EOFException thrown by Source.readInt at a frame boundary closes incoming normally. An EOF mid-frame propagates as EOFException.

Assumption: the provided Source is backed by a hot (buffered) stream (e.g. a Ktor read channel or an in-memory kotlinx.io.Buffer). Cold/non-buffered sources that do not allow multiple independent reads would need a single-reader adapter.