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 ReadXZYPosition64 (int &a_BlockX, int &a_BlockY, int &a_BlockZ)
 
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 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 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
 
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 989 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 213 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 224 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 1004 of file ByteBuffer.cpp.

◆ CommitRead()

void cByteBuffer::CommitRead ( void  )

Removes the bytes that have been read from the ringbuffer.

Definition at line 945 of file ByteBuffer.cpp.

◆ GetDataStart()

size_t cByteBuffer::GetDataStart ( void  ) const
inline

Returns the current data start index.

For debugging purposes.

Definition at line 50 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 160 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 194 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 181 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 1014 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 967 of file ByteBuffer.cpp.

◆ ReadAll()

void cByteBuffer::ReadAll ( ContiguousByteBuffer a_Data)

Reads all available data into a_Data.

Definition at line 909 of file ByteBuffer.cpp.

◆ ReadBEDouble()

bool cByteBuffer::ReadBEDouble ( double &  a_Value)

Definition at line 363 of file ByteBuffer.cpp.

◆ ReadBEFloat()

bool cByteBuffer::ReadBEFloat ( float &  a_Value)

Definition at line 349 of file ByteBuffer.cpp.

◆ ReadBEInt16()

bool cByteBuffer::ReadBEInt16 ( Int16 a_Value)

Definition at line 261 of file ByteBuffer.cpp.

◆ ReadBEInt32()

bool cByteBuffer::ReadBEInt32 ( Int32 a_Value)

Definition at line 291 of file ByteBuffer.cpp.

◆ ReadBEInt64()

bool cByteBuffer::ReadBEInt64 ( Int64 a_Value)

Definition at line 321 of file ByteBuffer.cpp.

◆ ReadBEInt8()

bool cByteBuffer::ReadBEInt8 ( Int8 a_Value)

Definition at line 235 of file ByteBuffer.cpp.

◆ ReadBEUInt16()

bool cByteBuffer::ReadBEUInt16 ( UInt16 a_Value)

Definition at line 277 of file ByteBuffer.cpp.

◆ ReadBEUInt32()

bool cByteBuffer::ReadBEUInt32 ( UInt32 a_Value)

Definition at line 307 of file ByteBuffer.cpp.

◆ ReadBEUInt64()

bool cByteBuffer::ReadBEUInt64 ( UInt64 a_Value)

Definition at line 335 of file ByteBuffer.cpp.

◆ ReadBEUInt8()

bool cByteBuffer::ReadBEUInt8 ( UInt8 a_Value)

Definition at line 248 of file ByteBuffer.cpp.

◆ ReadBool()

bool cByteBuffer::ReadBool ( bool &  a_Value)

Definition at line 377 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 796 of file ByteBuffer.cpp.

◆ ReadLEInt()

bool cByteBuffer::ReadLEInt ( int &  a_Value)

Definition at line 464 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 859 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 920 of file ByteBuffer.cpp.

◆ ReadUUID()

bool cByteBuffer::ReadUUID ( cUUID a_Value)

Definition at line 533 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 79 of file ByteBuffer.h.

◆ ReadVarInt32()

bool cByteBuffer::ReadVarInt32 ( UInt32 a_Value)

Definition at line 392 of file ByteBuffer.cpp.

◆ ReadVarInt64()

bool cByteBuffer::ReadVarInt64 ( UInt64 a_Value)

Definition at line 414 of file ByteBuffer.cpp.

◆ ReadVarUTF8String()

bool cByteBuffer::ReadVarUTF8String ( AString a_Value)

Definition at line 436 of file ByteBuffer.cpp.

◆ ReadXYZPosition64()

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

Definition at line 483 of file ByteBuffer.cpp.

◆ ReadXZYPosition64()

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

Definition at line 508 of file ByteBuffer.cpp.

◆ ResetRead()

void cByteBuffer::ResetRead ( void  )

Restarts next reading operation at the start of the ringbuffer.

Definition at line 956 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 893 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 680 of file ByteBuffer.cpp.

◆ WriteBEFloat()

bool cByteBuffer::WriteBEFloat ( float  a_Value)

Definition at line 667 of file ByteBuffer.cpp.

◆ WriteBEInt16()

bool cByteBuffer::WriteBEInt16 ( Int16  a_Value)

Definition at line 587 of file ByteBuffer.cpp.

◆ WriteBEInt32()

bool cByteBuffer::WriteBEInt32 ( Int32  a_Value)

Definition at line 615 of file ByteBuffer.cpp.

◆ WriteBEInt64()

bool cByteBuffer::WriteBEInt64 ( Int64  a_Value)

Definition at line 641 of file ByteBuffer.cpp.

◆ WriteBEInt8() [1/2]

bool cByteBuffer::WriteBEInt8 ( Int8  a_Value)

Definition at line 551 of file ByteBuffer.cpp.

◆ WriteBEInt8() [2/2]

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

Definition at line 563 of file ByteBuffer.cpp.

◆ WriteBEUInt16()

bool cByteBuffer::WriteBEUInt16 ( UInt16  a_Value)

Definition at line 602 of file ByteBuffer.cpp.

◆ WriteBEUInt32()

bool cByteBuffer::WriteBEUInt32 ( UInt32  a_Value)

Definition at line 628 of file ByteBuffer.cpp.

◆ WriteBEUInt64()

bool cByteBuffer::WriteBEUInt64 ( UInt64  a_Value)

Definition at line 654 of file ByteBuffer.cpp.

◆ WriteBEUInt8()

bool cByteBuffer::WriteBEUInt8 ( UInt8  a_Value)

Definition at line 575 of file ByteBuffer.cpp.

◆ WriteBool()

bool cByteBuffer::WriteBool ( bool  a_Value)

Definition at line 693 of file ByteBuffer.cpp.

◆ WriteBuf()

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 829 of file ByteBuffer.cpp.

◆ WriteLEInt32()

bool cByteBuffer::WriteLEInt32 ( Int32  a_Value)

◆ WriteVarInt32()

bool cByteBuffer::WriteVarInt32 ( UInt32  a_Value)

Definition at line 705 of file ByteBuffer.cpp.

◆ WriteVarInt64()

bool cByteBuffer::WriteVarInt64 ( UInt64  a_Value)

Definition at line 727 of file ByteBuffer.cpp.

◆ WriteVarUTF8String()

bool cByteBuffer::WriteVarUTF8String ( const AString a_Value)

Definition at line 749 of file ByteBuffer.cpp.

◆ WriteXYZPosition64()

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

Definition at line 766 of file ByteBuffer.cpp.

◆ WriteXZYPosition64()

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

Definition at line 781 of file ByteBuffer.cpp.

Member Data Documentation

◆ m_Buffer

std::byte* cByteBuffer::m_Buffer
protected

Definition at line 145 of file ByteBuffer.h.

◆ m_BufferSize

size_t cByteBuffer::m_BufferSize
protected

Definition at line 146 of file ByteBuffer.h.

◆ m_DataStart

size_t cByteBuffer::m_DataStart
protected

Definition at line 148 of file ByteBuffer.h.

◆ m_ReadPos

size_t cByteBuffer::m_ReadPos
protected

Definition at line 150 of file ByteBuffer.h.

◆ m_WritePos

size_t cByteBuffer::m_WritePos
protected

Definition at line 149 of file ByteBuffer.h.


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