R-Type 0.1
Loading...
Searching...
No Matches
PacketReader Class Reference

Bounds checked reader of big-endian values over a byte buffer. More...

#include <protocol/PacketReader.hpp>

Public Member Functions

 PacketReader (const std::uint8_t *data, std::size_t size)
 Creates a reader over a raw buffer that it does not own.
 
 PacketReader (const std::vector< uint8_t > &data)
 Creates a reader over a vector that must outlive the reader.
 
bool readUInt8 (std::uint8_t &value)
 Reads one byte.
 
bool readUInt16 (std::uint16_t &value)
 Reads a big-endian 16-bit unsigned integer.
 
bool readUInt32 (std::uint32_t &value)
 Reads a big-endian 32-bit unsigned integer.
 
bool readBytes (std::uint8_t *destination, std::size_t size)
 Copies raw bytes out of the buffer.
 
std::size_t getOffset () const
 Returns the number of bytes already read.
 
std::size_t getRemaining () const
 Returns the number of bytes left to read.
 
bool isEnd () const
 Tells whether every byte has been read.
 
void reset ()
 Moves the read offset back to the first byte.
 

Detailed Description

Bounds checked reader of big-endian values over a byte buffer.

Constructor & Destructor Documentation

◆ PacketReader() [1/2]

PacketReader::PacketReader ( const std::uint8_t * data,
std::size_t size )

Creates a reader over a raw buffer that it does not own.

Parameters
dataFirst byte of the buffer.
sizeNumber of readable bytes.

◆ PacketReader() [2/2]

PacketReader::PacketReader ( const std::vector< uint8_t > & data)

Creates a reader over a vector that must outlive the reader.

Parameters
dataBuffer to read from.

Member Function Documentation

◆ readUInt8()

bool PacketReader::readUInt8 ( std::uint8_t & value)

Reads one byte.

Parameters
valueReceives the byte.
Returns
True on success, false if the buffer is exhausted.

◆ readUInt16()

bool PacketReader::readUInt16 ( std::uint16_t & value)

Reads a big-endian 16-bit unsigned integer.

Parameters
valueReceives the decoded value.
Returns
True on success, false if fewer than 2 bytes remain.

◆ readUInt32()

bool PacketReader::readUInt32 ( std::uint32_t & value)

Reads a big-endian 32-bit unsigned integer.

Parameters
valueReceives the decoded value.
Returns
True on success, false if fewer than 4 bytes remain.

◆ readBytes()

bool PacketReader::readBytes ( std::uint8_t * destination,
std::size_t size )

Copies raw bytes out of the buffer.

Parameters
destinationBuffer of at least size bytes.
sizeNumber of bytes to copy.
Returns
True on success, false if not enough bytes remain.

◆ getOffset()

std::size_t PacketReader::getOffset ( ) const

Returns the number of bytes already read.

Returns
The current read offset.

◆ getRemaining()

std::size_t PacketReader::getRemaining ( ) const

Returns the number of bytes left to read.

Returns
The remaining byte count.

◆ isEnd()

bool PacketReader::isEnd ( ) const

Tells whether every byte has been read.

Returns
True if nothing is left to read.