27 #define MAX_STRING_SIZE (512 KiB)
29 #define NEEDBYTES(Num) if (!CanReadBytes(Num)) return false
30 #define PUTBYTES(Num) if (!CanWriteBytes(Num)) return false
36 #ifdef DEBUG_SINGLE_THREAD_ACCESS
41 class cSingleThreadAccessChecker
44 cSingleThreadAccessChecker(std::thread::id * a_ThreadID) :
45 m_ThreadID(a_ThreadID)
48 (*a_ThreadID == std::this_thread::get_id()) ||
49 (*a_ThreadID == m_EmptyThreadID)
53 *m_ThreadID = std::this_thread::get_id();
56 ~cSingleThreadAccessChecker()
59 *m_ThreadID = std::thread::id();
64 std::thread::id * m_ThreadID;
67 static std::thread::id m_EmptyThreadID;
70 std::thread::id cSingleThreadAccessChecker::m_EmptyThreadID;
72 #define CHECK_THREAD cSingleThreadAccessChecker Checker(&m_ThreadID);
86 m_Buffer(new
std::byte[a_BufferSize + 1]),
87 m_BufferSize(a_BufferSize + 1),
120 size_t WrittenBytes = 0;
123 if (CurFreeSpace < a_Count)
129 const char * Bytes =
static_cast<const char *
>(a_Bytes);
130 if (TillEnd <= a_Count)
139 WrittenBytes = TillEnd;
151 WrittenBytes += a_Count;
219 return (-128 <= a_Value) && (a_Value <= 127);
228 return (-32768 <= a_Value) && (a_Value <= 32767);
291 memcpy(&a_Value, &val, 2);
305 a_Value = ntohs(a_Value);
321 memcpy(&a_Value, &val, 4);
335 a_Value = ntohl(a_Value);
406 a_Value = (Value != 0);
425 Value = Value | ((
static_cast<UInt32>(b & 0x7f)) << Shift);
427 }
while ((b & 0x80) != 0);
447 Value = Value | ((
static_cast<UInt64>(b & 0x7f)) << Shift);
449 }
while ((b & 0x80) != 0);
469 LOGWARNING(
"%s: String too large: %u (%u KiB)", __FUNCTION__, Size, Size / 1024);
472 if (!
ReadSome(Buffer,
static_cast<size_t>(Size)))
478 a_Value = {
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size() };
495 a_Value = ((a_Value >> 24) & 0xff) | ((a_Value >> 16) & 0xff00) | ((a_Value >> 8) & 0xff0000) | (a_Value & 0xff000000);
515 UInt32 BlockXRaw = (Value >> 38) & 0x03ffffff;
516 UInt32 BlockYRaw = (Value >> 26) & 0x0fff;
517 UInt32 BlockZRaw = (Value & 0x03ffffff);
520 a_BlockX = ((BlockXRaw & 0x02000000) == 0) ?
static_cast<int>(BlockXRaw) : -(0x04000000 -
static_cast<int>(BlockXRaw));
521 a_BlockY = ((BlockYRaw & 0x0800) == 0) ?
static_cast<int>(BlockYRaw) : -(0x01000 -
static_cast<int>(BlockYRaw));
522 a_BlockZ = ((BlockZRaw & 0x02000000) == 0) ?
static_cast<int>(BlockZRaw) : -(0x04000000 -
static_cast<int>(BlockZRaw));
549 UInt32 BlockXRaw = (Value >> 38) & 0x03ffffff;
550 UInt32 BlockZRaw = (Value >> 12) & 0x03ffffff;
551 UInt32 BlockYRaw = (Value & 0x0fff);
554 a_BlockX = ((BlockXRaw & 0x02000000) == 0) ?
static_cast<int>(BlockXRaw) : (
static_cast<int>(BlockXRaw) - 0x04000000);
555 a_BlockY = ((BlockYRaw & 0x0800) == 0) ?
static_cast<int>(BlockYRaw) : (
static_cast<int>(BlockYRaw) - 0x01000);
556 a_BlockZ = ((BlockZRaw & 0x02000000) == 0) ?
static_cast<int>(BlockZRaw) : (
static_cast<int>(BlockZRaw) - 0x04000000);
577 std::array<Byte, 16> UUIDBuf;
578 if (!
ReadBuf(UUIDBuf.data(), UUIDBuf.size()))
633 memcpy(&val, &a_Value, 2);
647 a_Value = htons(a_Value);
737 UInt8 val = a_Value ? 1 : 0;
738 return Write(&val, 1);
755 b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
756 a_Value = a_Value >> 7;
758 }
while (a_Value > 0);
777 b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
778 a_Value = a_Value >> 7;
780 }
while (a_Value > 0);
799 return WriteBuf(a_Value.data(), a_Value.size());
811 ((
static_cast<UInt64>(a_BlockX) & 0x3FFFFFF) << 38) |
812 ((
static_cast<UInt64>(a_BlockY) & 0xFFF) << 26) |
813 (
static_cast<UInt64>(a_BlockZ) & 0x3FFFFFF)
826 ((
static_cast<UInt64>(a_BlockX) & 0x3FFFFFF) << 38) |
827 ((
static_cast<UInt64>(a_BlockZ) & 0x3FFFFFF) << 12) |
828 (
static_cast<UInt64>(a_BlockY) & 0xFFF)
841 char * Dst =
static_cast<char *
>(a_Buffer);
844 if (BytesToEndOfBuffer <= a_Count)
847 if (BytesToEndOfBuffer > 0)
850 Dst += BytesToEndOfBuffer;
851 a_Count -= BytesToEndOfBuffer;
874 const char * Src =
static_cast<const char *
>(a_Buffer);
877 if (BytesToEndOfBuffer <= a_Count)
881 Src += BytesToEndOfBuffer;
882 a_Count -= BytesToEndOfBuffer;
906 if (BytesToEndOfBuffer <= a_Count)
910 a_Count -= BytesToEndOfBuffer;
933 a_String.reserve(a_Count);
936 if (BytesToEndOfBuffer <= a_Count)
939 if (BytesToEndOfBuffer > 0)
942 ASSERT(a_Count >= BytesToEndOfBuffer);
943 a_Count -= BytesToEndOfBuffer;
998 while (a_NumBytes != 0)
1000 size_t num = (a_NumBytes >
sizeof(buf)) ?
sizeof(buf) : a_NumBytes;
1003 ASSERT(a_NumBytes >= num);
1091 }
while (a_Value != 0);
#define MAX_STRING_SIZE
When defined, each access to a cByteBuffer object is checked whether it's done in the same thread.
UInt32 HostToNetwork4(const void *a_Value)
Int64 NetworkToHostLong8(const void *a_Value)
double NetworkToHostDouble8(const void *a_Value)
UInt64 NetworkToHostULong8(const void *a_Value)
float NetworkToHostFloat4(const void *a_Value)
UInt64 HostToNetwork8(const void *a_Value)
unsigned long long UInt64
std::basic_string< std::byte > ContiguousByteBuffer
void LOGWARNING(std::string_view a_Format, const Args &... args)
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
bool ReadBEUInt16(UInt16 &a_Value)
size_t GetUsedSpace(void) const
Returns the number of bytes that are currently in the ringbuffer.
bool WriteXYZPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
bool SkipRead(size_t a_Count)
Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer.
bool ReadBEUInt8(UInt8 &a_Value)
bool CanReadBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for reading.
bool WriteVarInt64(UInt64 a_Value)
static bool CanBEInt16Represent(int a_Value)
Returns if the given value can fit in a protocol big-endian 16 bit integer.
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
bool ReadBEUInt64(UInt64 &a_Value)
bool ReadBuf(void *a_Buffer, size_t a_Count)
Reads a_Count bytes into a_Buffer; returns true if successful.
bool ReadXZYPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
void ReadAll(ContiguousByteBuffer &a_Data)
Reads all available data into a_Data.
bool ReadVarInt64(UInt64 &a_Value)
void CheckValid(void) const
Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs.
bool WriteXZYPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
bool ReadUUID(cUUID &a_Value)
bool ReadBEFloat(float &a_Value)
void AdvanceReadPos(size_t a_Count)
Advances the m_ReadPos by a_Count bytes.
bool ReadLEInt(int &a_Value)
bool ReadBEInt64(Int64 &a_Value)
bool WriteBEUInt32(UInt32 a_Value)
bool ReadToByteBuffer(cByteBuffer &a_Dst, size_t a_NumBytes)
Reads the specified number of bytes and writes it into the destinatio bytebuffer.
size_t GetFreeSpace(void) const
Returns the number of bytes that can be successfully written to the ringbuffer.
bool WriteBEFloat(float a_Value)
size_t GetReadableSpace(void) const
Returns the number of bytes that are currently available for reading (may be less than UsedSpace due ...
bool WriteBEInt32(Int32 a_Value)
bool ReadBool(bool &a_Value)
bool WriteBEInt64(Int64 a_Value)
bool ReadVarInt32(UInt32 &a_Value)
bool WriteBEInt16(Int16 a_Value)
bool WriteBEUInt64(UInt64 a_Value)
bool CanWriteBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for writing.
bool ReadVarInt(T &a_Value)
Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short,...
bool WriteBuf(const void *a_Buffer, size_t a_Count)
Writes a_Count bytes into a_Buffer; returns true if successful.
bool ReadXYZPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
bool WriteBool(bool a_Value)
void ReadAgain(ContiguousByteBuffer &a_Out)
Re-reads the data that has been read since the last commit to the current readpos.
bool WriteVarUTF8String(const AString &a_Value)
bool WriteBEInt8(Int8 a_Value)
bool WriteVarInt32(UInt32 a_Value)
static size_t GetVarIntSize(UInt32 a_Value)
Gets the number of bytes that are needed to represent the given VarInt.
bool ReadBEInt8(Int8 &a_Value)
bool ReadVarUTF8String(AString &a_Value)
bool ReadSome(ContiguousByteBuffer &a_String, size_t a_Count)
Reads a_Count bytes into a_String; returns true if successful.
bool ReadBEUInt32(UInt32 &a_Value)
bool ReadBEInt32(Int32 &a_Value)
void ResetRead(void)
Restarts next reading operation at the start of the ringbuffer.
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
static bool CanBEInt8Represent(int a_Value)
Returns if the given value can fit in a protocol big-endian 8 bit integer.
bool WriteBEUInt16(UInt16 a_Value)
bool WriteBEUInt8(UInt8 a_Value)
cByteBuffer(size_t a_BufferSize)
bool ReadBEDouble(double &a_Value)
bool WriteBEDouble(double a_Value)
bool ReadBEInt16(Int16 &a_Value)
void FromRaw(const std::array< Byte, 16 > &a_Raw)
Assigns from raw memory representation, respecting UUID variant.