Skip to content

atl/wire

import "atl/wire"

atl/wire: the binary encoding of typed network messages and saved state.

const Cobs = false

True when network messages are COBS-encoded rather than base64.

const Cobs = true

True when network messages are COBS-encoded rather than base64.

const (
ServerInbox = "__atl_sv"
ClientInbox = "__atl_cl"
)

The method names the SDK receives typed messages under, on the server and client.

const MaxMessageBytes = 1 << 20

The largest message that can be sent split into packets (1 MiB).

var (
Prefix = pick("@atl2:", "@atl1:")
ChunkPrefix = pick("@atlc:", "@atlk:")
)

Prefix of a whole message / of one chunk of a split message.

var ChunkBytes = pick(60000, 45000)

Encoded bytes per packet, keeping each packet under sys.NetworkPacketLimit once encoded.

func Fnv(s string) uint32

FNV-1a hash of a string.

func Mix(h, v uint32) uint32

Combines a hash with another value.

func TypeName(t reflect.Type) string

The name of type t, e.g. "lamp.State".

func Encode(w *Writer, v any)

Writes v.

func Decode(r *Reader, p any)

Reads into the value p points to.

func SchemaOf(t reflect.Type) uint32

The layout hash of t, computed once.

func CheckMessage(t reflect.Type)

Rejects message types that aren't plain structs.

func EncodedSize(msg any) int

Encoded size of a message in bytes.

func PacketCount(encoded int) int

How many packets a message of encoded bytes needs.

func Pack(msg any) atl.Value

Encodes a message as one value regardless of size, for storage (which has no packet limit).

func Packets(msg any, action string) []atl.Value

Encodes a message as one or more network payloads. Messages over ChunkBytes are split into chunks (ChunkPrefix + transfer id, index, count, bytes); the game objects travel with the last chunk. Logs an error and returns nothing above MaxMessageBytes.

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

Builds a message's bytes, collecting the game objects it refers to separately.

func EncodeMessage(msg any) *Writer

Encodes a message: the type's name hash, its layout hash, then its fields.

Methods

func (w *Writer) Bytes(p []byte)

Appends raw bytes.

func (w *Writer) U8(v uint8)
func (w *Writer) U32(v uint32)

Appends one byte / a 32-bit integer.

func (w *Writer) Varint(v uint64)

Appends an unsigned integer in 7-bit variable-length form.

func (w *Writer) Object(v atl.Value) int

Stores a game object and returns its index, to be written in its place.

func (w *Writer) Buffer() []byte

The bytes written so far.

func (w *Writer) Objects() []atl.Value

The game objects referred to by index.

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

Reads a message's bytes. Any read past the end or invalid data marks the reader as failed instead of trapping; failed reads return zeros.

func NewReader(bytes []byte, objects atl.Value) *Reader

Reads bytes; objects is the payload table holding the referenced game objects.

Methods

func (r *Reader) Bytes(n int) []byte

Reads n raw bytes.

func (r *Reader) U8() uint8
func (r *Reader) U32() uint32

Reads one byte / a 32-bit integer.

func (r *Reader) Varint() uint64

Reads a variable-length integer.

func (r *Reader) Count() int

Reads an element count, failing if it's implausibly large for the remaining data.

func (r *Reader) Object() atl.Value

Reads a game object written by Writer.Object.

func (r *Reader) Fail()

Marks the reader as failed.

func (r *Reader) Ok() bool

True while every read has succeeded.

func (r *Reader) Done() bool

True when everything succeeded and the whole input was consumed.

func (r *Reader) Remaining() int

Bytes left to read.

type Codec interface {
WireEncode(w *Writer)
WireSchema() uint32
}

How a type is encoded, for types that encode themselves. WireSchema is a hash of the type's layout used to reject messages from builds with a different definition. Implement it (WireDecode on a pointer) to send your own types.

type Decoder interface {
WireDecode(r *Reader)
}

The decoding half of Codec.

type TypeInfo struct {
Name uint32
Schema uint32
Display string
}

A registered message type: its name hash, layout hash and printable name.

func TypeInfoOf(t reflect.Type) *TypeInfo

The TypeInfo of message type t, registering it so a receiver can name it in error messages.

func FindType(name uint32) *TypeInfo

The registered type with a name hash, or nil.

type PacketKind int

What a received payload is.

const (
PacketNone PacketKind = iota
PacketWhole
PacketChunk
)
type Packet struct {
Kind PacketKind
Bytes []byte
Objects atl.Value
}

A decoded payload: its kind, its bytes, and the table holding its game objects.

func ReadPacket(payload atl.Value) Packet

Recognizes and decodes a payload in any of the supported formats.

type AssembleResult int

Outcome of adding a chunk.

const (
Pending AssembleResult = iota
Complete
Broken
)
type Assembler struct {
// contains filtered or unexported fields
}

Reassembles split messages. Packets from one sender arrive in order, so a new transfer from a sender abandons that sender's unfinished one.

Methods

func (a *Assembler) Add(sender atl.Value, chunk []byte) (AssembleResult, []byte)

Adds a chunk from sender. On Complete, the result holds the whole message's bytes.

type UnpackError int

Why Unpack failed.

const (
UnpackOk UnpackError = iota
NotPacked
OtherType
OtherDefinition
Malformed
)
func Unpack(payload atl.Value, p any) UnpackError

Decodes a whole payload into the message p points to.