Cuberite
A lightweight, fast and extensible game server for Minecraft
|
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 |
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.
cByteBuffer::cByteBuffer | ( | size_t | a_BufferSize | ) |
Definition at line 85 of file ByteBuffer.cpp.
cByteBuffer::~cByteBuffer | ( | ) |
Definition at line 100 of file ByteBuffer.cpp.
|
protected |
Advances the m_ReadPos by a_Count bytes.
Definition at line 1057 of file ByteBuffer.cpp.
|
static |
Returns if the given value can fit in a protocol big-endian 16 bit integer.
Definition at line 226 of file ByteBuffer.cpp.
|
static |
Returns if the given value can fit in a protocol big-endian 8 bit integer.
Definition at line 217 of file ByteBuffer.cpp.
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.
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.
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.
void cByteBuffer::CommitRead | ( | void | ) |
Removes the bytes that have been read from the ringbuffer.
Definition at line 1013 of file ByteBuffer.cpp.
|
inline |
Returns the current data start index.
For debugging purposes.
Definition at line 51 of file ByteBuffer.h.
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.
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.
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.
|
static |
Gets the number of bytes that are needed to represent the given VarInt.
Definition at line 1082 of file ByteBuffer.cpp.
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.
void cByteBuffer::ReadAll | ( | ContiguousByteBuffer & | a_Data | ) |
Reads all available data into a_Data.
Definition at line 977 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEDouble | ( | double & | a_Value | ) |
Definition at line 385 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEFloat | ( | float & | a_Value | ) |
Definition at line 371 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEInt16 | ( | Int16 & | a_Value | ) |
Definition at line 283 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEInt32 | ( | Int32 & | a_Value | ) |
Definition at line 313 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEInt64 | ( | Int64 & | a_Value | ) |
Definition at line 343 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEInt8 | ( | Int8 & | a_Value | ) |
Definition at line 257 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEUInt16 | ( | UInt16 & | a_Value | ) |
Definition at line 299 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEUInt32 | ( | UInt32 & | a_Value | ) |
Definition at line 329 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEUInt64 | ( | UInt64 & | a_Value | ) |
Definition at line 357 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBEUInt8 | ( | UInt8 & | a_Value | ) |
Definition at line 270 of file ByteBuffer.cpp.
bool cByteBuffer::ReadBool | ( | bool & | a_Value | ) |
Definition at line 399 of file ByteBuffer.cpp.
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.
bool cByteBuffer::ReadLEInt | ( | int & | a_Value | ) |
Definition at line 486 of file ByteBuffer.cpp.
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.
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.
bool cByteBuffer::ReadUUID | ( | cUUID & | a_Value | ) |
Definition at line 573 of file ByteBuffer.cpp.
|
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.
bool cByteBuffer::ReadVarInt32 | ( | UInt32 & | a_Value | ) |
Definition at line 414 of file ByteBuffer.cpp.
bool cByteBuffer::ReadVarInt64 | ( | UInt64 & | a_Value | ) |
Definition at line 436 of file ByteBuffer.cpp.
bool cByteBuffer::ReadVarUTF8String | ( | AString & | a_Value | ) |
Definition at line 458 of file ByteBuffer.cpp.
bool cByteBuffer::ReadXYZPosition64 | ( | int & | a_BlockX, |
int & | a_BlockY, | ||
int & | a_BlockZ | ||
) |
Definition at line 505 of file ByteBuffer.cpp.
bool cByteBuffer::ReadXYZPosition64 | ( | Vector3i & | a_Position | ) |
Definition at line 530 of file ByteBuffer.cpp.
bool cByteBuffer::ReadXZYPosition64 | ( | int & | a_BlockX, |
int & | a_BlockY, | ||
int & | a_BlockZ | ||
) |
Definition at line 539 of file ByteBuffer.cpp.
bool cByteBuffer::ReadXZYPosition64 | ( | Vector3i & | a_Position | ) |
Definition at line 564 of file ByteBuffer.cpp.
void cByteBuffer::ResetRead | ( | void | ) |
Restarts next reading operation at the start of the ringbuffer.
Definition at line 1024 of file ByteBuffer.cpp.
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.
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.
bool cByteBuffer::WriteBEDouble | ( | double | a_Value | ) |
Definition at line 720 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEFloat | ( | float | a_Value | ) |
Definition at line 707 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEInt16 | ( | Int16 | a_Value | ) |
Definition at line 627 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEInt32 | ( | Int32 | a_Value | ) |
Definition at line 655 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEInt64 | ( | Int64 | a_Value | ) |
Definition at line 681 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEInt8 | ( | Int8 | a_Value | ) |
Definition at line 591 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEInt8 | ( | std::byte | a_Value | ) |
Definition at line 603 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEUInt16 | ( | UInt16 | a_Value | ) |
Definition at line 642 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEUInt32 | ( | UInt32 | a_Value | ) |
Definition at line 668 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEUInt64 | ( | UInt64 | a_Value | ) |
Definition at line 694 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBEUInt8 | ( | UInt8 | a_Value | ) |
Definition at line 615 of file ByteBuffer.cpp.
bool cByteBuffer::WriteBool | ( | bool | a_Value | ) |
Definition at line 733 of file ByteBuffer.cpp.
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.
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.
bool cByteBuffer::WriteLEInt32 | ( | Int32 | a_Value | ) |
bool cByteBuffer::WriteVarInt32 | ( | UInt32 | a_Value | ) |
Definition at line 745 of file ByteBuffer.cpp.
bool cByteBuffer::WriteVarInt64 | ( | UInt64 | a_Value | ) |
Definition at line 767 of file ByteBuffer.cpp.
bool cByteBuffer::WriteVarUTF8String | ( | const AString & | a_Value | ) |
Definition at line 789 of file ByteBuffer.cpp.
Definition at line 806 of file ByteBuffer.cpp.
Definition at line 821 of file ByteBuffer.cpp.
|
protected |
Definition at line 157 of file ByteBuffer.h.
|
protected |
Definition at line 158 of file ByteBuffer.h.
|
protected |
Definition at line 160 of file ByteBuffer.h.
|
protected |
Definition at line 162 of file ByteBuffer.h.
|
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.
|
protected |
Definition at line 161 of file ByteBuffer.h.