framer

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

framer — message boundaries over stream I/O

Go Reference Benchmarks Coverage Status

Languages: English | 简体中文 | Español | 日本語 | Français

Portable message framing for Go. Preserve one-message-per-Read/Write over stream transports.

Scope: message boundary preservation for stream transports.

Overview

Byte-stream transports such as TCP, Unix streams and pipes do not preserve application message boundaries. Framer adds length prefixes so each completed read/write operation handles one message. A non-blocking operation may require several calls with retained state.

  • Message boundary preservation for byte streams (TCP, Unix stream, pipes).
  • Pass-through on boundary-preserving transports (UDP, Unix datagram, WebSocket, SCTP).
  • Portable wire format; configurable byte order.

Protocol adaptation

  • BinaryStream (stream transports: TCP, TLS-over-TCP, Unix stream, pipes): adds a length prefix; reads/writes whole messages.
  • SeqPacket (e.g., SCTP, WebSocket): pass-through; the transport already preserves boundaries.
  • Datagram (e.g., UDP, Unix datagram): pass-through; boundary already preserved.
  • For Reader.Read, packet modes are pass-through: WithReadLimit is checked after one receive, so an oversized packet may return n > limit with ErrTooLong; n is the bytes copied from that packet.
  • Packet-output paths retry whole packets only after zero-progress ErrWouldBlock / ErrMore; a fully accepted nonempty packet returned with ErrWouldBlock or ErrMore is not replayed, and partial packet writes are reported as io.ErrShortWrite.

Select at construction time via WithProtocol(...) (read/write variants exist) or via transport helpers (see Options).

Wire format

Compact variable-length length prefix, followed by payload bytes. Byte order for the extended length is configurable: WithByteOrder, or per-direction WithReadByteOrder / WithWriteByteOrder.

Frame data format

The framing scheme used by framer is intentionally compact:

  • Header byte H0 + optional extended length bytes.
  • Let L be the payload length in bytes.
    • If 0 ≤ L ≤ 253 (0x00..0xFD): H0 = L. No extra length bytes.
    • If 254 ≤ L ≤ 65535 (0x0000..0xFFFF): H0 = 0xFE and the next 2 bytes encode L as an unsigned 16‑bit integer in the configured byte order.
    • If 65536 ≤ L ≤ 2^56-1: H0 = 0xFF and the next 7 bytes carry L as a 56‑bit integer, laid out in the configured byte order.
      • Big‑endian: bytes [1..7] are the big‑endian lower 56 bits of L.
      • Little‑endian: bytes [1..7] are the little‑endian lower 56 bits of L.

Limits and errors:

  • The maximum supported payload length is 2^56-1; larger values result in framer.ErrTooLong.
  • When a read‑side limit is configured (WithReadLimit), lengths exceeding the limit fail with framer.ErrTooLong.

Installation

Requires Go 1.27 or later.

go get code.hybscloud.com/framer
c1, c2 := net.Pipe()
defer c1.Close()
defer c2.Close()

w := framer.NewWriter(c1, framer.WithWriteTCP())
r := framer.NewReader(c2, framer.WithReadTCP())

go func() { _, _ = w.Write([]byte("hello")) }()

buf := make([]byte, 64)
n, err := r.Read(buf)
if err != nil {
	panic(err)
}
fmt.Printf("got: %q\n", buf[:n])

Non-blocking usage

For BinaryStream, keep r, the full buf, and an accumulated received count in connection state. Each event-loop service attempt performs one call:

n, err := r.Read(buf)
received += n
if err != nil {
	return err
}
process(buf[:received])
received = 0

On ErrWouldBlock or ErrMore, retain that state and return control to the scheduler. Resume the same buffer under the input/readiness and work-budget policy. Process the full message only on nil error, including empty messages. n counts newly copied payload bytes in this call; a finishing call can return zero.

Borrowed BinaryStream codec

Use Decoder for bytes already received into caller-owned buffers. Its zero value selects big endian and the wire maximum; NewDecoder(order, limit) accepts nil or standard byte orders, with zero limit meaning the wire maximum.

Decode(src) returns consumed wire bytes and one Chunk: Data is a capacity-capped payload view, Length is the full payload length, and Begin/End mark message boundaries. It stops at the current message's end. Process the result and advance by consumed bytes; never replay them. Keep the backing valid while using Data; the decoder retains no input or payload.

nil ends one message. ErrMore reports incomplete-frame progress, possibly header-only; it does not promise more input. Empty input returns ErrWouldBlock; supply later input instead of polling. Call Finish() at input end to detect truncation. Failures persist until Reset(), which keeps configuration and requires a new stream or an established frame boundary.

EncodeHeader writes only the shortest header into supplied storage; errors leave it unchanged. Both operations avoid payload copies, heap allocation and waiting. The caller controls service budgets and buffer lifetimes.

var header [framer.MaxHeaderSize]byte
n, err := framer.EncodeHeader(header[:], uint64(len(payload)), nil)
if err != nil {
    return err
}
// Send header[:n] followed by payload; retain both until I/O releases them.

See the incremental decoding example.

