frame

package
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 2 Imported by: 5

Documentation

Overview

Package frame implements a binary frame protocol for inter-process communication.

Each frame consists of a header (12 bytes minimum) followed by a variable-length payload. The header is laid out as follows:

Byte 0:       version (upper 4 bits) | header length in 32-bit words (lower 4 bits)
Byte 1:       flags — codec selection (CodecRaw, CodecJSON, CodecGob, CodecProto)
              and control flags (CONTROL, ERROR)
Bytes 2-5:    payload length (little-endian uint32, max ~4 GB)
Bytes 6-9:    CRC32 (IEEE) of bytes 0-5
Bytes 10-11:  stream control bits (STREAM, STOP, PING, PONG)

When header length exceeds 3 words (12 bytes), the additional words carry up to 10 option values (uint32 each, 40 bytes maximum) appended after the base header.

Index

Constants

View Source
const (
	CONTROL      byte = 0x01
	CodecRaw     byte = 0x04
	CodecJSON    byte = 0x08
	CodecMsgpack byte = 0x10
	CodecGob     byte = 0x20
	ERROR        byte = 0x40
	CodecProto   byte = 0x80

	// Version1 byte
	Version1 byte = 0x01

	// STREAM bit
	STREAM byte = 0x01
	// STOP command
	STOP byte = 0x02
	// PING command
	PING byte = 0x04
	// PONG command
	PONG byte = 0x08
)

BYTE flags, it means, that we can set multiply flags from this group using bitwise OR For example CONTEXT_SEPARATOR | CodecRaw

View Source
const Headroom = 12 + OptionsMaxSize

Headroom is the space kept in front of a pooled payload for the header, so that Wire can hand the whole frame to a single Write without copying the payload.

View Source
const OptionsMaxSize = 40

OptionsMaxSize represents header's options maximum size

View Source
const WORD = 4

WORD represents 32bit word

Variables

This section is empty.

Functions

This section is empty.

Types

type Frame

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

Frame defines new user level package format.

func From

func From(header []byte, payload []byte) *Frame

From wraps the given header and payload slices as a Frame. The frame takes ownership of both slices: WritePayload, WriteOptions, AppendOptions and Reset write into them.

func NewFrame

func NewFrame() *Frame

NewFrame initializes a new frame with a 12-byte header and no payload. The header has capacity for the maximum of 10 options, so WriteOptions does not allocate. The payload is borrowed from the buffer pool on the first AllocPayload or WritePayload; Reset returns it when it is larger than the smallest pool tier.

func ReadFrame

func ReadFrame(data []byte) *Frame

ReadFrame produces a Frame from raw bytes. The first 12 bytes (or more if options are present) form the header; the rest is the payload. Without options, ReadFrame clears bytes 10 and 11 of data. The frame aliases data: WritePayload, WriteOptions, AppendOptions and Reset write into it, and a header extension overwrites the start of the payload.

func ReadHeader

func ReadHeader(data []byte) *Frame

ReadHeader reads only the header (first 12 bytes) from data, without payload. The frame aliases data: WriteOptions, AppendOptions and Reset write into it.

func (*Frame) AllocPayload

func (f *Frame) AllocPayload(n int) []byte

AllocPayload sets the payload length to n and returns it for the caller to fill. The contents are undefined. The payload is resliced in place when its capacity fits, both for a pooled buffer and for caller memory; otherwise a buffer of the right tier is taken from the pool. n == 0 never takes a buffer. A pooled payload starts Headroom bytes into its buffer, see Wire.

func (*Frame) AppendOptions

func (*Frame) AppendOptions(header *[]byte, options []byte)

AppendOptions appends raw option bytes to the header. The header is extended in place when its capacity is sufficient, otherwise it is reallocated.

func (*Frame) Bytes

func (f *Frame) Bytes() []byte

Bytes returns the full frame as a single byte slice (header + payload).

func (*Frame) Header

func (f *Frame) Header() []byte

Header returns the frame header byte slice.

func (*Frame) HeaderPtr

func (f *Frame) HeaderPtr() *[]byte

HeaderPtr returns a pointer to the frame header slice.

func (*Frame) IsPing

func (*Frame) IsPing(header []byte) bool

IsPing reports whether the PING bit is set on byte 10 of the header.

func (*Frame) IsPong

func (*Frame) IsPong(header []byte) bool

IsPong reports whether the PONG bit is set on byte 10 of the header.

func (*Frame) IsStop

func (*Frame) IsStop(header []byte) bool

