packet

package
v2.6.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 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(message *Message) (buffer.Buffer, error)

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

func Read added in v2.6.0

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

Read reads a message as a buffer. It reports whether the message is a heartbeat, the server-side timestamp in nanoseconds, the message buffer and any read error.

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 Message

type Message struct {
	Seq    int32  // Sequence number
	Route  int32  // Route ID
	Buffer []byte // Message content
}

Message is a message.

It is used to pass plain data messages between encoding and decoding.

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 message as a buffer. It reports whether the message is a heartbeat, the
	// server-side timestamp in nanoseconds, the message buffer and any read error.
	Read(reader io.Reader) (bool, int64, buffer.Buffer, error)
	// PackMessage packs message as a buffer and returns the packed buffer or a packing error.
	PackMessage(message *Message) (buffer.Buffer, 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)
	// 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)
	// PackHeartbeat packs a heartbeat. Pass server as true to pack a server-side heartbeat.
	PackHeartbeat(server ...bool) buffer.Buffer
}

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