Options

  • WithProtocol(proto Protocol): choose BinaryStream, SeqPacket, or Datagram (read/write variants available).
  • Byte order: WithByteOrder, or WithReadByteOrder / WithWriteByteOrder.
  • WithReadLimit(n int): cap maximum message payload size when reading; Reader.Read enforces it post-read in packet modes and may return n > limit with ErrTooLong.
  • WithRetryDelay(d time.Duration): configure zero-progress ErrWouldBlock policy; a negative value returns ErrWouldBlock immediately, zero yields and retries, and a positive value sleeps for d before retrying. Positive underlying byte progress bypasses this wait policy; header-only progress can still return a zero payload count. Related options: WithNonblock() / WithBlock().

Transport helpers (presets):

  • WithReadTCP / WithWriteTCP (BinaryStream, network‑order BigEndian)
  • WithReadUDP / WithWriteUDP (Datagram, BigEndian)
  • WithReadWebSocket / WithWriteWebSocket (SeqPacket, BigEndian)
  • WithReadSCTP / WithWriteSCTP (SeqPacket, BigEndian)
  • WithReadUnix / WithWriteUnix (BinaryStream, BigEndian)
  • WithReadUnixPacket / WithWriteUnixPacket (Datagram, BigEndian)
  • WithReadLocal / WithWriteLocal (BinaryStream, native byte order)

Everything else: see GoDoc: https://pkg.go.dev/code.hybscloud.com/framer

Semantics Contract

Packet mode note (SeqPacket / Datagram)
  • Packet mode preserves transport boundaries and does not split packets.
  • A packet source's (0, nil) completes an empty message; zero bytes with a source error do not form a message. Empty packet output is accepted only with nil; (0, ErrWouldBlock) / (0, ErrMore) remains pending.
  • Reader.Read enforces WithReadLimit after one packet read; transfer helpers use one sentinel byte to reject oversized packets before forwarding bytes.
  • Packet-preserving destinations retry the whole packet only after zero-progress ErrWouldBlock / ErrMore; a fully accepted nonempty packet returned with ErrWouldBlock or ErrMore is not replayed, and partial packet writes are boundary failures reported as io.ErrShortWrite.
  • Reader.WriteTo to an arbitrary io.Writer is a byte-copy transfer with suffix resume. When the destination is a framer.Writer, it uses the destination algebra: packet writers retry the whole packet after zero progress, and stream writers retry the same in-flight frame.
  • If a packet source returns (n > 0, err), Reader.WriteTo emits the admitted packet before reporting err; write-side suspension keeps that source signal pending across retry.
  • Progress counts are operation-indexed: Reader.Read reports bytes copied into p, Reader.WriteTo reports bytes written to dst, Writer.ReadFrom reports bytes read from src and admitted to the writer state, and Forwarder.ForwardOnce reports progress in its current phase.
Retry discipline
  • ErrWouldBlock is readiness suspension, not failure; aggregate helpers may return a positive count when earlier loop steps made progress before suspension.
  • ErrMore preserves a live operation; it is not EOF and does not establish input availability. Process progress and resume under that operation's retry contract and the caller's work budget.
  • Retry Reader.Read after partial stream progress on the same Reader with the same buffer.
  • Retry Writer.Write after BinaryStream suspension on the same Writer with the same message length; BinaryStream header bytes are not included in n. An empty frame's header may already be written before suspension; retry finishes the same frame without another header. In packet modes, n == len(p) && n > 0 with ErrWouldBlock or ErrMore means the packet was accepted, so do not replay p.
  • Retry Reader.WriteTo on the same Reader and same destination, Writer.ReadFrom on the same Writer, and Forwarder.ForwardOnce on the same Forwarder.
Performance contract
  • Hot paths keep runtime checks minimal for steady-state throughput.
  • Callers are responsible for valid option/buffer usage and operation-specific retry after ErrWouldBlock or ErrMore.
Error taxonomy
Error Meaning Caller action
nil Operation completed successfully Proceed; n reports progress in this call
io.EOF End of stream (no more messages) Stop reading; normal termination
io.ErrUnexpectedEOF Stream ended mid-message (header or payload incomplete) Treat as fatal; data corruption or disconnect
io.ErrShortBuffer Destination buffer too small for message payload Retry with larger buffer
io.ErrShortWrite Destination accepted fewer bytes than provided Retry or treat as fatal per context
io.ErrNoProgress Stream Reader or generic ReadFrom source returned (0, nil) on a non-empty buffer Stop or delegate retry to the owner; framer does not spin on zero progress
framer.ErrWouldBlock No progress possible now without waiting Retry later (after poll/event); n may be >0
framer.ErrMore Same operation has more progress to deliver, distinct from EOF and readiness suspension Process progress; resume under the operation's input and scheduling contract
framer.ErrTooLong Message exceeds a configured limit, transfer cap, or wire-format bound Reject message; possibly fatal
framer.ErrInvalidArgument Nil reader/writer or invalid config Fix configuration
Outcome tables

Reader/Writer counts below are per call, not cumulative message lengths. Sum counts across retries; nil can finish earlier progress with n == 0.

Reader.Read(p []byte) (n int, err error) (BinaryStream mode)

Condition n err
Complete message delivered payload bytes copied this call; possibly 0 nil
len(p) < payload length 0 io.ErrShortBuffer
Payload exceeds ReadLimit 0 ErrTooLong
Underlying returns would-block payload bytes copied this call; possibly 0 ErrWouldBlock
Underlying returns more payload bytes copied this call; possibly 0 ErrMore
EOF at message boundary 0 io.EOF
EOF mid-header or mid-payload payload bytes copied this call; possibly 0 io.ErrUnexpectedEOF

