Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::net::BinaryWriter Class Reference

Writes values field by field into a growing byte buffer. More...

#include <aegis2d/net/binary.hpp>

Public Member Functions

void write_u8 (std::uint8_t value)
 Appends an unsigned 8-bit integer (1 byte).
 
void write_u16 (std::uint16_t value)
 Appends an unsigned 16-bit integer (2 bytes).
 
void write_u32 (std::uint32_t value)
 Appends an unsigned 32-bit integer (4 bytes).
 
void write_u64 (std::uint64_t value)
 Appends an unsigned 64-bit integer (8 bytes).
 
void write_i8 (std::int8_t value)
 Appends a signed 8-bit integer (1 byte, two's complement).
 
void write_i16 (std::int16_t value)
 Appends a signed 16-bit integer (2 bytes, two's complement).
 
void write_i32 (std::int32_t value)
 Appends a signed 32-bit integer (4 bytes, two's complement).
 
void write_i64 (std::int64_t value)
 Appends a signed 64-bit integer (8 bytes, two's complement).
 
void write_f32 (float value)
 Appends a 32-bit float (4 bytes, IEEE 754 bits).
 
void write_f64 (double value)
 Appends a 64-bit float (8 bytes, IEEE 754 bits).
 
void write_bool (bool value)
 Appends a boolean as one byte, 0 or 1.
 
void write_bytes (std::span< const std::byte > bytes)
 Appends raw bytes, without length: the reader must know how many to read.
 
void write_string (std::string_view text)
 Appends a string as its byte length on 16 bits, then its bytes (UTF-8).
 
std::span< const std::byte > data () const noexcept
 Bytes written so far.
 
std::size_t size () const noexcept
 Number of bytes written so far.
 
void clear () noexcept
 Empties the buffer and keeps its memory, to write the next packet.
 

Static Public Attributes

static constexpr std::size_t max_string_size = 0xFFFF
 Longest string write_string() accepts: its length is stored on 16 bits.
 

Detailed Description

Writes values field by field into a growing byte buffer.

Every value has an explicit size and is stored in little-endian order, whatever the machine: a packet is the same bytes on Linux, Windows and macOS. Structs are never copied as they are in memory, so padding and compiler layout never reach the wire. Floating-point values are stored as their IEEE 754 bits.

Member Function Documentation

◆ clear()

void aegis2d::net::BinaryWriter::clear ( )
noexcept

Empties the buffer and keeps its memory, to write the next packet.

◆ data()

std::span< const std::byte > aegis2d::net::BinaryWriter::data ( ) const
noexcept

Bytes written so far.

Returns
A view of the buffer, valid until the next write or clear().

◆ size()

std::size_t aegis2d::net::BinaryWriter::size ( ) const
noexcept

Number of bytes written so far.

Returns
The size of data().

◆ write_bool()

void aegis2d::net::BinaryWriter::write_bool ( bool  value)

Appends a boolean as one byte, 0 or 1.

Parameters
valueValue to write.

◆ write_bytes()

void aegis2d::net::BinaryWriter::write_bytes ( std::span< const std::byte >  bytes)

Appends raw bytes, without length: the reader must know how many to read.

Parameters
bytesBytes to copy.

◆ write_f32()

void aegis2d::net::BinaryWriter::write_f32 ( float  value)

Appends a 32-bit float (4 bytes, IEEE 754 bits).

Parameters
valueValue to write.

◆ write_f64()

void aegis2d::net::BinaryWriter::write_f64 ( double  value)

Appends a 64-bit float (8 bytes, IEEE 754 bits).

Parameters
valueValue to write.

◆ write_i16()

void aegis2d::net::BinaryWriter::write_i16 ( std::int16_t  value)

Appends a signed 16-bit integer (2 bytes, two's complement).

Parameters
valueValue to write.

◆ write_i32()

void aegis2d::net::BinaryWriter::write_i32 ( std::int32_t  value)

Appends a signed 32-bit integer (4 bytes, two's complement).

Parameters
valueValue to write.

◆ write_i64()

void aegis2d::net::BinaryWriter::write_i64 ( std::int64_t  value)

Appends a signed 64-bit integer (8 bytes, two's complement).

Parameters
valueValue to write.

◆ write_i8()

void aegis2d::net::BinaryWriter::write_i8 ( std::int8_t  value)

Appends a signed 8-bit integer (1 byte, two's complement).

Parameters
valueValue to write.

◆ write_string()

void aegis2d::net::BinaryWriter::write_string ( std::string_view  text)

Appends a string as its byte length on 16 bits, then its bytes (UTF-8).

Parameters
textText to write.
Exceptions
std::length_errorif text is longer than max_string_size bytes.

◆ write_u16()

void aegis2d::net::BinaryWriter::write_u16 ( std::uint16_t  value)

Appends an unsigned 16-bit integer (2 bytes).

Parameters
valueValue to write.

◆ write_u32()

void aegis2d::net::BinaryWriter::write_u32 ( std::uint32_t  value)

Appends an unsigned 32-bit integer (4 bytes).

Parameters
valueValue to write.

◆ write_u64()

void aegis2d::net::BinaryWriter::write_u64 ( std::uint64_t  value)

Appends an unsigned 64-bit integer (8 bytes).

Parameters
valueValue to write.

◆ write_u8()

void aegis2d::net::BinaryWriter::write_u8 ( std::uint8_t  value)

Appends an unsigned 8-bit integer (1 byte).

Parameters
valueValue to write.

Member Data Documentation

◆ max_string_size

constexpr std::size_t aegis2d::net::BinaryWriter::max_string_size = 0xFFFF
staticconstexpr

Longest string write_string() accepts: its length is stored on 16 bits.


The documentation for this class was generated from the following file: