Skip to content

atl::wire

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

pub struct Writer

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

Methods

pub fn new() -> Writer

An empty writer.

pub fn bytes(&mut self, b: &[u8])

Appends raw bytes.

pub fn u8(&mut self, v: u8)

Appends one byte.

pub fn u32(&mut self, v: u32)

Appends a 32-bit integer (little-endian).

pub fn varint(&mut self, mut v: u64)

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

pub fn object(&mut self, v: Value) -> usize

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

pub fn buffer(&self) -> &[u8]

The bytes written so far.

pub fn objects(&self) -> &[Value]

The game objects referred to by index.

Implements Default

pub struct Reader<'a>

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.

Methods

pub fn new(bytes: &'a [u8], objects: Value) -> Reader<'a>

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

pub fn bytes_into(&mut self, out: &mut [u8]) -> bool

Reads out.len() raw bytes into out.

pub fn u8(&mut self) -> u8

Reads one byte.

pub fn u32(&mut self) -> u32

Reads a 32-bit integer.

pub fn varint(&mut self) -> u64

Reads a variable-length integer.

pub fn count(&mut self) -> usize

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

pub fn view(&mut self, n: usize) -> &'a [u8]

Reads n bytes as a slice of the input.

pub fn object(&mut self) -> Value

Reads a game object written by Writer::object.

pub fn fail(&mut self)

Marks the reader as failed.

pub fn ok(&self) -> bool

True while every read has succeeded.

pub fn done(&self) -> bool

True when everything succeeded and the whole input was consumed.

pub fn remaining(&self) -> usize

Bytes left to read.

pub struct TypeInfo

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

Fields

pub name: u32
pub schema: u32
pub display: &'static str

Implements Clone, Copy, Debug

pub struct Packet

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

Fields

pub kind: PacketKind
pub bytes: Vec<u8>
pub objects: Value
pub struct Assembler

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

Methods

pub fn add(&mut self, sender: &Value, chunk: &[u8], out: &mut Vec<u8>) -> AssembleResult

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

Implements Default

pub enum PacketKind

What a received payload is.

Variants

  • None
  • Whole
  • Chunk

Implements Clone, Copy, Debug, Eq, PartialEq

pub enum AssembleResult

Outcome of adding a chunk.

Variants

  • Pending
  • Complete
  • Error

Implements Clone, Copy, Debug, Eq, PartialEq

pub enum UnpackError

Why unpack_as failed.

Variants

  • NotPacked
  • OtherType
  • OtherDefinition
  • Malformed

Implements Clone, Copy, Debug, Eq, PartialEq

pub trait Codec: Sized

How a type is encoded: write, read, and schema, a hash of the type's layout used to reject messages from builds with a different definition. Implement it to send your own types.

Trait items

fn write(&self, w: &mut Writer)

Appends the value.

fn read(r: &mut Reader) -> Self

Reads a value. On malformed input, marks the reader failed.

fn schema() -> u32

A hash of the type's layout.

pub trait Message: Codec + 'static

A typed message or saved state: a struct with #[derive(atl::Message)].

pub const fn fnv(s: &str) -> u32

FNV-1a hash of a string.

pub const fn fnv_from(s: &str, mut h: u32) -> u32

FNV-1a hash of a string, continuing from h.

pub const fn mix(h: u32, v: u32) -> u32

Combines a hash with another value.

pub fn type_name<T: ?Sized>() -> &'static str

The compiler's name for type T, e.g. "my_mod::lamp::State".

pub fn type_info<M: Message>() -> TypeInfo

The TypeInfo of message type M.

pub fn register<M: Message>() -> TypeInfo

Registers M so a receiver can name it in error messages.

pub fn find_type(name: u32) -> Option<TypeInfo>

The registered type with a name hash, or None.

pub fn encode_message<M: Message>(msg: &M) -> Writer

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

pub fn encoded_size<M: Message>(msg: &M) -> usize

Encoded size of a message in bytes.

pub const fn packet_count_for(encoded: usize) -> usize

How many packets a message of encoded bytes needs.

pub fn packet_count<M: Message>(msg: &M) -> usize

How many packets a message needs.

pub fn pack<M: Message>(msg: &M) -> Value

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

pub fn packets<M: Message>(msg: &M, action: &str) -> Vec<Value>

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

pub fn read_packet(payload: &Value) -> Packet

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

pub fn unpack_as<M: Message>(payload: &Value) -> Result<M, UnpackError>

Decodes a whole payload as message type M.

pub const COBS: bool = !cfg!(feature = "wire-base64")

True when this build sends COBS (the default) rather than base64.

pub const PREFIX: &str = if COBS { "@atl2:" } else { "@atl1:" }

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

pub const CHUNK_PREFIX: &str = if COBS { "@atlc:" } else { "@atlk:" }
pub const SERVER_INBOX: &str = "__atl_sv"

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

pub const CLIENT_INBOX: &str = "__atl_cl"
pub const CHUNK_BYTES: usize = if COBS { 60000 } else { 45000 }

Encoded bytes per packet, keeping each packet under NETWORK_PACKET_LIMIT once encoded.

pub const MAX_MESSAGE_BYTES: usize = 1 << 20

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