Writer.Write(p []byte) (n int, err error) (BinaryStream mode)

Condition n err
Complete framed message emitted payload bytes accepted this call; possibly 0 nil
Payload exceeds max (2^56-1) 0 ErrTooLong
Underlying returns would-block payload bytes accepted this call; possibly 0 ErrWouldBlock
Underlying returns more payload bytes accepted this call; possibly 0 ErrMore

Reader.WriteTo(dst io.Writer) (n int64, err error)

Condition n err
All messages transferred until EOF total payload bytes nil
Underlying reader returns would-block payload bytes written ErrWouldBlock
Underlying reader returns more payload bytes written ErrMore
dst returns would-block payload bytes written ErrWouldBlock
Packet source exceeds ReadLimit before forwarding bytes already written before that packet ErrTooLong
Message exceeds internal buffer (64KiB default) bytes so far ErrTooLong
Stream ended mid-message bytes so far io.ErrUnexpectedEOF

Writer.ReadFrom(src io.Reader) (n int64, err error)

Condition n err
All chunks encoded until src EOF total bytes read from src nil
src returns (0, nil) bytes admitted earlier in this call io.ErrNoProgress
src returns would-block bytes read from src before the signal ErrWouldBlock
src returns more bytes read from src before the signal ErrMore
Underlying writer returns would-block bytes read from src and admitted before suspension; 0 on pure write-side resume ErrWouldBlock
Underlying writer returns more bytes read from src and admitted before suspension; 0 on pure write-side resume ErrMore

Forwarder.ForwardOnce() (n int, err error)

Condition n err
One message fully forwarded payload bytes accepted this call; possibly 0 nil
Packet source returns (n > 0, io.EOF) payload bytes accepted this call; possibly 0 nil (next call returns io.EOF)
No more messages 0 io.EOF
Source returns would-block read bytes if no packet was emitted; packet-source n > 0 is emitted first and returns payload bytes (write phase) ErrWouldBlock
Source returns more read bytes if no packet was emitted; packet-source n > 0 is emitted first and returns payload bytes (write phase) ErrMore
Write phase would-block bytes written this call ErrWouldBlock
Write phase more bytes written this call ErrMore
Stream message or required packet read capacity exceeds internal buffer 0 io.ErrShortBuffer
Packet exceeds ReadLimit/default packet transfer cap before forwarding bytes read from the packet, not forwarded ErrTooLong
Stream ended mid-message bytes so far io.ErrUnexpectedEOF
Operation classification
Operation Boundary behavior Use case
Reader.Read Message-preserving: one completed operation = one message; retries may be needed Application-level message processing
Writer.Write Message-preserving: one completed operation = one message; retries may be needed Application-level message sending
Reader.WriteTo Byte transfer to arbitrary writers; known framer destinations preserve packet/frame retry law Efficient bulk transfer with suffix resume
Writer.ReadFrom Chunking: each src chunk becomes one message; packet output is whole-packet retry only after zero progress Efficient bulk encoding; does NOT preserve upstream boundaries
Forwarder.ForwardOnce Message-preserving relay: decode one, re-encode one Message-aware proxying with boundary preservation
Blocking policy

By default, framer is non-blocking (WithNonblock()): ErrWouldBlock is returned immediately.

  • WithBlock() or WithRetryDelay(0): yield (runtime.Gosched) and retry zero-progress would-block
  • WithRetryDelay(d > 0): sleep d and retry zero-progress would-block
  • Negative RetryDelay (default): return zero-progress ErrWouldBlock immediately
  • The policy does not wait on an underlying ErrWouldBlock result with positive byte progress. Public counts follow each operation's count contract; header-only progress can return zero. Resume the same operation as documented above.

WithNonblock() disables framer's would-block retries. It does not make a blocking underlying transport non-blocking.

framer uses code.hybscloud.com/iox control flow signals. ErrWouldBlock and ErrMore are aliases from iox, enabling direct integration with other iox-aware components (iofd, takt).

Fast paths

framer implements stdlib copy fast paths to interoperate with io.Copy-style engines and iox.CopyPolicy:

  • (*Reader).WriteTo(io.Writer): efficiently transfers framed message payloads to dst.

    • Stream (BinaryStream): processes one framed message at a time and writes only the payload bytes to dst. If ReadLimit == 0, an internal default cap (64KiB) is used; messages larger than this cap return framer.ErrTooLong.
    • Packet (SeqPacket/Datagram): pass-through byte transfer; sentinel-cap reads reject oversized packets before forwarding, and n counts bytes written to dst.
    • Semantic write-side errors framer.ErrWouldBlock and framer.ErrMore are propagated unchanged with the progress count reflecting bytes written; packet-source errors returned with bytes are reported after the admitted packet is emitted.
  • (*Writer).ReadFrom(io.Reader): chunk-to-message; each positive-length Read chunk from src is encoded as a single framed message.

    • This is efficient but does not preserve application message boundaries from src.
    • Packet output emits each chunk as one packet; it does not establish the input protocol. A source (0, nil) returns io.ErrNoProgress immediately without an empty output message.
    • Semantic errors framer.ErrWouldBlock and framer.ErrMore are propagated unchanged; n counts bytes read from src and admitted to the writer state.

