Cuberite
A lightweight, fast and extensible game server for Minecraft
Packetizer.h
Go to the documentation of this file.
1 
2 // Packetizer.h
3 
4 // Declares the cPacketizer class representing a wrapper for sending a single packet over a protocol.
5 // The class provides auto-locking, serialization and send-on-instance-destroy semantics
6 
7 
8 
9 
10 
11 #pragma once
12 
13 #include "Protocol.h"
14 
15 
16 
17 class cByteBuffer;
18 
19 
20 // fwd:
21 class cUUID;
22 
23 
24 
25 
26 
28 #define HANDLE_READ(ByteBuf, Proc, Type, Var) \
29  Type Var; \
30  do { \
31  if (!ByteBuf.Proc(Var))\
32  {\
33  return;\
34  } \
35  } while (false)
36 
37 
38 
39 
40 
41 #define HANDLE_PACKET_READ(ByteBuf, Proc, Type, Var) \
42  Type Var; \
43  do { \
44  { \
45  if (!ByteBuf.Proc(Var)) \
46  { \
47  ByteBuf.CheckValid(); \
48  return false; \
49  } \
50  ByteBuf.CheckValid(); \
51  } \
52  } while (false)
53 
54 
55 
56 
57 
60 {
61 public:
64  cPacketizer(cProtocol & a_Protocol, cProtocol::ePacketType a_PacketType) :
65  m_Protocol(a_Protocol),
66  m_Out(a_Protocol.m_OutPacketBuffer),
67  m_Lock(a_Protocol.m_CSPacket),
68  m_PacketType(a_PacketType) // Used for logging purposes
69  {
71  }
72 
74  ~cPacketizer();
75 
76  inline void WriteBool(bool a_Value)
77  {
78  VERIFY(m_Out.WriteBool(a_Value));
79  }
80 
81  inline void WriteBEUInt8(UInt8 a_Value)
82  {
83  VERIFY(m_Out.WriteBEUInt8(a_Value));
84  }
85 
86 
87  inline void WriteBEInt8(Int8 a_Value)
88  {
89  VERIFY(m_Out.WriteBEInt8(a_Value));
90  }
91 
92 
93  inline void WriteBEInt16(Int16 a_Value)
94  {
95  VERIFY(m_Out.WriteBEInt16(a_Value));
96  }
97 
98 
99  inline void WriteBEUInt16(UInt16 a_Value)
100  {
101  VERIFY(m_Out.WriteBEUInt16(a_Value));
102  }
103 
104 
105  inline void WriteBEInt32(Int32 a_Value)
106  {
107  VERIFY(m_Out.WriteBEInt32(a_Value));
108  }
109 
110 
111  inline void WriteBEUInt32(UInt32 a_Value)
112  {
113  VERIFY(m_Out.WriteBEUInt32(a_Value));
114  }
115 
116 
117  inline void WriteBEInt64(Int64 a_Value)
118  {
119  VERIFY(m_Out.WriteBEInt64(a_Value));
120  }
121 
122 
123  inline void WriteBEUInt64(UInt64 a_Value)
124  {
125  VERIFY(m_Out.WriteBEUInt64(a_Value));
126  }
127 
128 
129  inline void WriteBEFloat(float a_Value)
130  {
131  VERIFY(m_Out.WriteBEFloat(a_Value));
132  }
133 
134 
135  inline void WriteBEDouble(double a_Value)
136  {
137  VERIFY(m_Out.WriteBEDouble(a_Value));
138  }
139 
140 
141  inline void WriteVarInt32(UInt32 a_Value)
142  {
143  VERIFY(m_Out.WriteVarInt32(a_Value));
144  }
145 
146 
147  inline void WriteString(const AString & a_Value)
148  {
149  VERIFY(m_Out.WriteVarUTF8String(a_Value));
150  }
151 
152 
153  inline void WriteBuf(const ContiguousByteBufferView a_Data)
154  {
155  VERIFY(m_Out.Write(a_Data.data(), a_Data.size()));
156  }
157 
158 
161  inline void WriteXYZPosition64(int a_BlockX, int a_BlockY, int a_BlockZ)
162  {
163  VERIFY(m_Out.WriteXYZPosition64(a_BlockX, a_BlockY, a_BlockZ));
164  }
165 
168  inline void WriteXYZPosition64(const Vector3i a_Position)
169  {
170  VERIFY(m_Out.WriteXYZPosition64(a_Position.x, a_Position.y, a_Position.z));
171  }
172 
175  inline void WriteXZYPosition64(int a_BlockX, int a_BlockY, int a_BlockZ)
176  {
177  VERIFY(m_Out.WriteXZYPosition64(a_BlockX, a_BlockY, a_BlockZ));
178  }
179 
182  inline void WriteXZYPosition64(const Vector3i a_Position)
183  {
184  VERIFY(m_Out.WriteXZYPosition64(a_Position.x, a_Position.y, a_Position.z));
185  }
186 
188  void WriteByteAngle(double a_Angle);
189 
191  void WriteFPInt(double a_Value);
192 
194  void WriteUUID(const cUUID & a_UUID);
195 
197 
200  static AString PacketTypeToStr(cProtocol::ePacketType a_PacketType);
201 
202 protected:
205 
208 
211 
215 } ;
216 
217 
218 
219 
#define VERIFY(x)
Definition: Globals.h:280
unsigned int UInt32
Definition: Globals.h:157
signed long long Int64
Definition: Globals.h:151
signed short Int16
Definition: Globals.h:153
std::basic_string_view< std::byte > ContiguousByteBufferView
Definition: Globals.h:376
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::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
bool WriteXYZPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:806
bool WriteXZYPosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:821
bool WriteBEUInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:668
bool WriteBEFloat(float a_Value)
Definition: ByteBuffer.cpp:707
bool WriteBEInt32(Int32 a_Value)
Definition: ByteBuffer.cpp:655
bool WriteBEInt64(Int64 a_Value)
Definition: ByteBuffer.cpp:681
bool WriteBEInt16(Int16 a_Value)
Definition: ByteBuffer.cpp:627
bool WriteBEUInt64(UInt64 a_Value)
Definition: ByteBuffer.cpp:694
bool WriteBool(bool a_Value)
Definition: ByteBuffer.cpp:733
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
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
Definition: ByteBuffer.cpp:111
bool WriteBEUInt16(UInt16 a_Value)
Definition: ByteBuffer.cpp:642
bool WriteBEUInt8(UInt8 a_Value)
Definition: ByteBuffer.cpp:615
bool WriteBEDouble(double a_Value)
Definition: ByteBuffer.cpp:720
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
Composes an individual packet in the protocol's m_OutPacketBuffer; sends it just before being destruc...
Definition: Packetizer.h:60
cProtocol::ePacketType GetPacketType() const
Definition: Packetizer.h:196
void WriteVarInt32(UInt32 a_Value)
Definition: Packetizer.h:141
void WriteUUID(const cUUID &a_UUID)
Writes the specified UUID as a 128-bit BigEndian integer.
Definition: Packetizer.cpp:44
void WriteBEUInt32(UInt32 a_Value)
Definition: Packetizer.h:111
void WriteBEInt32(Int32 a_Value)
Definition: Packetizer.h:105
void WriteXZYPosition64(int a_BlockX, int a_BlockY, int a_BlockZ)
Writes the specified block position as a single encoded 64-bit BigEndian integer.
Definition: Packetizer.h:175
void WriteBool(bool a_Value)
Definition: Packetizer.h:76
cProtocol::ePacketType m_PacketType
Type of the contained packet.
Definition: Packetizer.h:214
cCSLock m_Lock
The RAII lock preventing multithreaded access to the protocol buffer while constructing the packet.
Definition: Packetizer.h:210
void WriteXZYPosition64(const Vector3i a_Position)
Writes the specified block position as a single encoded 64-bit BigEndian integer.
Definition: Packetizer.h:182
static AString PacketTypeToStr(cProtocol::ePacketType a_PacketType)
Returns the human-readable representation of the packet type.
Definition: Packetizer.cpp:56
cProtocol & m_Protocol
The protocol instance in which the packet is being constructed.
Definition: Packetizer.h:204
void WriteXYZPosition64(int a_BlockX, int a_BlockY, int a_BlockZ)
Writes the specified block position as a single encoded 64-bit BigEndian integer.
Definition: Packetizer.h:161
void WriteBEUInt64(UInt64 a_Value)
Definition: Packetizer.h:123
~cPacketizer()
Sends the packet via the contained protocol's SendPacket() function.
Definition: Packetizer.cpp:17
void WriteBEFloat(float a_Value)
Definition: Packetizer.h:129
void WriteFPInt(double a_Value)
Writes the double value as a 27:5 fixed-point integer.
Definition: Packetizer.cpp:35
void WriteXYZPosition64(const Vector3i a_Position)
Writes the specified block position as a single encoded 64-bit BigEndian integer.
Definition: Packetizer.h:168
void WriteBEInt16(Int16 a_Value)
Definition: Packetizer.h:93
void WriteString(const AString &a_Value)
Definition: Packetizer.h:147
void WriteBEUInt8(UInt8 a_Value)
Definition: Packetizer.h:81
cPacketizer(cProtocol &a_Protocol, cProtocol::ePacketType a_PacketType)
Starts serializing a new packet into the protocol's m_OutPacketBuffer.
Definition: Packetizer.h:64
void WriteBEDouble(double a_Value)
Definition: Packetizer.h:135
void WriteBuf(const ContiguousByteBufferView a_Data)
Definition: Packetizer.h:153
void WriteBEInt64(Int64 a_Value)
Definition: Packetizer.h:117
void WriteBEUInt16(UInt16 a_Value)
Definition: Packetizer.h:99
void WriteByteAngle(double a_Angle)
Writes the specified angle using a single byte.
Definition: Packetizer.cpp:26
cByteBuffer & m_Out
The protocol's buffer for the constructed packet data.
Definition: Packetizer.h:207
void WriteBEInt8(Int8 a_Value)
Definition: Packetizer.h:87
virtual UInt32 GetPacketID(ePacketType a_Packet) const =0
Returns the protocol-specific packet ID given the protocol-agnostic packet enum.
ePacketType
Logical types of outgoing packets.
Definition: Protocol.h:57
Definition: UUID.h:11
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17