SmartFoxServer 3 C++ Client API 3.1.0-beta
Client API for SmartFoxServer 3
Loading...
Searching...
No Matches
sfs3::entities::ByteBuffer Class Reference

A growable buffer of bytes, with a read cursor and a write cursor. More...

#include <ByteBuffer.h>

Public Member Functions

 ByteBuffer ()
 Builds an unbounded buffer, which grows as long as there is memory.
 ByteBuffer (size_t capacity)
 Builds a bounded buffer.
 ByteBuffer (std::vector< uint8_t > bytes)
 Builds an unbounded buffer around bytes that already exist.
size_t readPosition () const
void readPosition (size_t newPos)
size_t writePosition () const
void writePosition (size_t newPos)
size_t capacity () const
size_t remaining () const
bool hasRemaining () const
void rewind ()
 Puts the read cursor back to the start of the buffer.
void clear ()
 Empties the buffer and puts both cursors back to the start.
BufferView view () const
 Returns a view of the whole backing store.
BufferView remainingView () const
 Returns a view of the region still to read, from the read cursor to the write cursor.
const std::vector< uint8_t > & bytes () const
 Returns the backing store.
uint8_t read ()
BufferView readView (size_t size)
 Reads a run of bytes without copying them.
std::vector< uint8_t > read (size_t size)
 Reads a run of bytes into a new vector.
void prepend (BufferView header)
 Inserts bytes in front of the content that is already in the buffer.
void prepend (const std::vector< uint8_t > &bytes)
void prepend (const uint8_t *bytes, size_t len)
std::string hexDump () const
std::string status () const

Static Public Member Functions

static std::string hexDump (BufferView data)

Detailed Description

A growable buffer of bytes, with a read cursor and a write cursor.

Every read and write moves its own cursor forward, so you can fill a buffer and then read it back from the start with ByteBuffer::rewind. All the multi-byte numbers are big-endian, which is the byte order of the SmartFoxServer protocol.

A buffer is either bounded, with a fixed capacity given at construction, or unbounded, in which case it grows as needed.

Constructor & Destructor Documentation

◆ ByteBuffer() [1/2]

sfs3::entities::ByteBuffer::ByteBuffer ( size_t capacity)

Builds a bounded buffer.

Parameters
capacitythe fixed size of the buffer, in bytes

◆ ByteBuffer() [2/2]

sfs3::entities::ByteBuffer::ByteBuffer ( std::vector< uint8_t > bytes)

Builds an unbounded buffer around bytes that already exist.

Parameters
bytesthe bytes the buffer takes over

Member Function Documentation

◆ readPosition() [1/2]

size_t sfs3::entities::ByteBuffer::readPosition ( ) const
Returns
the position of the read cursor

◆ readPosition() [2/2]

void sfs3::entities::ByteBuffer::readPosition ( size_t newPos)
Parameters
newPosthe new position of the read cursor

◆ writePosition() [1/2]

size_t sfs3::entities::ByteBuffer::writePosition ( ) const
Returns
the position of the write cursor, which is also the amount of data in the buffer

◆ writePosition() [2/2]

void sfs3::entities::ByteBuffer::writePosition ( size_t newPos)
Parameters
newPosthe new position of the write cursor

◆ capacity()

size_t sfs3::entities::ByteBuffer::capacity ( ) const
Returns
the size of the backing store, in bytes

◆ remaining()

size_t sfs3::entities::ByteBuffer::remaining ( ) const
Returns
the number of bytes still to read, that is, write position minus read position

◆ hasRemaining()

bool sfs3::entities::ByteBuffer::hasRemaining ( ) const
Returns
true if there is at least one byte still to read

◆ view()

BufferView sfs3::entities::ByteBuffer::view ( ) const

Returns a view of the whole backing store.

NOTE: this covers the full capacity, including the bytes past the write position that were never written. To read only the data that is really in the buffer, bound the view with ByteBuffer::writePosition, or use ByteBuffer::remainingView.

Returns
a view of the backing store

◆ remainingView()

BufferView sfs3::entities::ByteBuffer::remainingView ( ) const

Returns a view of the region still to read, from the read cursor to the write cursor.

Use this, and not ByteBuffer::view, on a buffer that is partly consumed: view() always starts at the first byte and would hand back the bytes the reader already walked past.

Returns
a view of the bytes still to read

◆ bytes()

const std::vector< uint8_t > & sfs3::entities::ByteBuffer::bytes ( ) const

Returns the backing store.

NOTE: as with ByteBuffer::view, this is the full capacity, including any bytes past the write position that were never written.

Returns
the bytes of the buffer

◆ read() [1/2]

uint8_t sfs3::entities::ByteBuffer::read ( )
Returns
the next byte, moving the read cursor forward by one

◆ readView()

BufferView sfs3::entities::ByteBuffer::readView ( size_t size)

Reads a run of bytes without copying them.

Parameters
sizehow many bytes to read
Returns
a view of those bytes, valid as long as the buffer is not changed

◆ read() [2/2]

std::vector< uint8_t > sfs3::entities::ByteBuffer::read ( size_t size)

Reads a run of bytes into a new vector.

Parameters
sizehow many bytes to read
Returns
a copy of those bytes

◆ prepend() [1/3]

void sfs3::entities::ByteBuffer::prepend ( BufferView header)

Inserts bytes in front of the content that is already in the buffer.

This is how the protocol wraps a payload in its headers. The cost grows with the size of the buffer, because the payload is moved forward. Both cursors move with it, so a read that is in progress stays on the same byte.

Parameters
headerthe bytes to insert at the start

◆ prepend() [2/3]

void sfs3::entities::ByteBuffer::prepend ( const std::vector< uint8_t > & bytes)

◆ prepend() [3/3]

void sfs3::entities::ByteBuffer::prepend ( const uint8_t * bytes,
size_t len )

◆ hexDump() [1/2]

std::string sfs3::entities::ByteBuffer::hexDump ( ) const
Returns
the content of the buffer as a pretty-printed hexadecimal dump

◆ hexDump() [2/2]

std::string sfs3::entities::ByteBuffer::hexDump ( BufferView data)
static
Returns
the given bytes as a pretty-printed hexadecimal dump

◆ status()

std::string sfs3::entities::ByteBuffer::status ( ) const
Returns
the capacity and the two cursor positions as text, for logging and debugging

The documentation for this class was generated from the following files:
  • entities/ByteBuffer.h
  • entities/ByteBuffer.cpp