Recommendation: prefer iox.CopyPolicy with a retry-aware policy (e.g., PolicyRetry) in non-blocking loops so ErrWouldBlock / ErrMore are handled explicitly.

Zero-allocation steady state: After initial buffer allocation, Forwarder and WriteTo paths reuse internal buffers. No heap allocations occur per message in steady state.

Note on partial write recovery: When using iox.Copy with non-blocking destinations, partial writes may occur. If the source does not implement io.Seeker, iox.Copy returns iox.ErrNoSeeker to prevent silent data loss. For non-seekable sources (e.g., network sockets), use iox.CopyPolicy with PolicyRetry for write-side semantic errors to ensure all read bytes are written before returning.

The benchmark guide shows how to measure frame rate, payload throughput and allocations for one or many peers.

Forwarding

  • Wire proxying (byte engines): use iox.CopyPolicy and standard io fast paths (WriterTo/ReaderFrom) when byte-level forwarding is acceptable and higher-level boundaries do not need preservation.
  • Message relay (preserve boundaries): use framer.NewForwarder(dst, src, ...) and call ForwardOnce() in your poll loop. It decodes exactly one framed message from src and re-encodes it as exactly one framed message to dst.
    • Non-blocking semantics: retry the same Forwarder instance after framer.ErrWouldBlock or framer.ErrMore; packet-source (n > 0, err) is emitted before the source signal is reported, and write-side suspension keeps that source signal pending for the later retry on the same Forwarder.
    • Limits: io.ErrShortBuffer when the internal buffer is too small for a stream message or required packet read capacity; framer.ErrTooLong when a packet exceeds WithReadLimit or the default packet transfer cap before forwarding.
    • Zero‑alloc steady state after construction; the internal scratch buffer is reused per message.

Message relay example:

fwd := framer.NewForwarder(dst, src, framer.WithReadTCP(), framer.WithWriteTCP())

Construct the forwarder once. Each later service attempt uses the same instance and returns its control signal to the owner:

_, err := fwd.ForwardOnce()
return err

License

MIT — see LICENSE.

©2025 Hayabusa Cloud Co., Ltd.

Documentation

Overview

Package framer provides portable message framing over io.Reader and io.Writer.

Semantics and design:

  • Protocol adaptation: on stream transports such as TCP, framer adds a compact length prefix and preserves one message per completed read/write operation. On boundary-preserving transports such as SCTP, UDP, and WebSocket, framer is pass-through.
  • Packet-mode limit semantics: Reader.Read checks WithReadLimit after one transport read; oversized packets may return (n > limit, ErrTooLong), where n is the number of bytes copied from that packet. Reader.WriteTo and Forwarder use a sentinel byte to reject oversized packets before forwarding bytes.
  • Non-blocking first: ErrWouldBlock and ErrMore are control-flow signals, not failures. Prepared steady-state paths avoid allocations; underlying transports retain their own blocking behavior.
  • Performance contract: hot paths keep runtime validation minimal; callers are responsible for valid option and buffer usage plus operation-specific retry after ErrWouldBlock or ErrMore.
  • io interfaces: Reader, Writer, and ReadWriter implement standard io interfaces. BinaryStream retries follow the documented same-buffer and same-message rules; progress counts apply to each call.
  • Borrowed input: Decoder consumes received slices and returns Chunk views without payload copies. EncodeHeader writes framing metadata into caller-owned storage for output with an explicit I/O lifetime.

Wire format (stream mode): a 1-byte header followed by optional extended length bytes and then the payload. Let L be payload length in bytes:

  • 0 <= L <= 253: header[0] = L (no extended length)
  • 254 <= L <= 65535: header[0] = 0xFE; next 2 bytes encode L (configured byte order)
  • 65536 <= L <= 2^56-1: header[0] = 0xFF; next 7 bytes encode lower 56 bits of L in the configured byte order

Maximum supported payload is 2^56-1; larger values produce ErrTooLong. A per-reader limit can be set via WithReadLimit.

Index

Examples

Constants

View Source
const MaxHeaderSize = 8

MaxHeaderSize is the maximum BinaryStream length-prefix size in bytes.

Variables

View Source
var (
	// ErrInvalidArgument reports an invalid configuration or nil reader/writer.
	ErrInvalidArgument = errors.New("framer: invalid argument")

	// ErrTooLong reports that a message exceeds a configured limit, transfer cap,
	// or wire-format bound.
	ErrTooLong = errors.New("framer: message too long")
)
View Source
var (
	// ErrWouldBlock reports that the current non-blocking transport attempt cannot
	// progress without waiting.
	//
	// It is a control-flow signal, not a failure. Aggregate helpers may return a
	// positive count with ErrWouldBlock when earlier steps in the same call made
	// progress before the current attempt suspended.
	//
	// ErrWouldBlock is an alias for iox.ErrWouldBlock.
	ErrWouldBlock = iox.ErrWouldBlock

	// ErrMore reports that the same operation has additional progress to deliver.
	//
	// It is not io.EOF and does not establish input availability. Process any
	// returned progress, then resume under the operation's retry contract.
	// Decoder callers supply unconsumed or later input; they never replay bytes.
	//
	// ErrMore is an alias for iox.ErrMore.
	ErrMore = iox.ErrMore
)

