|
Cuberite
A lightweight, fast and extensible game server for Minecraft
|
Go to the documentation of this file.
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
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;
273 memcpy(&a_Value, &val, 2);
287 a_Value = ntohs(a_Value);
303 memcpy(&a_Value, &val, 4);
317 a_Value = ntohl(a_Value);
388 a_Value = (Value != 0);
407 Value = Value | ((
static_cast<UInt32>(b & 0x7f)) << Shift);
409 }
while ((b & 0x80) != 0);
429 Value = Value | ((
static_cast<UInt64>(b & 0x7f)) << Shift);
431 }
while ((b & 0x80) != 0);
451 LOGWARNING(
"%s: String too large: %u (%u KiB)", __FUNCTION__, Size, Size / 1024);
454 if (!
ReadSome(Buffer,
static_cast<size_t>(Size)))
460 a_Value = {
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size() };
477 a_Value = ((a_Value >> 24) & 0xff) | ((a_Value >> 16) & 0xff00) | ((a_Value >> 8) & 0xff0000) | (a_Value & 0xff000000);
497 UInt32 BlockXRaw = (Value >> 38) & 0x03ffffff;
498 UInt32 BlockYRaw = (Value >> 26) & 0x0fff;
499 UInt32 BlockZRaw = (Value & 0x03ffffff);
502 a_BlockX = ((BlockXRaw & 0x02000000) == 0) ?
static_cast<int>(BlockXRaw) : -(0x04000000 -
static_cast<int>(BlockXRaw));
503 a_BlockY = ((BlockYRaw & 0x0800) == 0) ?
static_cast<int>(BlockYRaw) : -(0x01000 -
static_cast<int>(BlockYRaw));
504 a_BlockZ = ((BlockZRaw & 0x02000000) == 0) ?
static_cast<int>(BlockZRaw) : -(0x04000000 -
static_cast<int>(BlockZRaw));
531 UInt32 BlockXRaw = (Value >> 38) & 0x03ffffff;
532 UInt32 BlockZRaw = (Value >> 12) & 0x03ffffff;
533 UInt32 BlockYRaw = (Value & 0x0fff);
536 a_BlockX = ((BlockXRaw & 0x02000000) == 0) ?
static_cast<int>(BlockXRaw) : (
static_cast<int>(BlockXRaw) - 0x04000000);
537 a_BlockY = ((BlockYRaw & 0x0800) == 0) ?
static_cast<int>(BlockYRaw) : (
static_cast<int>(BlockYRaw) - 0x01000);
538 a_BlockZ = ((BlockZRaw & 0x02000000) == 0) ?
static_cast<int>(BlockZRaw) : (
static_cast<int>(BlockZRaw) - 0x04000000);
559 std::array<Byte, 16> UUIDBuf;
560 if (!
ReadBuf(UUIDBuf.data(), UUIDBuf.size()))
615 memcpy(&val, &a_Value, 2);
629 a_Value = htons(a_Value);
719 UInt8 val = a_Value ? 1 : 0;
720 return Write(&val, 1);
737 b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
738 a_Value = a_Value >> 7;
740 }
while (a_Value > 0);
759 b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
760 a_Value = a_Value >> 7;
762 }
while (a_Value > 0);
781 return WriteBuf(a_Value.data(), a_Value.size());
793 ((
static_cast<UInt64>(a_BlockX) & 0x3FFFFFF) << 38) |
794 ((
static_cast<UInt64>(a_BlockY) & 0xFFF) << 26) |
795 (
static_cast<UInt64>(a_BlockZ) & 0x3FFFFFF)
808 ((
static_cast<UInt64>(a_BlockX) & 0x3FFFFFF) << 38) |
809 ((
static_cast<UInt64>(a_BlockZ) & 0x3FFFFFF) << 12) |
810 (
static_cast<UInt64>(a_BlockY) & 0xFFF)
823 char * Dst =
static_cast<char *
>(a_Buffer);
826 if (BytesToEndOfBuffer <= a_Count)
829 if (BytesToEndOfBuffer > 0)
832 Dst += BytesToEndOfBuffer;
833 a_Count -= BytesToEndOfBuffer;
856 const char * Src =
static_cast<const char *
>(a_Buffer);
859 if (BytesToEndOfBuffer <= a_Count)
863 Src += BytesToEndOfBuffer;
864 a_Count -= BytesToEndOfBuffer;
888 if (BytesToEndOfBuffer <= a_Count)
892 a_Count -= BytesToEndOfBuffer;
915 a_String.reserve(a_Count);
918 if (BytesToEndOfBuffer <= a_Count)
921 if (BytesToEndOfBuffer > 0)
924 ASSERT(a_Count >= BytesToEndOfBuffer);
925 a_Count -= BytesToEndOfBuffer;
980 while (a_NumBytes != 0)
982 size_t num = (a_NumBytes >
sizeof(buf)) ?
sizeof(buf) : a_NumBytes;
985 ASSERT(a_NumBytes >= num);
1073 }
while (a_Value != 0);
bool ReadBEUInt16(UInt16 &a_Value)
void ReadAll(ContiguousByteBuffer &a_Data)
Reads all available data into a_Data.
void AdvanceReadPos(size_t a_Count)
Advances the m_ReadPos by a_Count bytes.
bool ReadUUID(cUUID &a_Value)
std::basic_string< std::byte > ContiguousByteBuffer
void ReadAgain(ContiguousByteBuffer &a_Out)
Re-reads the data that has been read since the last commit to the current readpos.
bool WriteBEFloat(float a_Value)
bool ReadVarInt(T &a_Value)
Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short,...
size_t GetReadableSpace(void) const
Returns the number of bytes that are currently available for reading (may be less than UsedSpace due ...
bool WriteBuf(const void *a_Buffer, size_t a_Count)
Writes a_Count bytes into a_Buffer; returns true if successful.
bool ReadVarUTF8String(AString &a_Value)
double NetworkToHostDouble8(const void *a_Value)
bool ReadBEInt8(Int8 &a_Value)
bool WriteVarInt32(UInt32 a_Value)
bool CanReadBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for reading.
bool ReadToByteBuffer(cByteBuffer &a_Dst, size_t a_NumBytes)
Reads the specified number of bytes and writes it into the destinatio bytebuffer.
Int64 NetworkToHostLong8(const void *a_Value)
bool CanWriteBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for writing.
bool ReadBEUInt32(UInt32 &a_Value)
bool ReadBool(bool &a_Value)
bool ReadXYZPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
bool ReadVarInt64(UInt64 &a_Value)
bool WriteBEInt64(Int64 a_Value)
bool ReadBEInt16(Int16 &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 CheckValid(void) const
Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs.
bool ReadBEUInt64(UInt64 &a_Value)
bool WriteBEInt8(Int8 a_Value)
bool WriteVarInt64(UInt64 a_Value)
bool WriteBEUInt64(UInt64 a_Value)
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
bool ReadBuf(void *a_Buffer, size_t a_Count)
Reads a_Count bytes into a_Buffer; returns true if successful.
bool ReadBEDouble(double &a_Value)
bool WriteBEDouble(double a_Value)
bool ReadBEInt32(Int32 &a_Value)
static size_t GetVarIntSize(UInt32 a_Value)
Gets the number of bytes that are needed to represent the given VarInt.
bool ReadBEInt64(Int64 &a_Value)
size_t GetFreeSpace(void) const
Returns the number of bytes that can be successfully written to the ringbuffer.
void ResetRead(void)
Restarts next reading operation at the start of the ringbuffer.
size_t GetUsedSpace(void) const
Returns the number of bytes that are currently in the ringbuffer.
bool ReadBEFloat(float &a_Value)
bool ReadVarInt32(UInt32 &a_Value)
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
UInt64 HostToNetwork8(const void *a_Value)
bool ReadSome(ContiguousByteBuffer &a_String, size_t a_Count)
Reads a_Count bytes into a_String; returns true if successful.
bool WriteXYZPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
bool ReadBEUInt8(UInt8 &a_Value)
void LOGWARNING(std::string_view a_Format, const Args &... args)
bool WriteBEInt16(Int16 a_Value)
void FromRaw(const std::array< Byte, 16 > &a_Raw)
Assigns from raw memory representation, respecting UUID variant.
cByteBuffer(size_t a_BufferSize)
bool WriteBool(bool a_Value)
bool SkipRead(size_t a_Count)
Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer.
bool WriteBEInt32(Int32 a_Value)
bool WriteBEUInt16(UInt16 a_Value)
UInt32 HostToNetwork4(const void *a_Value)
bool WriteBEUInt32(UInt32 a_Value)
bool WriteXZYPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
UInt64 NetworkToHostULong8(const void *a_Value)
bool WriteVarUTF8String(const AString &a_Value)
float NetworkToHostFloat4(const void *a_Value)
unsigned long long UInt64
bool ReadXZYPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
bool ReadLEInt(int &a_Value)
bool WriteBEUInt8(UInt8 a_Value)