atl/wire.hpp
atl::wire: the binary encoding of typed network messages and saved state.
atl::wire
Section titled "atl::wire"constexpr uint32_t fnv(StringView s, uint32_t h = 2166136261u);FNV-1a hash of a string.
Parameters
s(StringView)h(uint32_t)
Returns uint32_t
constexpr uint32_t mix(uint32_t h, uint32_t v);Combines a hash with another value.
Parameters
h(uint32_t)v(uint32_t)
Returns uint32_t
template <class T> StringView typeName();The compiler's name for type T, e.g. "lamp::State".
Returns StringView
template <class T> void encode(Writer &w, const T &v);Writes v using its Codec.
Parameters
w(Writer &)v(const double &)
template <class T> void decode(Reader &r, T &v);Reads v using its Codec.
Parameters
r(Reader &)v(double &)
template <class T> uint32_t schemaOf();The layout hash of T, computed once.
Returns uint32_t
inline Vector<const TypeInfo *> ®istry();Every message type this module has sent or registered.
Returns Vector<const TypeInfo *> &
template <class M> const TypeInfo &typeInfo();The TypeInfo of message type M.
Returns const TypeInfo &
inline const TypeInfo *findType(uint32_t name);The registered type with a name hash, or nullptr.
Parameters
name(uint32_t)
Returns const TypeInfo *
template <class M> constexpr void checkMessage();Rejects, at compile time, message types that aren't plain structs.
inline constexpr bool kCobs = true;inline constexpr StringView kPrefix = kCobs ? "@atl2:" : "@atl1:";Prefix of a whole message / of one chunk of a split message.
inline constexpr StringView kChunkPrefix = kCobs ? "@atlc:" : "@atlk:";Prefix of a whole message / of one chunk of a split message.
inline constexpr const char *kServerInbox = "__atl_sv";The method names the SDK receives typed messages under, on the server and client.
inline constexpr const char *kClientInbox = "__atl_cl";The method names the SDK receives typed messages under, on the server and client.
inline constexpr size_t kChunkBytes = kCobs ? 60000 : 45000;Encoded bytes per packet, keeping each packet under sm::kNetworkPacketLimit once encoded.
inline constexpr size_t kMaxMessageBytes = 1 << 20;The largest message that can be sent split into packets (1 MiB).
template <class M> Writer encodeMessage(const M &msg);Encodes a message: the type's name hash, its layout hash, then its fields.
Parameters
msg(const M &)
Returns Writer
template <class M> size_t encodedSize(const M &msg);Encoded size of a message in bytes.
Parameters
msg(const M &)
Returns size_t
inline constexpr size_t packetCount(size_t encoded);How many packets a message of encoded bytes needs.
Parameters
encoded(size_t)
Returns size_t
template <class M> size_t packetCount(const M &msg);How many packets a message needs.
Parameters
msg(const M &)
Returns size_t
template <class M> Value pack(const M &msg);Encodes a message as one value regardless of size, for storage (which has no packet limit).
Parameters
msg(const M &)
Returns Value
template <class M> Vector<Value> packets(const M &msg, StringView action);Encodes a message as one or more network payloads. Messages over kChunkBytes are split into chunks (kChunkPrefix + transfer id, index, count, bytes); the game objects travel with the last chunk. Logs an error and returns nothing above kMaxMessageBytes.
Parameters
msg(const M &)action(StringView)
Returns Vector<Value>
inline Packet readPacket(const Value &payload);Recognizes and decodes a payload in any of the supported formats.
Parameters
payload(const Value &)
Returns Packet
template <class M> Optional<M> unpackAs(const Value &payload, UnpackError *error = nullptr);Decodes a whole payload as message type M, or none (with the reason in *error).
Parameters
payload(const Value &)error(UnpackError *)
Returns Optional<M>
PacketKind
Section titled "PacketKind"What a received payload is.
enum class PacketKind { None, Whole, Chunk };UnpackError
Section titled "UnpackError"Why unpackAs failed.
enum class UnpackError { None, NotPacked, OtherType, OtherDefinition, Malformed };Writer
Section titled "Writer"Builds a message's bytes, collecting the game objects it refers to separately.
class Writervoid bytes(const void *p, size_t n);Appends raw bytes.
Parameters
p(const void *)n(size_t)
void u8(uint8_t v);Appends one byte / a 32-bit integer.
Parameters
v(uint8_t)
void u32(uint32_t v);Appends one byte / a 32-bit integer.
Parameters
v(uint32_t)
void varint(uint64_t v);Appends an unsigned integer in 7-bit variable-length form.
Parameters
v(uint64_t)
size_t object(Value v);Stores a game object and returns its index, to be written in its place.
Parameters
v(Value)
Returns size_t
const String &buffer() const;The bytes written so far.
Returns const String &
const Vector<Value> &objects() const;The game objects referred to by index.
Returns const Vector<Value> &
Reader
Section titled "Reader"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.
class ReaderReader(StringView bytes, Value objects);Reads bytes; objects is the payload table holding the referenced game objects.
Parameters
bytes(StringView)objects(Value)
bool bytes(void *p, size_t n);Reads n raw bytes into p.
Parameters
p(void *)n(size_t)
Returns bool
uint8_t u8();Reads one byte / a 32-bit integer.
Returns uint8_t
uint32_t u32();Reads one byte / a 32-bit integer.
Returns uint32_t
uint64_t varint();Reads a variable-length integer.
Returns uint64_t
size_t count();Reads an element count, failing if it's implausibly large for the remaining data.
Returns size_t
StringView view(size_t n);Reads n bytes as a view into the input.
Parameters
n(size_t)
Returns StringView
Value object();Reads a game object written by Writer::object.
Returns Value
void fail();Marks the reader as failed.
bool ok() const;True while every read has succeeded.
Returns bool
bool done() const;True when everything succeeded and the whole input was consumed.
Returns bool
size_t remaining() const;Bytes left to read.
Returns size_t
How a type is encoded. Each specialization provides write(Writer&, const T&), read(Reader&, T&), and schema(), a hash of the type's layout used to reject messages from builds with a different definition. Specialize it to send your own types.
template <class T> struct Codecstatic void write(Writer &w, const T &v);Parameters
w(Writer &)v(const T &)
static void read(Reader &r, T &v);Parameters
r(Reader &)v(T &)
static uint32_t schema();Returns uint32_t
Plain structs: each field is encoded in declaration order, so no field list is needed. The schema hashes the struct's name and every field's type.
template <class T> struct Codecstatic void write(Writer &w, const T &v);Parameters
w(Writer &)v(const T &)
static void read(Reader &r, T &v);Parameters
r(Reader &)v(T &)
static uint32_t schema();Returns uint32_t
TypeInfo
Section titled "TypeInfo"A registered message type: its name hash, layout hash and printable name.
struct TypeInfouint32_t name;uint32_t schema;StringView display;Packet
Section titled "Packet"A decoded payload: its kind, its bytes, and the table holding its game objects.
struct PacketPacketKind kind = PacketKind::None;String bytes;Value objects;Assembler
Section titled "Assembler"Reassembles split messages. Packets from one sender arrive in order, so a new transfer from a sender abandons that sender's unfinished one.
class AssemblerResult add(const Value &sender, StringView chunk, String &out);Adds a chunk from sender. On Complete, out holds the whole message's bytes.
Parameters
sender(const Value &)chunk(StringView)out(String &)
Returns Result
Result
Section titled "Result"Outcome of adding a chunk.
enum class Result { Pending, Complete, Error };