Functions

func EncodeHeader added in v0.1.6

func EncodeHeader(dst []byte, length uint64, order binary.ByteOrder) (int, error)

EncodeHeader writes the shortest BinaryStream header for length into dst. It returns the number of header bytes written; it does not write payload. A nil order selects big endian. Only binary.BigEndian, binary.LittleEndian, and binary.NativeEndian are supported; other orders return ErrInvalidArgument. Lengths above 2^56-1 return ErrTooLong. An insufficient dst returns io.ErrShortBuffer. On error, no bytes are written. Success leaves the suffix after the header unchanged.

EncodeHeader does not retain dst. Caller must keep header storage valid until any asynchronous I/O using it has released that storage.

Example
package main

import (
	"fmt"

	"code.hybscloud.com/framer"
)

func main() {
	var header [framer.MaxHeaderSize]byte
	n, err := framer.EncodeHeader(header[:], 300, nil)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%x\n", header[:n])
}
Output:
fe012c

func NewPipe

func NewPipe(opts ...Option) (reader io.Reader, writer io.Writer)

NewPipe returns a synchronous in-memory framing pipe.

The pipe is backed by io.Pipe. Both returned interfaces refer to the same state-bearing ReadWriter, so callers coordinate reads and writes as one synchronous pipe rather than treating them as independent endpoints.

func NewReadWriter

func NewReadWriter(r io.Reader, w io.Writer, opts ...Option) io.ReadWriter

NewReadWriter returns an io.ReadWriter that reads and writes framed messages.

The returned value shares one state machine for both directions and is intended for coordinated use. Concurrent access requires caller synchronization.

func NewReader

func NewReader(r io.Reader, opts ...Option) io.Reader

NewReader returns an io.Reader that reads framed messages from r.

The returned dynamic type is *Reader. Callers that need Reader.WriteTo can type assert the result to *Reader.

func NewWriter

func NewWriter(w io.Writer, opts ...Option) io.Writer

NewWriter returns an io.Writer that writes framed messages to w.

The returned dynamic type is *Writer. Callers that need Writer.ReadFrom can type assert the result to *Writer.

Types

type Chunk added in v0.1.6

type Chunk struct {
	Data   []byte // Payload bytes only; no framing header.
	Length uint64 // Full declared payload length when a chunk or boundary is emitted.
	Begin  bool   // The validated length header was completed in this call.
	End    bool   // The message completed in this call, including an empty one.
}

Chunk describes part of one BinaryStream message. Data is a read-only view into the input supplied to Decoder.Decode, with capacity equal to its length. Caller must keep that backing valid while using Data. The decoder neither owns nor retains it; subsequent Decode, Finish, and Reset calls do not revoke it.

type Decoder added in v0.1.6

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder incrementally decodes BinaryStream framing from borrowed input. Its zero value uses big endian and the wire limit of 2^56-1 bytes. It keeps fixed parser state and at most eight header bytes, never input slices or payload storage. It performs no I/O, waiting, or payload copies.

Caller must serialize access. A live Decoder must not be copied to resume the same stream independently.

Example
package main

import (
	"fmt"

	"code.hybscloud.com/framer"
)

func main() {
	var d framer.Decoder
	for _, input := range [][]byte{{3, 'a'}, {'b', 'c', 0}} {
		for len(input) > 0 {
			n, chunk, err := d.Decode(input)
			if chunk.Begin {
				fmt.Println("begin", chunk.Length)
			}
			if len(chunk.Data) > 0 {
				fmt.Printf("data %s\n", chunk.Data)
			}
			if chunk.End {
				fmt.Println("end")
			}
			input = input[n:]
			if err != nil && err != framer.ErrMore {
				panic(err)
			}
		}
	}
	if err := d.Finish(); err != nil {
		panic(err)
	}
}
Output:
begin 3
data a
data bc
end
begin 0
end

func NewDecoder added in v0.1.6

func NewDecoder(order binary.ByteOrder, limit uint64) (Decoder, error)

NewDecoder returns a value configured for order and limit. A nil order selects big endian; the three standard encoding/binary byte orders are supported. Zero limit selects the wire maximum. An unsupported order or a limit above 2^56-1 returns ErrInvalidArgument.

func (*Decoder) Decode added in v0.1.6

func (d *Decoder) Decode(src []byte) (consumed int, chunk Chunk, err error)

Decode consumes part of src and returns at most one borrowed payload chunk, without crossing the current message's end. Consumed counts header and payload bytes. Always process the returned chunk and advance by consumed before considering err; never replay consumed bytes.

A nil error completes one message (End is true). ErrMore reports positive progress in an incomplete message, including a partial header with no Chunk. It does not promise more input is available. Empty input on a live decoder returns ErrWouldBlock with zero progress and unchanged state. Length rejection returns ErrTooLong after consuming the header, without emitting Begin or Data. Failures persist until Reset; after successful Finish, Decode returns io.EOF.

Begin is emitted once when the header passes the limit check, possibly before any payload arrives. An empty message emits Begin and End with empty Data. No event is emitted when both flags are false and Data is empty. Caller must have capacity to handle the result before calling Decode: progress is committed on return. Bound per-turn work by limiting src and the number of calls.

