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

Reads values written by BinaryWriter from untrusted bytes, never past their end. More...

#include <aegis2d/net/binary.hpp>

Public Member Functions

 BinaryReader (std::span< const std::byte > bytes) noexcept
 Starts reading at the beginning of bytes.
 
std::optional< std::uint8_t > read_u8 () noexcept
 Reads an unsigned 8-bit integer.
 
std::optional< std::uint16_t > read_u16 () noexcept
 Reads an unsigned 16-bit integer.
 
std::optional< std::uint32_t > read_u32 () noexcept
 Reads an unsigned 32-bit integer.
 
std::optional< std::uint64_t > read_u64 () noexcept
 Reads an unsigned 64-bit integer.
 
std::optional< std::int8_t > read_i8 () noexcept
 Reads a signed 8-bit integer.
 
std::optional< std::int16_t > read_i16 () noexcept
 Reads a signed 16-bit integer.
 
std::optional< std::int32_t > read_i32 () noexcept
 Reads a signed 32-bit integer.
 
std::optional< std::int64_t > read_i64 () noexcept
 Reads a signed 64-bit integer.
 
std::optional< float > read_f32 () noexcept
 Reads a 32-bit float.
 
std::optional< double > read_f64 () noexcept
 Reads a 64-bit float.
 
std::optional< bool > read_bool () noexcept
 Reads a boolean.
 
std::optional< std::span< const std::byte > > read_bytes (std::size_t count) noexcept
 Reads raw bytes without copying them.
 
std::optional< std::string > read_string ()
 Reads a string written by BinaryWriter::write_string().
 
bool failed () const noexcept
 Tells whether a read has failed.
 
bool at_end () const noexcept
 Tells whether everything was read without error.
 
std::size_t position () const noexcept
 Number of bytes read so far.
 
std::size_t remaining () const noexcept
 Number of bytes not read yet.
 

Detailed Description

Reads values written by BinaryWriter from untrusted bytes, never past their end.

Every read checks that enough bytes remain. When they do not, or when a value is invalid (a boolean other than 0 or 1), the read returns an empty std::optional and the reader fails: every later read fails too, so a truncated or forged packet is never half-parsed with shifted fields. Lengths are checked against the remaining bytes before anything is allocated.

The reader does not copy the bytes: they must outlive it.

Constructor & Destructor Documentation

◆ BinaryReader()

aegis2d::net::BinaryReader::BinaryReader ( std::span< const std::byte >  bytes)
explicitnoexcept

Starts reading at the beginning of bytes.

Parameters
bytesReceived data, for instance a datagram.

Member Function Documentation

◆ at_end()

bool aegis2d::net::BinaryReader::at_end ( ) const
noexcept

Tells whether everything was read without error.

Parsers check it last, to reject packets with trailing bytes.

Returns
true if no read failed and no byte remains.

◆ failed()

bool aegis2d::net::BinaryReader::failed ( ) const
noexcept

Tells whether a read has failed.

Returns
true once a read returned nothing; it stays true.

◆ position()

std::size_t aegis2d::net::BinaryReader::position ( ) const
noexcept

Number of bytes read so far.

Returns
The offset of the next read.

◆ read_bool()

std::optional< bool > aegis2d::net::BinaryReader::read_bool ( )
noexcept

Reads a boolean.

Returns
The value, or nothing if the reader has failed, no byte remains or the byte is neither 0 nor 1 (the reader then fails).

◆ read_bytes()

std::optional< std::span< const std::byte > > aegis2d::net::BinaryReader::read_bytes ( std::size_t  count)
noexcept

Reads raw bytes without copying them.

Parameters
countNumber of bytes to read.
Returns
A view of the bytes, valid as long as the buffer given to the reader, or nothing if the reader has failed or fewer than count bytes remain.

◆ read_f32()

std::optional< float > aegis2d::net::BinaryReader::read_f32 ( )
noexcept

Reads a 32-bit float.

Any bit pattern is accepted, NaN and infinities included: callers that need a finite value check it.

Returns
The value, or nothing if the reader has failed or fewer than 4 bytes remain.

◆ read_f64()

std::optional< double > aegis2d::net::BinaryReader::read_f64 ( )
noexcept

Reads a 64-bit float.

Any bit pattern is accepted, NaN and infinities included.

Returns
The value, or nothing if the reader has failed or fewer than 8 bytes remain.

◆ read_i16()

std::optional< std::int16_t > aegis2d::net::BinaryReader::read_i16 ( )
noexcept

Reads a signed 16-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 2 bytes remain.

◆ read_i32()

std::optional< std::int32_t > aegis2d::net::BinaryReader::read_i32 ( )
noexcept

Reads a signed 32-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 4 bytes remain.

◆ read_i64()

std::optional< std::int64_t > aegis2d::net::BinaryReader::read_i64 ( )
noexcept

Reads a signed 64-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 8 bytes remain.

◆ read_i8()

std::optional< std::int8_t > aegis2d::net::BinaryReader::read_i8 ( )
noexcept

Reads a signed 8-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 1 byte remains.

◆ read_string()

std::optional< std::string > aegis2d::net::BinaryReader::read_string ( )

Reads a string written by BinaryWriter::write_string().

The length is checked against the remaining bytes before the string is allocated. The bytes are not checked to be valid UTF-8.

Returns
The text, or nothing if the reader has failed or the length or the bytes are missing.

◆ read_u16()

std::optional< std::uint16_t > aegis2d::net::BinaryReader::read_u16 ( )
noexcept

Reads an unsigned 16-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 2 bytes remain.

◆ read_u32()

std::optional< std::uint32_t > aegis2d::net::BinaryReader::read_u32 ( )
noexcept

Reads an unsigned 32-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 4 bytes remain.

◆ read_u64()

std::optional< std::uint64_t > aegis2d::net::BinaryReader::read_u64 ( )
noexcept

Reads an unsigned 64-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 8 bytes remain.

◆ read_u8()

std::optional< std::uint8_t > aegis2d::net::BinaryReader::read_u8 ( )
noexcept

Reads an unsigned 8-bit integer.

Returns
The value, or nothing if the reader has failed or fewer than 1 byte remains.

◆ remaining()

std::size_t aegis2d::net::BinaryReader::remaining ( ) const
noexcept

Number of bytes not read yet.

Returns
The buffer size minus position().

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