atl/wire
import "atl/wire"atl/wire: the binary encoding of typed network messages and saved state.
Constants
Section titled "Constants"const Cobs = falseTrue when network messages are COBS-encoded rather than base64.
const Cobs = trueTrue 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 << 20The largest message that can be sent split into packets (1 MiB).
Variables
Section titled "Variables"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.
Functions
Section titled "Functions"func Fnv(s string) uint32FNV-1a hash of a string.
func Mix(h, v uint32) uint32Combines a hash with another value.
func TypeName(t reflect.Type) stringThe 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) uint32The layout hash of t, computed once.
func CheckMessage(t reflect.Type)Rejects message types that aren't plain structs.
func EncodedSize(msg any) intEncoded size of a message in bytes.
func PacketCount(encoded int) intHow many packets a message of encoded bytes needs.
func Pack(msg any) atl.ValueEncodes a message as one value regardless of size, for storage (which has no packet limit).
func Packets(msg any, action string) []atl.ValueEncodes 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.
Writer
Section titled "Writer"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) *WriterEncodes 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) intStores a game object and returns its index, to be written in its place.
func (w *Writer) Buffer() []byteThe bytes written so far.
func (w *Writer) Objects() []atl.ValueThe game objects referred to by index.
Reader
Section titled "Reader"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) *ReaderReads bytes; objects is the payload table holding the referenced game objects.
Methods
func (r *Reader) Bytes(n int) []byteReads n raw bytes.
func (r *Reader) U8() uint8func (r *Reader) U32() uint32Reads one byte / a 32-bit integer.
func (r *Reader) Varint() uint64Reads a variable-length integer.
func (r *Reader) Count() intReads an element count, failing if it's implausibly large for the remaining data.
func (r *Reader) Object() atl.ValueReads a game object written by Writer.Object.
func (r *Reader) Fail()Marks the reader as failed.
func (r *Reader) Ok() boolTrue while every read has succeeded.
func (r *Reader) Done() boolTrue when everything succeeded and the whole input was consumed.
func (r *Reader) Remaining() intBytes 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.
Decoder
Section titled "Decoder"type Decoder interface { WireDecode(r *Reader)}The decoding half of Codec.
TypeInfo
Section titled "TypeInfo"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) *TypeInfoThe TypeInfo of message type t, registering it so a receiver can name it in error messages.
func FindType(name uint32) *TypeInfoThe registered type with a name hash, or nil.
PacketKind
Section titled "PacketKind"type PacketKind intWhat a received payload is.
const ( PacketNone PacketKind = iota PacketWhole PacketChunk)Packet
Section titled "Packet"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) PacketRecognizes and decodes a payload in any of the supported formats.
AssembleResult
Section titled "AssembleResult"type AssembleResult intOutcome of adding a chunk.
const ( Pending AssembleResult = iota Complete Broken)Assembler
Section titled "Assembler"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.
UnpackError
Section titled "UnpackError"type UnpackError intWhy Unpack failed.
const ( UnpackOk UnpackError = iota NotPacked OtherType OtherDefinition Malformed)func Unpack(payload atl.Value, p any) UnpackErrorDecodes a whole payload into the message p points to.