func (*Decoder) Finish added in v0.1.6

func (d *Decoder) Finish() error

Finish declares input end after the caller has processed all supplied bytes. It returns nil at a message boundary, or io.ErrUnexpectedEOF for an unfinished header or body. Repeated calls preserve the result, including an earlier length failure. Earlier chunks are not rolled back by a truncated message.

func (*Decoder) Reset added in v0.1.6

func (d *Decoder) Reset()

Reset clears parser and terminal state while preserving configuration. Caller must start a new stream or independently establish a frame boundary; Reset does not resynchronize arbitrary bytes or release caller-owned storage.

type Forwarder

type Forwarder struct {
	// contains filtered or unexported fields
}

Forwarder relays framed messages from a source to a destination while preserving message boundaries.

Semantics (BinaryStream):

  • One call to ForwardOnce processes at most one logical message.
  • Two-phase state machine per message: 1) Read a whole framed message payload from src into an internal buffer (non-blocking; may return early with partial progress and ErrWouldBlock or ErrMore). 2) Write that same payload as exactly one framed message to dst (non-blocking; may return early with partial progress and ErrWouldBlock or ErrMore).
  • Returns (n, nil) when a whole message payload has been forwarded to dst.
  • Returns ErrWouldBlock or ErrMore when the current phase cannot continue without a later retry. The returned n is the current-phase progress and can be zero when that phase accepted no payload bytes.
  • Message boundaries are preserved: the destination sees exactly the same payload bytes as the source, encoded as one framed message on stream transports.

Semantics (SeqPacket/Datagram):

  • Treats one packet as one message unit per call. Reads one packet from src and writes one message to dst. A source (0, nil) is an empty packet; zero bytes with a non-nil source error do not form a message.
  • If the packet source returns (n > 0, err), ForwardOnce forwards that packet before reporting err. A final (n > 0, io.EOF) packet is reported as nil after the packet is forwarded, and the next call reports io.EOF.
  • Destination packet writes are atomic: zero-progress ErrWouldBlock or ErrMore keeps the whole packet pending, while a partial packet write fails with io.ErrShortWrite rather than retrying a suffix as a second packet. Empty packet output completes only with nil error. An empty stream frame can suspend after its header is written; retry finishes that same frame.
  • Return counts follow the current ForwardOnce phase documented on Forwarder.ForwardOnce.

Limits and buffer sizing:

  • The internal payload buffer is allocated during construction based on read-side limit (WithReadLimit). If ReadLimit is zero, a conservative default (64KiB) is used. There are no heap allocations in the steady-state forwarding path.
  • If a stream message or required packet read capacity exceeds the internal buffer capacity, ForwardOnce returns io.ErrShortBuffer. Callers can construct a new Forwarder with a larger ReadLimit to accommodate larger messages.
  • If a packet exceeds ReadLimit, or exceeds the default packet transfer cap when ReadLimit is zero, ForwardOnce returns ErrTooLong before forwarding bytes.

Retry rule:

  • On ErrWouldBlock or ErrMore, the caller must retry ForwardOnce on the same Forwarder instance. The instance may hold an in-flight write or a pending source signal that must remain ordered with the forwarded packet.

func NewForwarder

func NewForwarder(dst io.Writer, src io.Reader, opts ...Option) *Forwarder

NewForwarder returns a Forwarder that relays messages from src to dst.

Options apply to both directions unless a read-side or write-side option is used.

func (*Forwarder) ForwardOnce

func (f *Forwarder) ForwardOnce() (n int, err error)

ForwardOnce forwards at most one message.

See Forwarder documentation for stream, packet, limit, and retry semantics.

Return value n reflects progress in the current phase:

  • During the read phase, n is the number of payload bytes read into the internal buffer in this call.
  • During the write phase, n is the number of payload bytes written to dst in this call.
  • In packet mode, if the source returns n > 0 with an error, ForwardOnce emits that packet before reporting the source error. If write-side suspension happens first, the source error remains pending for a later ForwardOnce call on the same Forwarder.

type Option

type Option func(*Options)

Option configures Reader, Writer, ReadWriter, and Forwarder construction.

Later options override earlier settings for the same field.

func WithBlock

func WithBlock() Option

WithBlock enables cooperative blocking by yielding and retrying on zero-progress ErrWouldBlock.

func WithByteOrder

func WithByteOrder(order binary.ByteOrder) Option

WithByteOrder sets both read and write byte order for BinaryStream length prefixes.

func WithNonblock

func WithNonblock() Option

WithNonblock disables framer's retries on zero-progress ErrWouldBlock. It does not change the underlying transport's blocking behavior.

func WithProtocol

func WithProtocol(proto Protocol) Option

WithProtocol sets both read and write protocol modes.

func WithReadByteOrder

func WithReadByteOrder(order binary.ByteOrder) Option

WithReadByteOrder sets the read-side byte order for BinaryStream length prefixes.

func WithReadLimit

func WithReadLimit(limit int) Option

WithReadLimit sets the maximum accepted payload size on the read side.

In SeqPacket and Datagram modes, the limit is checked after a packet read, so an oversized packet may return (n > limit, ErrTooLong). A non-positive limit means no caller-configured limit; transfer helpers may still apply their documented internal buffer cap.

