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 
35  cByteBuffer(size_t a_BufferSize);
36  ~cByteBuffer();
37 
39  bool Write(const void * a_Bytes, size_t a_Count);
40 
42  size_t GetFreeSpace(void) const;
43 
45  size_t GetUsedSpace(void) const;
46 
48  size_t GetReadableSpace(void) const;
49 
51  size_t GetDataStart(void) const { return m_DataStart; }
52 
54  static bool CanBEInt8Represent(int a_Value);
55 
57  static bool CanBEInt16Represent(int a_Value);
58 
60  bool CanReadBytes(size_t a_Count) const;
61 
63  bool CanWriteBytes(size_t a_Count) const;
64 
65  // Read the specified datatype and advance the read pointer; return true if successfully read:
66  bool ReadBEInt8 (Int8 & a_Value);
67  bool ReadBEInt16 (Int16 & a_Value);
68  bool ReadBEInt32 (Int32 & a_Value);
69  bool ReadBEInt64 (Int64 & a_Value);
70  bool ReadBEUInt8 (UInt8 & a_Value);
71  bool ReadBEUInt16 (UInt16 & a_Value);
72  bool ReadBEUInt32 (UInt32 & a_Value);
73  bool ReadBEUInt64 (UInt64 & a_Value);
74  bool ReadBEFloat (float & a_Value);
75  bool ReadBEDouble (double & a_Value);
76  bool ReadBool (bool & a_Value);
77  bool ReadVarInt32 (UInt32 & a_Value);
78  bool ReadVarInt64 (UInt64 & a_Value);
79  bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
80  bool ReadLEInt (int & a_Value);
81  bool ReadXYZPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
82  bool ReadXYZPosition64 (Vector3i & a_Position);
83  bool ReadXZYPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
84  bool ReadXZYPosition64 (Vector3i & a_Position);
85  bool ReadUUID (cUUID & a_Value);
86 
88  template <typename T> bool ReadVarInt(T & a_Value)
89  {
90  UInt64 v;
91  bool res = ReadVarInt64(v);
92  if (res)
93  {
94  a_Value = static_cast<T>(v);
95  }
96  return res;
97  }
98 
99  // Write the specified datatype; return true if successfully written
100  bool WriteBEInt8 (Int8 a_Value);
101  bool WriteBEInt8 (std::byte a_Value);
102  bool WriteBEInt16 (Int16 a_Value);
103  bool WriteBEInt32 (Int32 a_Value);
104  bool WriteBEInt64 (Int64 a_Value);
105  bool WriteBEUInt8 (UInt8 a_Value);
106  bool WriteBEUInt16 (UInt16 a_Value);
107  bool WriteBEUInt32 (UInt32 a_Value);
108  bool WriteBEUInt64 (UInt64 a_Value);
109  bool WriteBEFloat (float a_Value);
110  bool WriteBEDouble (double a_Value);
111  bool WriteBool (bool a_Value);
112  bool WriteVarInt32 (UInt32 a_Value);
113  bool WriteVarInt64 (UInt64 a_Value);
114  bool WriteVarUTF8String (const AString & a_Value); // string length as VarInt, then string as UTF-8
115  bool WriteLEInt32 (Int32 a_Value);
116  bool WriteXYZPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);
117  bool WriteXZYPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);
118 
120  bool ReadBuf(void * a_Buffer, size_t a_Count);
121 
123  bool WriteBuf(const void * a_Buffer, size_t a_Count);
124 
126  bool WriteBuf(size_t a_Count, unsigned char a_Value);
127 
129  bool ReadSome(ContiguousByteBuffer & a_String, size_t a_Count);
130 
132  bool SkipRead(size_t a_Count);
133 
135  void ReadAll(ContiguousByteBuffer & a_Data);
136 
138  bool ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes);
139 
141  void CommitRead(void);
142 
144  void ResetRead(void);
145 
147  void ReadAgain(ContiguousByteBuffer & a_Out);
148 
150  void CheckValid(void) const;
151 
153  static size_t GetVarIntSize(UInt32 a_Value);
154 
155 protected:
156 
157  std::byte * m_Buffer;
158  size_t m_BufferSize; // Total size of the ringbuffer
159 
160  size_t m_DataStart; // Where the data starts in the ringbuffer
161  size_t m_WritePos; // Where the data ends in the ringbuffer
162  size_t m_ReadPos; // Where the next read will start in the ringbuffer
163 
164  #ifndef NDEBUG
167  mutable std::thread::id m_ThreadID;
168  #endif
169 
171  void AdvanceReadPos(size_t a_Count);
172 } ;
unsigned int UInt32
Definition: Globals.h:157
signed long long Int64
Definition: Globals.h:151
signed short Int16
Definition: Globals.h:153
signed char Int8
Definition: Globals.h:154
signed int Int32
Definition: Globals.h:152
unsigned char UInt8
Definition: Globals.h:159
unsigned long long UInt64
Definition: Globals.h:156
unsigned short UInt16
Definition: Globals.h:158
std::basic_string< std::byte > ContiguousByteBuffer
Definition: Globals.h:375
std::string AString
Definition: StringUtils.h:11
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
Definition: ByteBuffer.h:32
size_t m_ReadPos
Definition: ByteBuffer.h:162
bool ReadBEUInt16(UInt16 &a_Value)
Definition: ByteBuffer.cpp:299
size_t m_WritePos
Definition: ByteBuffer.h:161
size_t GetUsedSpace(void) const
Returns the number of bytes that are currently in the ringbuffer.
Definition: ByteBuffer.cpp:185
bool WriteXYZPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:806
bool SkipRead(size_t a_Count)
Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer.
Definition: ByteBuffer.cpp:961
bool ReadBEUInt8(UInt8 &a_Value)
Definition: ByteBuffer.cpp:270
bool CanReadBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for reading.
Definition: ByteBuffer.cpp:235
bool WriteVarInt64(UInt64 a_Value)
Definition: ByteBuffer.cpp:767
static bool CanBEInt16Represent(int a_Value)
Returns if the given value can fit in a protocol big-endian 16 bit integer.
Definition: ByteBuffer.cpp:226
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
bool ReadBEUInt64(UInt64 &a_Value)
Definition: ByteBuffer.cpp:357
size_t m_BufferSize
Definition: ByteBuffer.h:158
bool ReadBuf(void *a_Buffer, size_t a_Count)
Reads a_Count bytes into a_Buffer; returns true if successful.
Definition: ByteBuffer.cpp:836
bool ReadXZYPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
Definition: ByteBuffer.cpp:539
void ReadAll(ContiguousByteBuffer &a_Data)
Reads all available data into a_Data.
Definition: ByteBuffer.cpp:977
bool ReadVarInt64(UInt64 &a_Value)
Definition: ByteBuffer.cpp:436
void CheckValid(void) const
Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs.
bool WriteLEInt32(Int32 a_Value)
bool WriteXZYPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:821
bool ReadUUID(cUUID &a_Value)
Definition: ByteBuffer.cpp:573
bool ReadBEFloat(float &a_Value)
Definition: ByteBuffer.cpp:371
void AdvanceReadPos(size_t a_Count)
Advances the m_ReadPos by a_Count bytes.
std::byte * m_Buffer
Definition: ByteBuffer.h:157
bool ReadLEInt(int &a_Value)
Definition: ByteBuffer.cpp:486
bool ReadBEInt64(Int64 &a_Value)
Definition: ByteBuffer.cpp:343
bool WriteBEUInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:668
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:988
size_t GetFreeSpace(void) const
Returns the number of bytes that can be successfully written to the ringbuffer.
Definition: ByteBuffer.cpp:164
bool WriteBEFloat(float a_Value)
Definition: ByteBuffer.cpp:707
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
bool WriteBEInt32(Int32 a_Value)
Definition: ByteBuffer.cpp:655
bool ReadBool(bool &a_Value)
Definition: ByteBuffer.cpp:399
bool WriteBEInt64(Int64 a_Value)
Definition: ByteBuffer.cpp:681
bool ReadVarInt32(UInt32 &a_Value)
Definition: ByteBuffer.cpp:414
bool WriteBEInt16(Int16 a_Value)
Definition: ByteBuffer.cpp:627
bool WriteBEUInt64(UInt64 a_Value)
Definition: ByteBuffer.cpp:694
bool CanWriteBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for writing.
Definition: ByteBuffer.cpp:246
bool ReadVarInt(T &a_Value)
Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short,...
Definition: ByteBuffer.h:88
bool WriteBuf(const void *a_Buffer, size_t a_Count)
Writes a_Count bytes into a_Buffer; returns true if successful.
Definition: ByteBuffer.cpp:869
std::thread::id m_ThreadID
The ID of the thread currently accessing the object.
Definition: ByteBuffer.h:167
bool ReadXYZPosition64(int &a_BlockX, int &a_BlockY, int &a_BlockZ)
Definition: ByteBuffer.cpp:505
bool WriteBool(bool a_Value)
Definition: ByteBuffer.cpp:733
void ReadAgain(ContiguousByteBuffer &a_Out)
Re-reads the data that has been read since the last commit to the current readpos.
size_t GetDataStart(void) const
Returns the current data start index.
Definition: ByteBuffer.h:51
bool WriteVarUTF8String(const AString &a_Value)
Definition: ByteBuffer.cpp:789
bool WriteBEInt8(Int8 a_Value)
Definition: ByteBuffer.cpp:591
bool WriteVarInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:745
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)
Definition: ByteBuffer.cpp:257
bool ReadVarUTF8String(AString &a_Value)
Definition: ByteBuffer.cpp:458
bool ReadSome(ContiguousByteBuffer &a_String, size_t a_Count)
Reads a_Count bytes into a_String; returns true if successful.
Definition: ByteBuffer.cpp:927
bool ReadBEUInt32(UInt32 &a_Value)
Definition: ByteBuffer.cpp:329
bool ReadBEInt32(Int32 &a_Value)
Definition: ByteBuffer.cpp:313
size_t m_DataStart
Definition: ByteBuffer.h:160
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.
Definition: ByteBuffer.cpp:111
static bool CanBEInt8Represent(int a_Value)
Returns if the given value can fit in a protocol big-endian 8 bit integer.
Definition: ByteBuffer.cpp:217
bool WriteBEUInt16(UInt16 a_Value)
Definition: ByteBuffer.cpp:642
bool WriteBEUInt8(UInt8 a_Value)
Definition: ByteBuffer.cpp:615
cByteBuffer(size_t a_BufferSize)
Definition: ByteBuffer.cpp:85
bool ReadBEDouble(double &a_Value)
Definition: ByteBuffer.cpp:385
bool WriteBEDouble(double a_Value)
Definition: ByteBuffer.cpp:720
bool ReadBEInt16(Int16 &a_Value)
Definition: ByteBuffer.cpp:283
Definition: UUID.h:11