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

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

#include <aegis2d/net/bits.hpp>

Public Member Functions

 BitReader (std::span< const std::byte > bytes) noexcept
 Starts reading at the first bit of bytes.
 
std::optional< std::uint64_t > read_bits (unsigned width) noexcept
 Reads an unsigned value.
 
std::optional< std::int64_t > read_signed (unsigned width) noexcept
 Reads a signed value in two's complement.
 
std::optional< bool > read_bool () noexcept
 Reads a boolean, one bit.
 
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 bit_position () const noexcept
 Number of bits read so far.
 
std::size_t bits_remaining () const noexcept
 Number of bits not read yet, the padding of the last byte included.
 

Detailed Description

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

When not enough bits remain or a width is invalid, the read returns an empty std::optional, moves nothing and the reader fails: every later read fails too, so a truncated or forged payload is never half-parsed with shifted fields.

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

Constructor & Destructor Documentation

◆ BitReader()

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

Starts reading at the first bit of bytes.

Parameters
bytesReceived data, for instance the payload of a datagram.

Member Function Documentation

◆ at_end()

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

Tells whether everything was read without error.

Parsers check it last, to reject payloads with trailing data.

Returns
true if no read failed and what remains is only the 0 bits BitWriter puts at the end of the last byte.

◆ bit_position()

std::size_t aegis2d::net::BitReader::bit_position ( ) const
noexcept

Number of bits read so far.

Returns
The offset of the next read, in bits.

◆ bits_remaining()

std::size_t aegis2d::net::BitReader::bits_remaining ( ) const
noexcept

Number of bits not read yet, the padding of the last byte included.

Returns
The size of the buffer in bits minus bit_position().

◆ failed()

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

Tells whether a read has failed.

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

◆ read_bits()

std::optional< std::uint64_t > aegis2d::net::BitReader::read_bits ( unsigned  width)
noexcept

Reads an unsigned value.

Parameters
widthNumber of bits, from 0 to BitWriter::max_width; 0 reads the value 0.
Returns
The value, or nothing if the reader has failed, width is above BitWriter::max_width or fewer than width bits remain.

◆ read_bool()

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

Reads a boolean, one bit.

Returns
The value, or nothing if the reader has failed or no bit remains.

◆ read_signed()

std::optional< std::int64_t > aegis2d::net::BitReader::read_signed ( unsigned  width)
noexcept

Reads a signed value in two's complement.

Parameters
widthNumber of bits, sign included, from 1 to BitWriter::max_width.
Returns
The value, or nothing if the reader has failed, width is out of range or fewer than width bits remain.

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