func WithReadLocal

func WithReadLocal() Option

WithReadLocal configures the reader side for local stream transports with BinaryStream and native byte order.

func WithReadProtocol

func WithReadProtocol(proto Protocol) Option

WithReadProtocol sets the reader-side protocol mode.

func WithReadSCTP

func WithReadSCTP() Option

WithReadSCTP configures the reader side for SCTP with SeqPacket pass-through and BigEndian byte order.

func WithReadTCP

func WithReadTCP() Option

WithReadTCP configures the reader side for TCP with BinaryStream and BigEndian length prefixes.

func WithReadUDP

func WithReadUDP() Option

WithReadUDP configures the reader side for UDP with Datagram pass-through and BigEndian byte order.

func WithReadUnix

func WithReadUnix() Option

WithReadUnix configures the reader side for Unix stream sockets with BinaryStream and BigEndian byte order.

func WithReadUnixPacket

func WithReadUnixPacket() Option

WithReadUnixPacket configures the reader side for Unix packet sockets with Datagram pass-through and BigEndian byte order.

func WithReadWebSocket

func WithReadWebSocket() Option

WithReadWebSocket configures the reader side for WebSocket with SeqPacket pass-through and BigEndian byte order.

func WithRetryDelay

func WithRetryDelay(d time.Duration) Option

WithRetryDelay sets the wait policy used when the underlying transport returns ErrWouldBlock without transferring bytes.

A negative duration returns ErrWouldBlock immediately. A zero duration yields and retries. A positive duration sleeps for d before retrying. Positive underlying progress bypasses this wait policy; header-only progress can still have a zero public payload count.

func WithWriteByteOrder

func WithWriteByteOrder(order binary.ByteOrder) Option

WithWriteByteOrder sets the write-side byte order for BinaryStream length prefixes.

func WithWriteLocal

func WithWriteLocal() Option

WithWriteLocal configures the writer side for local stream transports with BinaryStream and native byte order.

func WithWriteProtocol

func WithWriteProtocol(proto Protocol) Option

WithWriteProtocol sets the writer-side protocol mode.

func WithWriteSCTP

func WithWriteSCTP() Option

WithWriteSCTP configures the writer side for SCTP with SeqPacket pass-through and BigEndian byte order.

func WithWriteTCP

func WithWriteTCP() Option

WithWriteTCP configures the writer side for TCP with BinaryStream and BigEndian length prefixes.

func WithWriteUDP

func WithWriteUDP() Option

WithWriteUDP configures the writer side for UDP with Datagram pass-through and BigEndian byte order.

func WithWriteUnix

func WithWriteUnix() Option

WithWriteUnix configures the writer side for Unix stream sockets with BinaryStream and BigEndian byte order.

func WithWriteUnixPacket

func WithWriteUnixPacket() Option

WithWriteUnixPacket configures the writer side for Unix packet sockets with Datagram pass-through and BigEndian byte order.

func WithWriteWebSocket

func WithWriteWebSocket() Option

WithWriteWebSocket configures the writer side for WebSocket with SeqPacket pass-through and BigEndian byte order.

type Options

type Options struct {
	// ReadByteOrder controls BinaryStream length-prefix decoding.
	ReadByteOrder binary.ByteOrder

	// WriteByteOrder controls BinaryStream length-prefix encoding.
	WriteByteOrder binary.ByteOrder

	// ReadProto selects the reader-side protocol mode.
	ReadProto Protocol

	// WriteProto selects the writer-side protocol mode.
	WriteProto Protocol

	// ReadLimit caps the maximum accepted payload size in bytes.
	// A non-positive value means no caller-configured read limit.
	ReadLimit int

	// RetryDelay controls how the framer handles zero-progress ErrWouldBlock
	// from the underlying transport:
	//   - negative: nonblock, return ErrWouldBlock immediately
	//   - zero: yield (runtime.Gosched) and retry
	//   - positive: sleep for the duration and retry
	//
	// The policy does not wait on an underlying ErrWouldBlock result with
	// positive byte progress. Public counts remain operation-specific and can
	// be zero after header-only progress.
	RetryDelay time.Duration
}

Options configures framing behavior used by constructors.

Constructors start from package defaults before applying Option values. Callers normally pass Option helpers rather than constructing Options directly; the zero Options value is not a complete configuration because byte order fields may be nil. Byte orders must use a consistent big- or little-endian layout across integer widths. Constructors determine that layout once and retain the implementation.

type Protocol

type Protocol uint8

Protocol describes the message-boundary behavior expected from the underlying transport.

The framer logic adapts its algorithm based on this setting:

  • BinaryStream: boundaries are not preserved, for example TCP; framer adds a length prefix.
  • SeqPacket and Datagram: boundaries are preserved; framer is pass-through.
const (
	// BinaryStream selects length-prefixed framing for byte-stream transports.
	BinaryStream Protocol = 1

	// SeqPacket selects pass-through framing for sequenced-packet transports.
	SeqPacket Protocol = 2

	// Datagram selects pass-through framing for datagram transports.
	Datagram Protocol = 3
)

type ReadWriter

type ReadWriter struct {
	*Reader
	*Writer
}