IsStop reports whether the STOP bit is set on byte 10 of the header.

func (*Frame) IsStream

func (*Frame) IsStream(header []byte) bool

IsStream reports whether the STREAM bit is set on byte 10 of the header.

func (*Frame) Payload

func (f *Frame) Payload() []byte

Payload returns the frame payload without the header. The slice is valid until the next AllocPayload, WritePayload or Reset on the frame.

func (*Frame) ReadFlags

func (f *Frame) ReadFlags() byte

ReadFlags returns the flags byte (byte 1) of the frame header.

func (*Frame) ReadHL

func (*Frame) ReadHL(header []byte) byte

ReadHL reads the header length from the lower 4 bits of byte 0. The upper 4 bits (version) are masked off with bitwise AND 0x0F.

func (*Frame) ReadOptions

func (f *Frame) ReadOptions(header []byte) []uint32

ReadOptions reads uint32 option values from the extended header. Returns nil if no options are present (HL <= 3). Option count is derived from HL - 3.

func (*Frame) ReadPayloadLen

func (*Frame) ReadPayloadLen(header []byte) uint32

ReadPayloadLen reads the payload length from bytes 2-5 of the header in little-endian format.

func (*Frame) ReadVersion

func (*Frame) ReadVersion(header []byte) byte

ReadVersion returns the protocol version from the upper 4 bits of byte 0. 1111_0000 -> 0000_1111 (15)

func (*Frame) Reset

func (f *Frame) Reset()

Reset clears the frame, restoring it to its initial state with a 12-byte header and an empty payload. The header keeps its capacity. A payload buffer of the smallest pool tier stays on the frame, so small frames do not pay a pool round trip per cycle; a larger one goes back to the pool. Either way slices returned earlier by Payload are invalid after Reset. A payload that aliases caller memory is only truncated. The header capacity must be at least 12 bytes.

func (*Frame) SetPingBit

func (*Frame) SetPingBit(header []byte)

SetPingBit sets the PING bit on byte 10 of the header.

func (*Frame) SetPongBit

func (*Frame) SetPongBit(header []byte)

SetPongBit sets the PONG bit on byte 10 of the header.

func (*Frame) SetStopBit

func (*Frame) SetStopBit(header []byte)

SetStopBit sets the STOP bit on byte 10 of the header.

func (*Frame) SetStreamFlag

func (*Frame) SetStreamFlag(header []byte)

SetStreamFlag sets the STREAM bit on byte 10 of the header.

func (*Frame) VerifyCRC

func (*Frame) VerifyCRC(header []byte) bool

VerifyCRC verifies the CRC32 checksum stored in bytes 6-9 against the computed checksum of bytes 0-5. Returns false if the checksums do not match, indicating a corrupted frame.

func (*Frame) Wire

func (f *Frame) Wire() (wire []byte, ok bool)

Wire writes the header into the headroom in front of a pooled payload and returns the contiguous header-plus-payload slice, valid until the next write or Reset. ok is false when the frame has no pooled payload, an empty payload, or a header longer than Headroom; the caller then sends the header and the payload separately.

func (*Frame) WriteCRC

func (*Frame) WriteCRC(header []byte)

WriteCRC calculates the CRC32 checksum of bytes 0-5 and writes it into bytes 6-9 of the header.

func (*Frame) WriteFlags

func (*Frame) WriteFlags(header []byte, flags ...byte)

WriteFlags sets one or more flags on byte 1 of the header using bitwise OR.

func (*Frame) WriteOptions

func (f *Frame) WriteOptions(header *[]byte, options ...uint32)

WriteOptions writes uint32 option values into the header, extending it by 4 bytes per option. At most 10 options (40 bytes) are allowed. The header is extended in place when its capacity is sufficient, otherwise it is reallocated, so the header pointer is required.

func (*Frame) WritePayload

func (f *Frame) WritePayload(data []byte)

WritePayload copies data into the frame's payload. See AllocPayload for where the buffer comes from.

func (*Frame) WritePayloadLen

func (*Frame) WritePayloadLen(header []byte, payloadLen uint32)

WritePayloadLen writes the payload length into bytes 2-5 of the header in little-endian format.

func (*Frame) WriteVersion

func (*Frame) WriteVersion(header []byte, version byte)

WriteVersion writes the protocol version (0-15) into the upper 4 bits of byte 0. The version is shifted left by 4 and OR'd with the existing byte to preserve the header length.

Jump to

Keyboard shortcuts

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