Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cByteBuffer Class Reference

An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes are stored in a ringbuffer of constant size; if more than that size is requested, the write operation fails. More...

#include <ByteBuffer.h>

Public Member Functions

bool CanReadBytes (size_t a_Count) const
 Returns true if the specified amount of bytes are available for reading. More...
 
bool CanWriteBytes (size_t a_Count) const
 Returns true if the specified amount of bytes are available for writing. More...
 
 cByteBuffer (size_t a_BufferSize)
 
void CheckValid (void) const
 Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs. More...
 
void CommitRead (void)
 Removes the bytes that have been read from the ringbuffer. More...
 
size_t GetDataStart (void) const
 Returns the current data start index. More...
 
size_t GetFreeSpace (void) const
 Returns the number of bytes that can be successfully written to the ringbuffer. More...
 
size_t GetReadableSpace (void) const
 Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already) More...
 
size_t GetUsedSpace (void) const
 Returns the number of bytes that are currently in the ringbuffer. More...
 
void ReadAgain (ContiguousByteBuffer &a_Out)
 Re-reads the data that has been read since the last commit to the current readpos. More...
 
void ReadAll (ContiguousByteBuffer &a_Data)
 Reads all available data into a_Data. More...
 
bool ReadBEDouble (double &a_Value)
 
bool ReadBEFloat (float &a_Value)
 
bool ReadBEInt16 (Int16 &a_Value)
 
bool ReadBEInt32 (Int32 &a_Value)
 
bool ReadBEInt64 (Int64 &a_Value)
 
bool ReadBEInt8 (Int8 &a_Value)
 
bool ReadBEUInt16 (UInt16 &a_Value)
 
bool ReadBEUInt32 (UInt32 &a_Value)
 
bool ReadBEUInt64 (UInt64 &a_Value)
 
bool ReadBEUInt8 (UInt8 &a_Value)
 
bool ReadBool (bool &a_Value)
 
bool ReadBuf (void *a_Buffer, size_t a_Count)
 Reads a_Count bytes into a_Buffer; returns true if successful. More...
 
bool ReadLEInt (int &a_Value)
 
bool ReadSome (ContiguousByteBuffer &a_String, size_t a_Count)
 Reads a_Count bytes into a_String; returns true if successful. More...
 
bool ReadToByteBuffer (cByteBuffer &a_Dst, size_t a_NumBytes)
 Reads the specified number of bytes and writes it into the destinatio bytebuffer. More...
 
bool ReadUUID (cUUID &a_Value)
 
template<typename T >
bool ReadVarInt (T &a_Value)
 Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short, char, Byte, double, ...) More...
 
bool ReadVarInt32 (UInt32 &a_Value)
 
bool ReadVarInt64 (UInt64 &a_Value)
 
bool ReadVarUTF8String (AString &a_Value)
 
bool ReadXYZPosition64 (int &a_BlockX, int &a_BlockY, int &a_BlockZ)
 
bool ReadXYZPosition64 (Vector3i &a_Position)
 
bool ReadXZYPosition64 (int &a_BlockX, int &a_BlockY, int &a_BlockZ)
 
bool ReadXZYPosition64 (Vector3i &a_Position)
 
void ResetRead (void)
 Restarts next reading operation at the start of the ringbuffer. More...
 
bool SkipRead (size_t a_Count)
 Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer. More...
 
bool Write (const void *a_Bytes, size_t a_Count)
 Writes the bytes specified to the ringbuffer. More...
 
bool WriteBEDouble (double a_Value)
 
bool WriteBEFloat (float a_Value)
 
bool WriteBEInt16 (Int16 a_Value)
 
bool WriteBEInt32 (Int32 a_Value)
 
bool WriteBEInt64 (Int64 a_Value)
 
bool WriteBEInt8 (Int8 a_Value)
 
bool WriteBEInt8 (std::byte a_Value)
 
bool WriteBEUInt16 (UInt16 a_Value)
 
