27 #define MAX_STRING_SIZE (512 KiB) 29 #define NEEDBYTES(Num) if (!CanReadBytes(Num)) return false // Check if at least Num bytes can be read from the buffer, return false if not 30 #define PUTBYTES(Num) if (!CanWriteBytes(Num)) return false // Check if at least Num bytes can be written to the buffer, return false if not 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 char[a_BufferSize + 1]),
87 m_BufferSize(a_BufferSize + 1),
121 size_t WrittenBytes = 0;
123 if (CurFreeSpace < a_Count)
129 const char * Bytes =
static_cast<const char *
>(a_Bytes);
130 if (TillEnd <= a_Count)
135 memcpy(
m_Buffer + m_WritePos, Bytes, TillEnd);
138 WrittenBytes = TillEnd;
146 memcpy(
m_Buffer + m_WritePos, Bytes, a_Count);
147 m_WritePos += a_Count;
148 WrittenBytes += a_Count;
269 memcpy(&a_Value, &val, 2);
283 a_Value = ntohs(a_Value);
299 memcpy(&a_Value, &val, 4);
313 a_Value = ntohl(a_Value);
384 a_Value = (Value != 0);
403 Value = Value | ((
static_cast<UInt32>(b & 0x7f)) << Shift);
405 }
while ((b & 0x80) != 0);
425 Value = Value | ((
static_cast<UInt64>(b & 0x7f)) << Shift);
427 }
while ((b & 0x80) != 0);
447 LOGWARNING(
"%s: String too large: %u (%u KiB)", __FUNCTION__, Size, Size / 1024);
449 return ReadString(a_Value, static_cast<size_t>(Size));
465 a_Value = ((a_Value >> 24) & 0xff) | ((a_Value >> 16) & 0xff00) | ((a_Value >> 8) & 0xff0000) | (a_Value & 0xff000000);
485 UInt32 BlockXRaw = (Value >> 38) & 0x03ffffff;
486 UInt32 BlockYRaw = (Value >> 26) & 0x0fff;
487 UInt32 BlockZRaw = (Value & 0x03ffffff);
490 a_BlockX = ((BlockXRaw & 0x02000000) == 0) ?
static_cast<int>(BlockXRaw) : -(0x04000000 - static_cast<int>(BlockXRaw));
491 a_BlockY = ((BlockYRaw & 0x0800) == 0) ?
static_cast<int>(BlockYRaw) : -(0x0800 - static_cast<int>(BlockYRaw));
492 a_BlockZ = ((BlockZRaw & 0x02000000) == 0) ?
static_cast<int>(BlockZRaw) : -(0x04000000 - static_cast<int>(BlockZRaw));
504 std::array<Byte, 16> UUIDBuf;
505 if (!
ReadBuf(UUIDBuf.data(), UUIDBuf.size()))
548 memcpy(&val, &a_Value, 2);
562 a_Value = htons(a_Value);
652 UInt8 val = a_Value ? 1 : 0;
653 return Write(&val, 1);
670 b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
671 a_Value = a_Value >> 7;
673 }
while (a_Value > 0);
692 b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
693 a_Value = a_Value >> 7;
695 }
while (a_Value > 0);
709 bool res =
WriteVarInt32(static_cast<UInt32>(a_Value.size()));
714 return WriteBuf(a_Value.data(), a_Value.size());
726 (static_cast<Int64>(a_BlockX & 0x3FFFFFF) << 38) |
727 (static_cast<Int64>(a_BlockY & 0xFFF) << 26) |
728 (static_cast<Int64>(a_BlockZ & 0x3FFFFFF))
741 char * Dst =
static_cast<char *
>(a_Buffer);
744 if (BytesToEndOfBuffer <= a_Count)
747 if (BytesToEndOfBuffer > 0)
749 memcpy(Dst,
m_Buffer + m_ReadPos, BytesToEndOfBuffer);
750 Dst += BytesToEndOfBuffer;
751 a_Count -= BytesToEndOfBuffer;
759 memcpy(Dst,
m_Buffer + m_ReadPos, a_Count);
760 m_ReadPos += a_Count;
774 const char * Src =
static_cast<const char *
>(a_Buffer);
777 if (BytesToEndOfBuffer <= a_Count)
780 memcpy(
m_Buffer + m_WritePos, Src, BytesToEndOfBuffer);
781 Src += BytesToEndOfBuffer;
782 a_Count -= BytesToEndOfBuffer;
789 memcpy(
m_Buffer + m_WritePos, Src, a_Count);
790 m_WritePos += a_Count;
805 a_String.reserve(a_Count);
808 if (BytesToEndOfBuffer <= a_Count)
811 if (BytesToEndOfBuffer > 0)
813 a_String.assign(
m_Buffer + m_ReadPos, BytesToEndOfBuffer);
814 ASSERT(a_Count >= BytesToEndOfBuffer);
815 a_Count -= BytesToEndOfBuffer;
823 a_String.append(
m_Buffer + m_ReadPos, a_Count);
824 m_ReadPos += a_Count;
870 while (a_NumBytes != 0)
872 size_t num = (a_NumBytes >
sizeof(buf)) ?
sizeof(buf) : a_NumBytes;
875 ASSERT(a_NumBytes >= num);
963 }
while (a_Value != 0);
Int64 NetworkToHostLong8(const void *a_Value)
UInt64 HostToNetwork8(const void *a_Value)
bool WriteVarInt32(UInt32 a_Value)
void ReadAll(AString &a_Data)
Reads all available data into a_Data.
void AdvanceReadPos(size_t a_Count)
Advances the m_ReadPos by a_Count bytes.
size_t GetReadableSpace(void) const
Returns the number of bytes that are currently available for reading (may be less than UsedSpace due ...
bool ReadBEDouble(double &a_Value)
cByteBuffer(size_t a_BufferSize)
bool WriteBEDouble(double a_Value)
bool ReadVarUTF8String(AString &a_Value)
float NetworkToHostFloat4(const void *a_Value)
bool WriteVarUTF8String(const AString &a_Value)
bool ReadString(AString &a_String, size_t a_Count)
Reads a_Count bytes into a_String; returns true if successful.
bool ReadUUID(cUUID &a_Value)
bool ReadBEInt8(Int8 &a_Value)
UInt64 NetworkToHostULong8(const void *a_Value)
double NetworkToHostDouble8(const void *a_Value)
bool WriteBEInt16(Int16 a_Value)
bool WritePosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
bool ReadBEUInt16(UInt16 &a_Value)
bool ReadBEUInt64(UInt64 &a_Value)
bool ReadBEUInt8(UInt8 &a_Value)
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
static size_t GetVarIntSize(UInt32 a_Value)
Gets the number of bytes that are needed to represent the given VarInt.
bool ReadLEInt(int &a_Value)
bool ReadBEUInt32(UInt32 &a_Value)
bool WriteVarInt64(UInt64 a_Value)
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
bool WriteBEUInt64(UInt64 a_Value)
bool ReadBool(bool &a_Value)
bool ReadToByteBuffer(cByteBuffer &a_Dst, size_t a_NumBytes)
Reads the specified number of bytes and writes it into the destinatio bytebuffer. ...
unsigned long long UInt64
bool CanReadBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for reading.
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
void ResetRead(void)
Restarts next reading operation at the start of the ringbuffer.
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
bool ReadVarInt32(UInt32 &a_Value)
bool WriteBEInt64(Int64 a_Value)
bool ReadBEInt16(Int16 &a_Value)
bool SkipRead(size_t a_Count)
Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer.
void ReadAgain(AString &a_Out)
Re-reads the data that has been read since the last commit to the current readpos.
bool ReadVarInt(T &a_Value)
Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short...
bool WriteBEUInt8(UInt8 a_Value)
#define MAX_STRING_SIZE
When defined, each access to a cByteBuffer object is checked whether it's done in the same thread...
void FromRaw(const std::array< Byte, 16 > &a_Raw)
Assigns from raw memory representation, respecting UUID variant.
size_t GetUsedSpace(void) const
Returns the number of bytes that are currently in the ringbuffer.
bool WriteBEInt8(Int8 a_Value)
bool WriteBEFloat(float a_Value)
bool WriteBEInt32(Int32 a_Value)
size_t GetFreeSpace(void) const
Returns the number of bytes that can be successfully written to the ringbuffer.
bool WriteBuf(const void *a_Buffer, size_t a_Count)
Writes a_Count bytes into a_Buffer; returns true if successful.
bool ReadBuf(void *a_Buffer, size_t a_Count)
Reads a_Count bytes into a_Buffer; returns true if successful.
bool ReadBEInt32(Int32 &a_Value)
bool ReadBEInt64(Int64 &a_Value)
bool WriteBEUInt32(UInt32 a_Value)
bool ReadBEFloat(float &a_Value)
void CheckValid(void) const
Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs...
bool WriteBool(bool a_Value)
bool ReadPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
bool ReadVarInt64(UInt64 &a_Value)
UInt32 HostToNetwork4(const void *a_Value)
bool CanWriteBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for writing.
bool WriteBEUInt16(UInt16 a_Value)