Cuberite
A lightweight, fast and extensible game server for Minecraft
ByteBuffer.h
Go to the documentation of this file.
1 
2 // ByteStream.h
3 
4 // Interfaces to the cByteBuffer class representing a ringbuffer of bytes
5 
6 
7 
8 
9 
10 #pragma once
11 
12 
13 
14 
15 
16 // fwd:
17 class cUUID;
18 
19 
32 {
33 public:
34  cByteBuffer(size_t a_BufferSize);
35  ~cByteBuffer();
36 
38  bool Write(const void * a_Bytes, size_t a_Count);
39 
41  size_t GetFreeSpace(void) const;
42 
44  size_t GetUsedSpace(void) const;
45 
47  size_t GetReadableSpace(void) const;
48 
50  size_t GetDataStart(void) const { return m_DataStart; }
51 
53  bool CanReadBytes(size_t a_Count) const;
54 
56  bool CanWriteBytes(size_t a_Count) const;
57 
58  // Read the specified datatype and advance the read pointer; return true if successfully read:
59  bool ReadBEInt8 (Int8 & a_Value);
60  bool ReadBEInt16 (Int16 & a_Value);
61  bool ReadBEInt32 (Int32 & a_Value);
62  bool ReadBEInt64 (Int64 & a_Value);
63  bool ReadBEUInt8 (UInt8 & a_Value);
64  bool ReadBEUInt16 (UInt16 & a_Value);
65  bool ReadBEUInt32 (UInt32 & a_Value);
66  bool ReadBEUInt64 (UInt64 & a_Value);
67  bool ReadBEFloat (float & a_Value);
68  bool ReadBEDouble (double & a_Value);
69  bool ReadBool (bool & a_Value);
70  bool ReadVarInt32 (UInt32 & a_Value);
71  bool ReadVarInt64 (UInt64 & a_Value);
72  bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
73  bool ReadLEInt (int & a_Value);
74  bool ReadXYZPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
75  bool ReadXYZPosition64 (Vector3i & a_Position);
76  bool ReadXZYPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
77  bool ReadXZYPosition64 (Vector3i & a_Position);
78  bool ReadUUID (cUUID & a_Value);
79 
81  template <typename T> bool ReadVarInt(T & a_Value)
82  {
83  UInt64 v;
84  bool res = ReadVarInt64(v);
85  if (res)
86  {
87  a_Value = static_cast<T>(v);
88  }
89  return res;
90  }
91 
92  // Write the specified datatype; return true if successfully written
93  bool WriteBEInt8 (Int8 a_Value);
94  bool WriteBEInt8 (std::byte a_Value);
95  bool WriteBEInt16 (Int16 a_Value);
96  bool WriteBEInt32 (Int32 a_Value);
97  bool WriteBEInt64 (Int64 a_Value);
98  bool WriteBEUInt8 (UInt8 a_Value);
99  bool WriteBEUInt16 (UInt16 a_Value);
100  bool WriteBEUInt32 (UInt32 a_Value);
101  bool WriteBEUInt64 (UInt64 a_Value);
102  bool WriteBEFloat (float a_Value);
103  bool WriteBEDouble (double a_Value);
104  bool WriteBool (bool a_Value);
105  bool WriteVarInt32 (UInt32 a_Value);
106  bool WriteVarInt64 (UInt64 a_Value);
107  bool WriteVarUTF8String (const AString & a_Value); // string length as VarInt, then string as UTF-8
108  bool WriteLEInt32 (Int32 a_Value);
109  bool WriteXYZPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);
110  bool WriteXZYPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);
111 
113  bool ReadBuf(void * a_Buffer, size_t a_Count);
114 
116  bool WriteBuf(const void * a_Buffer, size_t a_Count);
117 
119  bool WriteBuf(size_t a_Count, unsigned char a_Value);
120 
122  bool ReadSome(ContiguousByteBuffer & a_String, size_t a_Count);
123 
125  bool SkipRead(size_t a_Count);
126 
128  void ReadAll(ContiguousByteBuffer & a_Data);
129 
131  bool ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes);
132 
134  void CommitRead(void);
135 
137  void ResetRead(void);
138 
140  void ReadAgain(ContiguousByteBuffer & a_Out);
141 
143  void CheckValid(void) const;
144 
146  static size_t GetVarIntSize(UInt32 a_Value);
147 
148 protected:
149 
150  std::byte * m_Buffer;
151  size_t m_BufferSize; // Total size of the ringbuffer
152 
153  size_t m_DataStart; // Where the data starts in the ringbuffer
154  size_t m_WritePos; // Where the data ends in the ringbuffer
155  size_t m_ReadPos; // Where the next read will start in the ringbuffer
156 
157  #ifndef NDEBUG
158 
160  mutable std::thread::id m_ThreadID;
161  #endif
162 
164  void AdvanceReadPos(size_t a_Count);
165 } ;
cByteBuffer::ReadBEUInt16
bool ReadBEUInt16(UInt16 &a_Value)
Definition: ByteBuffer.cpp:281
cByteBuffer::ReadAll
void ReadAll(ContiguousByteBuffer &a_Data)
Reads all available data into a_Data.
Definition: ByteBuffer.cpp:959
cUUID
Definition: UUID.h:10
cByteBuffer::AdvanceReadPos
void AdvanceReadPos(size_t a_Count)
Advances the m_ReadPos by a_Count bytes.
Definition: ByteBuffer.cpp:1039
cByteBuffer::ReadUUID
bool ReadUUID(cUUID &a_Value)
Definition: ByteBuffer.cpp:555
ContiguousByteBuffer
std::basic_string< std::byte > ContiguousByteBuffer
Definition: Globals.h:372
cByteBuffer::ReadAgain
void ReadAgain(ContiguousByteBuffer &a_Out)
Re-reads the data that has been read since the last commit to the current readpos.
Definition: ByteBuffer.cpp:1017
cByteBuffer::WriteBEFloat
bool WriteBEFloat(float a_Value)
Definition: ByteBuffer.cpp:689
cByteBuffer::GetDataStart
size_t GetDataStart(void) const
Returns the current data start index.
Definition: ByteBuffer.h:50
cByteBuffer::m_ReadPos
size_t m_ReadPos
Definition: ByteBuffer.h:155
cByteBuffer::ReadVarInt
bool ReadVarInt(T &a_Value)
Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short,...
Definition: ByteBuffer.h:81
cByteBuffer::m_ThreadID
std::thread::id m_ThreadID
The ID of the thread currently accessing the object.
Definition: ByteBuffer.h:160
cByteBuffer::GetReadableSpace
size_t GetReadableSpace(void) const
Returns the number of bytes that are currently available for reading (may be less than UsedSpace due ...
Definition: ByteBuffer.cpp:198
cByteBuffer::WriteBuf
bool WriteBuf(const void *a_Buffer, size_t a_Count)
Writes a_Count bytes into a_Buffer; returns true if successful.
Definition: ByteBuffer.cpp:851
cByteBuffer::ReadVarUTF8String
bool ReadVarUTF8String(AString &a_Value)
Definition: ByteBuffer.cpp:440
cByteBuffer::ReadBEInt8
bool ReadBEInt8(Int8 &a_Value)
Definition: ByteBuffer.cpp:239
UInt16
unsigned short UInt16
Definition: Globals.h:155
cByteBuffer::WriteVarInt32
bool WriteVarInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:727
cByteBuffer::CanReadBytes
bool CanReadBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for reading.
Definition: ByteBuffer.cpp:217
cByteBuffer::ReadToByteBuffer
bool ReadToByteBuffer(cByteBuffer &a_Dst, size_t a_NumBytes)
Reads the specified number of bytes and writes it into the destinatio bytebuffer.
Definition: ByteBuffer.cpp:970
cByteBuffer::CanWriteBytes
bool CanWriteBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for writing.
Definition: ByteBuffer.cpp:228
UInt32
unsigned int UInt32
Definition: Globals.h:154
cByteBuffer::m_DataStart
size_t m_DataStart
Definition: ByteBuffer.h:153
cByteBuffer::ReadBEUInt32
bool ReadBEUInt32(UInt32 &a_Value)
Definition: ByteBuffer.cpp:311
cByteBuffer::ReadBool
bool ReadBool(bool &a_Value)
Definition: ByteBuffer.cpp:381
cByteBuffer::ReadXYZPosition64
bool ReadXYZPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
Definition: ByteBuffer.cpp:487
cByteBuffer::ReadVarInt64
bool ReadVarInt64(UInt64 &a_Value)
Definition: ByteBuffer.cpp:418
cByteBuffer::WriteBEInt64
bool WriteBEInt64(Int64 a_Value)
Definition: ByteBuffer.cpp:663
cByteBuffer::ReadBEInt16
bool ReadBEInt16(Int16 &a_Value)
Definition: ByteBuffer.cpp:265
cByteBuffer::CheckValid
void CheckValid(void) const
Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs.
Definition: ByteBuffer.cpp:1054
cByteBuffer::ReadBEUInt64
bool ReadBEUInt64(UInt64 &a_Value)
Definition: ByteBuffer.cpp:339
cByteBuffer::WriteBEInt8
bool WriteBEInt8(Int8 a_Value)
Definition: ByteBuffer.cpp:573
cByteBuffer::WriteVarInt64
bool WriteVarInt64(UInt64 a_Value)
Definition: ByteBuffer.cpp:749
cByteBuffer::WriteBEUInt64
bool WriteBEUInt64(UInt64 a_Value)
Definition: ByteBuffer.cpp:676
cByteBuffer
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
Definition: ByteBuffer.h:31
cByteBuffer::ReadBuf
bool ReadBuf(void *a_Buffer, size_t a_Count)
Reads a_Count bytes into a_Buffer; returns true if successful.
Definition: ByteBuffer.cpp:818
cByteBuffer::ReadBEDouble
bool ReadBEDouble(double &a_Value)
Definition: ByteBuffer.cpp:367
cByteBuffer::WriteBEDouble
bool WriteBEDouble(double a_Value)
Definition: ByteBuffer.cpp:702
cByteBuffer::ReadBEInt32
bool ReadBEInt32(Int32 &a_Value)
Definition: ByteBuffer.cpp:295
cByteBuffer::GetVarIntSize
static size_t GetVarIntSize(UInt32 a_Value)
Gets the number of bytes that are needed to represent the given VarInt.
Definition: ByteBuffer.cpp:1064
Int32
signed int Int32
Definition: Globals.h:149
cByteBuffer::ReadBEInt64
bool ReadBEInt64(Int64 &a_Value)
Definition: ByteBuffer.cpp:325
cByteBuffer::GetFreeSpace
size_t GetFreeSpace(void) const
Returns the number of bytes that can be successfully written to the ringbuffer.
Definition: ByteBuffer.cpp:164
cByteBuffer::ResetRead
void ResetRead(void)
Restarts next reading operation at the start of the ringbuffer.
Definition: ByteBuffer.cpp:1006
cByteBuffer::GetUsedSpace
size_t GetUsedSpace(void) const
Returns the number of bytes that are currently in the ringbuffer.
Definition: ByteBuffer.cpp:185
cByteBuffer::ReadBEFloat
bool ReadBEFloat(float &a_Value)
Definition: ByteBuffer.cpp:353
cByteBuffer::ReadVarInt32
bool ReadVarInt32(UInt32 &a_Value)
Definition: ByteBuffer.cpp:396
cByteBuffer::CommitRead
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
Definition: ByteBuffer.cpp:995
cByteBuffer::m_Buffer
std::byte * m_Buffer
Definition: ByteBuffer.h:150
cByteBuffer::ReadSome
bool ReadSome(ContiguousByteBuffer &a_String, size_t a_Count)
Reads a_Count bytes into a_String; returns true if successful.
Definition: ByteBuffer.cpp:909
cByteBuffer::~cByteBuffer
~cByteBuffer()
Definition: ByteBuffer.cpp:100
cByteBuffer::m_BufferSize
size_t m_BufferSize
Definition: ByteBuffer.h:151
cByteBuffer::WriteXYZPosition64
bool WriteXYZPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:788
Int8
signed char Int8
Definition: Globals.h:151
cByteBuffer::ReadBEUInt8
bool ReadBEUInt8(UInt8 &a_Value)
Definition: ByteBuffer.cpp:252
cByteBuffer::WriteBEInt16
bool WriteBEInt16(Int16 a_Value)
Definition: ByteBuffer.cpp:609
Int64
signed long long Int64
Definition: Globals.h:148
cByteBuffer::m_WritePos
size_t m_WritePos
Definition: ByteBuffer.h:154
cByteBuffer::WriteLEInt32
bool WriteLEInt32(Int32 a_Value)
cByteBuffer::cByteBuffer
cByteBuffer(size_t a_BufferSize)
Definition: ByteBuffer.cpp:85
cByteBuffer::WriteBool
bool WriteBool(bool a_Value)
Definition: ByteBuffer.cpp:715
cByteBuffer::SkipRead
bool SkipRead(size_t a_Count)
Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer.
Definition: ByteBuffer.cpp:943
cByteBuffer::WriteBEInt32
bool WriteBEInt32(Int32 a_Value)
Definition: ByteBuffer.cpp:637
cByteBuffer::WriteBEUInt16
bool WriteBEUInt16(UInt16 a_Value)
Definition: ByteBuffer.cpp:624
UInt8
unsigned char UInt8
Definition: Globals.h:156
cByteBuffer::WriteBEUInt32
bool WriteBEUInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:650
cByteBuffer::WriteXZYPosition64
bool WriteXZYPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:803
cByteBuffer::Write
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
Definition: ByteBuffer.cpp:111
cByteBuffer::WriteVarUTF8String
bool WriteVarUTF8String(const AString &a_Value)
Definition: ByteBuffer.cpp:771
AString
std::string AString
Definition: StringUtils.h:11
Vector3
Definition: Vector3.h:10
UInt64
unsigned long long UInt64
Definition: Globals.h:153
Int16
signed short Int16
Definition: Globals.h:150
cByteBuffer::ReadXZYPosition64
bool ReadXZYPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
Definition: ByteBuffer.cpp:521
cByteBuffer::ReadLEInt
bool ReadLEInt(int &a_Value)
Definition: ByteBuffer.cpp:468
cByteBuffer::WriteBEUInt8
bool WriteBEUInt8(UInt8 a_Value)
Definition: ByteBuffer.cpp:597