bool WriteBEUInt32 (UInt32 a_Value)
 
bool WriteBEUInt64 (UInt64 a_Value)
 
bool WriteBEUInt8 (UInt8 a_Value)
 
bool WriteBool (bool a_Value)
 
bool WriteBuf (const void *a_Buffer, size_t a_Count)
 Writes a_Count bytes into a_Buffer; returns true if successful. More...
 
bool WriteBuf (size_t a_Count, unsigned char a_Value)
 Writes a_Count bytes into a_Buffer; returns true if successful. More...
 
bool WriteLEInt32 (Int32 a_Value)
 
bool WriteVarInt32 (UInt32 a_Value)
 
bool WriteVarInt64 (UInt64 a_Value)
 
bool WriteVarUTF8String (const AString &a_Value)
 
bool WriteXYZPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
 
bool WriteXZYPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
 
 ~cByteBuffer ()
 

Static Public Member Functions

static bool CanBEInt16Represent (int a_Value)
 Returns if the given value can fit in a protocol big-endian 16 bit integer. More...
 
static bool CanBEInt8Represent (int a_Value)
 Returns if the given value can fit in a protocol big-endian 8 bit integer. More...
 
static size_t GetVarIntSize (UInt32 a_Value)
 Gets the number of bytes that are needed to represent the given VarInt. More...
 

Protected Member Functions

void AdvanceReadPos (size_t a_Count)
 Advances the m_ReadPos by a_Count bytes. More...
 

Protected Attributes

std::byte * m_Buffer
 
size_t m_BufferSize
 
size_t m_DataStart
 
size_t m_ReadPos
 
std::thread::id m_ThreadID
 The ID of the thread currently accessing the object. More...
 
size_t m_WritePos
 

Detailed Description

An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes are stored in a ringbuffer of constant size; if more than that size is requested, the write operation fails.

The bytes stored can be retrieved using various ReadXXX functions; these assume that the needed number of bytes are present in the buffer (ASSERT; for performance reasons). The reading doesn't actually remove the bytes, it only moves the internal read ptr. To remove the bytes, call CommitRead(). To re-start reading from the beginning, call ResetRead(). This class doesn't implement thread safety, the clients of this class need to provide their own synchronization.

Definition at line 31 of file ByteBuffer.h.

Constructor & Destructor Documentation

◆ cByteBuffer()

cByteBuffer::cByteBuffer ( size_t  a_BufferSize)

Definition at line 85 of file ByteBuffer.cpp.

◆ ~cByteBuffer()

cByteBuffer::~cByteBuffer ( )

Definition at line 100 of file ByteBuffer.cpp.

Member Function Documentation

◆ AdvanceReadPos()

void cByteBuffer::AdvanceReadPos ( size_t  a_Count)
protected

Advances the m_ReadPos by a_Count bytes.

Definition at line 1057 of file ByteBuffer.cpp.

◆ CanBEInt16Represent()

bool cByteBuffer::CanBEInt16Represent ( int  a_Value)
static

Returns if the given value can fit in a protocol big-endian 16 bit integer.

Definition at line 226 of file ByteBuffer.cpp.

◆ CanBEInt8Represent()

bool cByteBuffer::CanBEInt8Represent ( int  a_Value)
static

Returns if the given value can fit in a protocol big-endian 8 bit integer.

Definition at line 217 of file ByteBuffer.cpp.

◆ CanReadBytes()

bool cByteBuffer::CanReadBytes ( size_t  a_Count) const

Returns true if the specified amount of bytes are available for reading.

Definition at line 235 of file ByteBuffer.cpp.

◆ CanWriteBytes()

bool cByteBuffer::CanWriteBytes ( size_t  a_Count) const

Returns true if the specified amount of bytes are available for writing.

Definition at line 246 of file ByteBuffer.cpp.

◆ CheckValid()

void cByteBuffer::CheckValid ( void  ) const

Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs.

Definition at line 1072 of file ByteBuffer.cpp.

◆ CommitRead()

void cByteBuffer::CommitRead ( void  )

Removes the bytes that have been read from the ringbuffer.

Definition at line 1013 of file ByteBuffer.cpp.

◆ GetDataStart()

size_t cByteBuffer::GetDataStart ( void  ) const
inline

Returns the current data start index.

For debugging purposes.

Definition at line 51 of file ByteBuffer.h.

◆ GetFreeSpace()

size_t cByteBuffer::GetFreeSpace ( void  ) const

Returns the number of bytes that can be successfully written to the ringbuffer.

Definition at line 164 of file ByteBuffer.cpp.

◆ GetReadableSpace()

size_t cByteBuffer::GetReadableSpace ( void  ) const

Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already)

Definition at line 198 of file ByteBuffer.cpp.

◆ GetUsedSpace()

size_t cByteBuffer::GetUsedSpace ( void  ) const

Returns the number of bytes that are currently in the ringbuffer.

Note GetReadableBytes()

Definition at line 185 of file ByteBuffer.cpp.

◆ GetVarIntSize()

size_t cByteBuffer::GetVarIntSize ( UInt32  a_Value)
static

Gets the number of bytes that are needed to represent the given VarInt.

Definition at line 1082 of file ByteBuffer.cpp.

◆ ReadAgain()

void cByteBuffer::ReadAgain ( ContiguousByteBuffer a_Out)

Re-reads the data that has been read since the last commit to the current readpos.

Used by ProtoProxy to duplicate communication

Definition at line 1035 of file ByteBuffer.cpp.

◆ ReadAll()

void cByteBuffer::ReadAll ( ContiguousByteBuffer a_Data)

Reads all available data into a_Data.

Definition at line 977 of file ByteBuffer.cpp.

◆ ReadBEDouble()

bool cByteBuffer::ReadBEDouble ( double &  a_Value)

Definition at line 385 of file ByteBuffer.cpp.

◆ ReadBEFloat()

bool cByteBuffer::ReadBEFloat ( float &  a_Value)

Definition at line 371 of file ByteBuffer.cpp.

◆ ReadBEInt16()

bool cByteBuffer::ReadBEInt16 ( Int16 a_Value)

Definition at line 283 of file ByteBuffer.cpp.

◆ ReadBEInt32()

bool cByteBuffer::ReadBEInt32 ( Int32 a_Value)

Definition at line 313 of file ByteBuffer.cpp.

◆ ReadBEInt64()

bool cByteBuffer::ReadBEInt64 ( Int64 a_Value)

Definition at line 343 of file ByteBuffer.cpp.

◆ ReadBEInt8()

bool cByteBuffer::ReadBEInt8 ( Int8 a_Value)

Definition at line 257 of file ByteBuffer.cpp.

◆ ReadBEUInt16()

bool cByteBuffer::ReadBEUInt16 ( UInt16 a_Value)

Definition at line 299 of file ByteBuffer.cpp.

◆ ReadBEUInt32()

bool cByteBuffer::ReadBEUInt32 ( UInt32 a_Value)

Definition at line 329 of file ByteBuffer.cpp.

◆ ReadBEUInt64()

bool cByteBuffer::ReadBEUInt64 ( UInt64 a_Value)

Definition at line 357 of file ByteBuffer.cpp.

◆ ReadBEUInt8()

bool cByteBuffer::ReadBEUInt8 ( UInt8 a_Value)

Definition at line 270 of file ByteBuffer.cpp.

◆ ReadBool()

bool cByteBuffer::ReadBool ( bool &  a_Value)

Definition at line 399 of file ByteBuffer.cpp.

◆ ReadBuf()

bool cByteBuffer::ReadBuf ( void *  a_Buffer,
size_t  a_Count 
)

Reads a_Count bytes into a_Buffer; returns true if successful.

Definition at line 836 of file ByteBuffer.cpp.

◆ ReadLEInt()

bool cByteBuffer::ReadLEInt ( int &  a_Value)

Definition at line 486 of file ByteBuffer.cpp.

◆ ReadSome()

bool cByteBuffer::ReadSome ( ContiguousByteBuffer a_String,
size_t  a_Count 
)

Reads a_Count bytes into a_String; returns true if successful.

Definition at line 927 of file ByteBuffer.cpp.

◆ ReadToByteBuffer()

bool cByteBuffer::ReadToByteBuffer ( cByteBuffer a_Dst,
size_t  a_NumBytes 
)

Reads the specified number of bytes and writes it into the destinatio bytebuffer.

Returns true on success.

Definition at line 988 of file ByteBuffer.cpp.

◆ ReadUUID()

bool cByteBuffer::ReadUUID ( cUUID a_Value)

Definition at line 573 of file ByteBuffer.cpp.

◆ ReadVarInt()

template<typename T >
bool cByteBuffer::ReadVarInt ( T &  a_Value)
inline

Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short, char, Byte, double, ...)

Definition at line 88 of file ByteBuffer.h.

◆ ReadVarInt32()

bool cByteBuffer::ReadVarInt32 ( UInt32 a_Value)

Definition at line 414 of file ByteBuffer.cpp.

◆ ReadVarInt64()

bool cByteBuffer::ReadVarInt64 ( UInt64 a_Value)

Definition at line 436 of file ByteBuffer.cpp.

◆ ReadVarUTF8String()

bool cByteBuffer::ReadVarUTF8String ( AString a_Value)

Definition at line 458 of file ByteBuffer.cpp.

◆ ReadXYZPosition64() [1/2]

bool cByteBuffer::ReadXYZPosition64 ( int &  a_BlockX,
int &  a_BlockY,
int &  a_BlockZ 
)

Definition at line 505 of file ByteBuffer.cpp.

◆ ReadXYZPosition64() [2/2]

bool cByteBuffer::ReadXYZPosition64 ( Vector3i a_Position)

Definition at line 530 of file ByteBuffer.cpp.

◆ ReadXZYPosition64() [1/2]

bool cByteBuffer::ReadXZYPosition64 ( int &  a_BlockX,
int &  a_BlockY,
int &  a_BlockZ 
)

Definition at line 539 of file ByteBuffer.cpp.

◆ ReadXZYPosition64() [2/2]

bool cByteBuffer::ReadXZYPosition64 ( Vector3i a_Position)

Definition at line 564 of file ByteBuffer.cpp.

◆ ResetRead()

void cByteBuffer::ResetRead ( void  )

Restarts next reading operation at the start of the ringbuffer.

Definition at line 1024 of file ByteBuffer.cpp.

◆ SkipRead()

bool cByteBuffer::SkipRead ( size_t  a_Count)

Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer.

Definition at line 961 of file ByteBuffer.cpp.

◆ Write()

bool cByteBuffer::Write ( const void *  a_Bytes,
size_t  a_Count 
)

Writes the bytes specified to the ringbuffer.

Returns true if successful, false if not

Definition at line 111 of file ByteBuffer.cpp.

◆ WriteBEDouble()

bool cByteBuffer::WriteBEDouble ( double  a_Value)

Definition at line 720 of file ByteBuffer.cpp.

◆ WriteBEFloat()

bool cByteBuffer::WriteBEFloat ( float  a_Value)

Definition at line 707 of file ByteBuffer.cpp.

◆ WriteBEInt16()

bool cByteBuffer::WriteBEInt16 ( Int16  a_Value)

Definition at line 627 of file ByteBuffer.cpp.

◆ WriteBEInt32()

bool cByteBuffer::WriteBEInt32 ( Int32  a_Value)

Definition at line 655 of file ByteBuffer.cpp.

◆ WriteBEInt64()

bool cByteBuffer::WriteBEInt64 ( Int64  a_Value)