ReadWriter groups a Reader and Writer that share one framer state.

ReadWriter is a convenience wrapper for coordinated use. Concurrent access requires caller synchronization.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads framed messages from a stream or packet-preserving transport.

A Reader carries in-flight state after ErrWouldBlock or ErrMore. Retry the interrupted operation on the same Reader instance before switching between Read and WriteTo.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read reads one message payload into p.

In SeqPacket and Datagram modes, Read passes through one transport read. In BinaryStream mode, p must fit the whole message payload or Read returns io.ErrShortBuffer without consuming the payload.

In SeqPacket/Datagram mode, WithReadLimit is enforced after one transport read, so an oversized packet may return (n > limit, ErrTooLong); n reports bytes copied from that packet. A packet read returning (0, nil) completes an empty message.

If Read returns ErrWouldBlock or ErrMore after partial stream progress, retry Read on the same Reader instance with the same buffer until the message completes or a terminal error is returned.

The count is payload bytes copied during this call. On a stream retry, new bytes are placed after the previously copied prefix in p. Accumulate counts across retries to find the complete message length; a successful finishing call can return zero when an earlier call copied the final bytes with a control signal. A nil error completes one message, including an empty one.

func (*Reader) WriteTo

func (r *Reader) WriteTo(dst io.Writer) (int64, error)

WriteTo writes messages from r to dst until the source reaches EOF or returns an error.

Semantics:

  • Stream (BinaryStream): transfers one framed message payload at a time from the underlying reader into dst. The payload bytes are written as-is; this method does not attempt to preserve or reconstruct framer wire format on the destination unless dst is itself a framer.Writer. It uses an internal reusable scratch buffer sized by the Reader's ReadLimit; when ReadLimit is zero, a conservative default cap is used (64KiB) and messages exceeding this cap result in ErrTooLong.
  • Packet (SeqPacket/Datagram): pass-through byte transfer for arbitrary dst. Partial dst writes resume from an internal byte cursor on the next WriteTo call. When dst is a framer packet Writer, whole-packet retry is used after zero-progress ErrWouldBlock/ErrMore. When dst is a framer stream Writer, same-message frame retry is used so the destination frame length remains stable. ReadLimit is checked post-read with one sentinel byte, so oversized packets are rejected before forwarding.

Non-blocking semantics: if the underlying reader or writer returns iox.ErrWouldBlock or iox.ErrMore, WriteTo returns immediately with the progress count and the same control signal. Retry WriteTo on the same Reader instance and destination to resume the interrupted transfer. Short writes on dst are handled per io.Writer contract. If a packet source returns bytes with an error, that source error is reported after the admitted packet is emitted, even if write-side suspension requires a later WriteTo call. A packet source's (0, nil) is an empty message, preserved for framer destinations; zero bytes with a non-nil source error do not form a message.

The returned count is bytes written to dst. A rejected oversized packet does not add to the count.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer writes framed messages to a stream or packet-preserving transport.

A Writer can carry in-flight state after ErrWouldBlock or ErrMore. Retry an interrupted BinaryStream Write or ReadFrom operation on the same Writer before switching operations. Direct packet-mode Write carries no hidden replay state after the whole packet has been accepted.

func (*Writer) ReadFrom

func (w *Writer) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads chunks from src and writes each chunk as a framed message.

Semantics:

  • Chunk-to-message: each positive-length chunk read from src is encoded as a single framed message and written via w.Write. This is efficient but does not preserve upstream application message boundaries. SeqPacket/Datagram output emits each chunk as a packet; it does not identify src as a packet source. A source (0, nil) returns io.ErrNoProgress without another read or an empty output message. Use a packet Reader or Forwarder to preserve empty input packets.

Non-blocking semantics: if src.Read or the underlying writer returns iox.ErrWouldBlock or iox.ErrMore, ReadFrom returns immediately with the same control signal. Retry ReadFrom on the same Writer instance to resume the interrupted message. Pure write-side resume calls can return n == 0 because the pending bytes were already read from src and admitted to the Writer.

The returned count is bytes read from src and admitted to this Writer. If src.Read returns n > 0 with an error, the source error is reported after that admitted chunk is emitted. If write-side ErrWouldBlock or ErrMore is observed first, the source signal remains pending for a later ReadFrom call. No heap allocations occur in the steady-state path.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write writes p as one message.

In BinaryStream mode, Write emits a length prefix before p. In SeqPacket and Datagram modes, Write passes p through as one transport packet and reports io.ErrShortWrite if the destination accepts only a prefix.

The returned count is bytes accepted from p during this call; BinaryStream header bytes are not included. A successful finishing call can return zero when an earlier call accepted the final payload bytes with a control signal. In BinaryStream mode, if Write returns ErrWouldBlock or ErrMore, retry Write on the same Writer instance with the same message length before starting another message. An empty frame's header may already be written before suspension; retry finishes that frame without another header. In packet modes, n == len(p) && n > 0 with ErrWouldBlock or ErrMore means the packet was accepted; do not replay p. Empty packet output is accepted only with nil error. If packet-mode Write returns ErrWouldBlock or ErrMore with n == 0, retry with the same packet, including an empty one.

Directories

Path Synopsis
internal
bo
Package bo provides native byte order selection.
Package bo provides native byte order selection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL