protocol

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 2 Imported by: 1 module, 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.

Documentation

Overview

Protocol package implements a simple framing protocol for sending and receiving data.

Frame format: A CCCC DDD...DDD FFFF Where: A: command byte C: payload length (32 bits, big endian) D: payload (array of bytes) F: checksum (FNV-1a, 32 bits, big endian)

The checksum covers the command byte, payload length, and the data itself.

Index

Examples

Constants

View Source
const (
	MSG    = 0x1
	RESIZE = 0x2
)

Frame command bytes. Mirrored in assets/term.js; change both together.

View Source
const BufferSize = 262144

BufferSize is the largest payload a frame carries, and the read buffer size used throughout the project.

View Source
const MaxPackageSize = BufferSize + Overhead

MaxPackageSize is the largest a whole frame can be.

View Source
const Overhead = 9

Overhead is the number of framing bytes around a payload: command (1) + length (4) + checksum (4).

Variables

View Source
var (
	ErrInvalidSize     = errors.New("invalid size")
	ErrInvalidChecksum = errors.New("invalid checksum")
)

Functions

func Decode

func Decode(dest, src []byte) (cmd byte, n int, err error)

Decode reads one frame from src into dest, returning the command byte and the payload length.

Example
out := make([]byte, MaxPackageSize)
n, err := Encode(out, []byte("hello"), 0x01)
if err != nil {
	panic(err)
}

data := make([]byte, BufferSize)
cmd, n, err := Decode(data, out[:n])
if err != nil {
	panic(err)
}

fmt.Printf("cmd: %02X\n", cmd)
fmt.Printf("data: %v\n", string(data[:n]))
Output:
cmd: 01
data: hello

func Encode

func Encode(dest, src []byte, cmd byte) (int, error)

Encode frames src into dest with the given command and returns the frame length.

Example
out := make([]byte, MaxPackageSize)
n, err := Encode(out, []byte("hello"), 0x01)
if err != nil {
	panic(err)
}
fmt.Printf("frame: %02X\n", out[:n])
Output:
frame: 010000000568656C6C6FEFE3E091

Types

This section is empty.

Jump to

Keyboard shortcuts

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