Definition at line 681 of file ByteBuffer.cpp.

◆ WriteBEInt8() [1/2]

bool cByteBuffer::WriteBEInt8 ( Int8  a_Value)

Definition at line 591 of file ByteBuffer.cpp.

◆ WriteBEInt8() [2/2]

bool cByteBuffer::WriteBEInt8 ( std::byte  a_Value)

Definition at line 603 of file ByteBuffer.cpp.

◆ WriteBEUInt16()

bool cByteBuffer::WriteBEUInt16 ( UInt16  a_Value)

Definition at line 642 of file ByteBuffer.cpp.

◆ WriteBEUInt32()

bool cByteBuffer::WriteBEUInt32 ( UInt32  a_Value)

Definition at line 668 of file ByteBuffer.cpp.

◆ WriteBEUInt64()

bool cByteBuffer::WriteBEUInt64 ( UInt64  a_Value)

Definition at line 694 of file ByteBuffer.cpp.

◆ WriteBEUInt8()

bool cByteBuffer::WriteBEUInt8 ( UInt8  a_Value)

Definition at line 615 of file ByteBuffer.cpp.

◆ WriteBool()

bool cByteBuffer::WriteBool ( bool  a_Value)

Definition at line 733 of file ByteBuffer.cpp.

◆ WriteBuf() [1/2]

bool cByteBuffer::WriteBuf ( const void *  a_Buffer,
size_t  a_Count 
)

Writes a_Count bytes into a_Buffer; returns true if successful.

Definition at line 869 of file ByteBuffer.cpp.

◆ WriteBuf() [2/2]

bool cByteBuffer::WriteBuf ( size_t  a_Count,
unsigned char  a_Value 
)

Writes a_Count bytes into a_Buffer; returns true if successful.

Definition at line 899 of file ByteBuffer.cpp.

◆ WriteLEInt32()

bool cByteBuffer::WriteLEInt32 ( Int32  a_Value)

◆ WriteVarInt32()

bool cByteBuffer::WriteVarInt32 ( UInt32  a_Value)

Definition at line 745 of file ByteBuffer.cpp.

◆ WriteVarInt64()

bool cByteBuffer::WriteVarInt64 ( UInt64  a_Value)

Definition at line 767 of file ByteBuffer.cpp.

◆ WriteVarUTF8String()

bool cByteBuffer::WriteVarUTF8String ( const AString a_Value)

Definition at line 789 of file ByteBuffer.cpp.

◆ WriteXYZPosition64()

bool cByteBuffer::WriteXYZPosition64 ( Int32  a_BlockX,
Int32  a_BlockY,
Int32  a_BlockZ 
)

Definition at line 806 of file ByteBuffer.cpp.

◆ WriteXZYPosition64()

bool cByteBuffer::WriteXZYPosition64 ( Int32  a_BlockX,
Int32  a_BlockY,
Int32  a_BlockZ 
)

Definition at line 821 of file ByteBuffer.cpp.

Member Data Documentation

◆ m_Buffer

std::byte* cByteBuffer::m_Buffer
protected

Definition at line 157 of file ByteBuffer.h.

◆ m_BufferSize

size_t cByteBuffer::m_BufferSize
protected

Definition at line 158 of file ByteBuffer.h.

◆ m_DataStart

size_t cByteBuffer::m_DataStart
protected

Definition at line 160 of file ByteBuffer.h.

◆ m_ReadPos

size_t cByteBuffer::m_ReadPos
protected

Definition at line 162 of file ByteBuffer.h.

◆ m_ThreadID

std::thread::id cByteBuffer::m_ThreadID
mutableprotected

The ID of the thread currently accessing the object.

Used for checking that only one thread accesses the object at a time, via cSingleThreadAccessChecker.

Definition at line 167 of file ByteBuffer.h.

◆ m_WritePos

size_t cByteBuffer::m_WritePos
protected

Definition at line 161 of file ByteBuffer.h.


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