packet

package
v2.6.4 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 9 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractHandshake added in v2.6.3

func ExtractHandshake(buf buffer.Buffer) ([]byte, error)

ExtractHandshake extracts the payload of a handshake frame.

func ExtractMessage added in v2.6.3

func ExtractMessage(buf buffer.Buffer) (int32, int32, []byte, error)

ExtractMessage returns the route, the sequence number and the payload of buf without modifying the buffer, so it is safe to call on a shared buffer. The payload may alias buf and must not be used after buf is released or modified.

func ExtractRouteSeq added in v2.6.0

func ExtractRouteSeq(buf buffer.Buffer) (int32, int32, error)

ExtractRouteSeq extracts the route and sequence number from the message buffer buf. It returns the route, the sequence number and any unpacking error.

func MessageBufferHeadroom added in v2.6.3

func MessageBufferHeadroom() int

MessageBufferHeadroom returns the space that the global packer can use before a pooled payload. It returns zero when the packer does not support in-place header prepending.

func NewPacker

func NewPacker(opts ...Option) *defaultPacker

NewPacker returns a new default packer.

It validates the options and prebuilds the heartbeat packet. The program terminates when the given options are invalid.

func PackHandshake added in v2.6.3

func PackHandshake(payload []byte) (buffer.Buffer, error)

PackHandshake packs a handshake frame.

Handshake frames carry neither a route nor a sequence number, and they are marked by a reserved header bit, so they do not consume a route number.

func PackHeartbeat added in v2.1.0

func PackHeartbeat(server ...bool) buffer.Buffer

PackHeartbeat packs a heartbeat.

The returned heartbeat buffer must neither be modified nor released. Pass server as true to pack a server-side heartbeat.

func PackMessage added in v2.1.0

func PackMessage(route, seq int32, data []byte) (buffer.Buffer, error)

PackMessage packs a message payload as a buffer and returns the packed buffer or a packing error.

func PackMessageBuffer added in v2.6.3

func PackMessageBuffer(route, seq int32, payload *buffer.Bytes) (buffer.Buffer, error)

PackMessageBuffer packs a pooled payload, transferring ownership only on success. Packers without MessageBufferPacker support receive a copy so the pooled memory can be released safely.

func SetPacker

func SetPacker(packer Packer)

SetPacker sets the global packer.

It overrides the global packer and is used to replace the default packing implementation.

func UnpackMessage added in v2.1.0

func UnpackMessage(buf buffer.Buffer) (int32, int32, buffer.Buffer, error)

UnpackMessage unpacks the message buffer buf. It returns the route, the sequence number, the message buffer and any unpacking error.

Types

type FrameType added in v2.6.3

type FrameType uint8

FrameType is the type of a frame read from the wire.

const (
	// FrameData is a business data frame.
	FrameData FrameType = iota
	// FrameHeartbeat is a heartbeat frame.
	FrameHeartbeat
	// FrameHandshake is a handshake frame.
	FrameHandshake
)

func Read added in v2.6.0

func Read(reader io.Reader) (FrameType, int64, buffer.Buffer, error)

Read reads a frame as a buffer. It reports the frame type, the server-side timestamp in nanoseconds, the frame buffer and any read error.

type MessageBufferHeadroomPacker added in v2.6.3

type MessageBufferHeadroomPacker interface {
	MessageBufferHeadroom() int
}

MessageBufferHeadroomPacker reports how much space a pooled payload can reserve so that PackMessageBuffer can prepend the message header without wrapping the payload in another buffer.

type MessageBufferPacker added in v2.6.3

type MessageBufferPacker interface {
	PackMessageBuffer(route, seq int32, payload *buffer.Bytes) (buffer.Buffer, error)
}

MessageBufferPacker packs a pooled payload without copying it. On success the returned buffer owns payload; on error the caller retains ownership.

type MessageExtractor added in v2.6.3

type MessageExtractor interface {
	// ExtractMessage returns the route, the sequence number and the payload of buf.
	ExtractMessage(buf buffer.Buffer) (int32, int32, []byte, error)
}

MessageExtractor is implemented by packers that can extract the payload of a packed message without modifying the buffer.

type Option

type Option func(o *options)

func WithBufferBytes

func WithBufferBytes(bufferBytes int) Option

WithBufferBytes returns an Option that sets the number of message bytes, that is, the maximum allowed message size.

func WithByteOrder

func WithByteOrder(byteOrder binary.ByteOrder) Option

WithByteOrder returns an Option that sets the byte order.

func WithHeartbeatTime added in v2.1.0

func WithHeartbeatTime(heartbeatTime bool) Option

WithHeartbeatTime returns an Option that sets whether the heartbeat time is carried.

func WithRouteBytes

func WithRouteBytes(routeBytes int) Option

WithRouteBytes returns an Option that sets the number of route bytes.

The number of route bytes must be 1, 2 or 4.

func WithSeqBytes

func WithSeqBytes(seqBytes int) Option

WithSeqBytes returns an Option that sets the number of sequence-number bytes.

It may be 0, 1, 2 or 4; sequence-number encoding is disabled when it is 0.

type Packer

type Packer interface {
	// Read reads a frame as a buffer. It reports the frame type, the server-side timestamp in
	// nanoseconds, the frame buffer and any read error. The timestamp is meaningful only for
	// heartbeat frames.
	Read(reader io.Reader) (FrameType, int64, buffer.Buffer, error)
	// PackHandshake packs a handshake frame. A handshake frame carries neither a route nor a
	// sequence number, and it is marked by a reserved header bit.
	PackHandshake(payload []byte) (buffer.Buffer, error)
	// PackHeartbeat packs a heartbeat. Pass server as true to pack a server-side heartbeat.
	PackHeartbeat(server ...bool) buffer.Buffer
	// PackMessage packs a message payload and returns the packed buffer or a packing error.
	PackMessage(route, seq int32, data []byte) (buffer.Buffer, error)
	// UnpackMessage unpacks the message buffer buf. It returns the route, the sequence number,
	// the message buffer and any unpacking error.
	UnpackMessage(buf buffer.Buffer) (int32, int32, buffer.Buffer, error)
	// ExtractHandshake extracts the payload of a handshake frame. It reports
	// [errors.ErrInvalidMessage] when buf is not a handshake frame.
	ExtractHandshake(buf buffer.Buffer) ([]byte, error)
	// ExtractRouteSeq extracts the route and sequence number from the message buffer buf. It
	// returns the route, the sequence number and any unpacking error.
	ExtractRouteSeq(buf buffer.Buffer) (int32, int32, error)
}

Packer defines the encoding and decoding capabilities of messages.

func GetPacker

func GetPacker() Packer

GetPacker returns the global packer.

Jump to

Keyboard shortcuts

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