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 
29 {
30 public:
33  cPacketizer(cProtocol & a_Protocol, cProtocol::ePacketType a_PacketType) :
34  m_Protocol(a_Protocol),
35  m_Out(a_Protocol.m_OutPacketBuffer),
36  m_Lock(a_Protocol.m_CSPacket),
37  m_PacketType(a_PacketType) // Used for logging purposes
38  {
40  }
41 
43  ~cPacketizer();
44 
45  inline void WriteBool(bool a_Value)
46  {
47  VERIFY(m_Out.WriteBool(a_Value));
48  }
49 
50  inline void WriteBEUInt8(UInt8 a_Value)
51  {
52  VERIFY(m_Out.WriteBEUInt8(a_Value));
53  }
54 
55 
56  inline void WriteBEInt8(Int8 a_Value)
57  {
58  VERIFY(m_Out.WriteBEInt8(a_Value));
59  }
60 
61 
62  inline void WriteBEInt16(Int16 a_Value)
63  {
64  VERIFY(m_Out.WriteBEInt16(a_Value));
65  }
66 
67 
68  inline void WriteBEUInt16(UInt16 a_Value)
69  {
70  VERIFY(m_Out.WriteBEUInt16(a_Value));
71  }
72 
73 
74  inline void WriteBEInt32(Int32 a_Value)
75  {
76  VERIFY(m_Out.WriteBEInt32(a_Value));
77  }
78 
79 
80  inline void WriteBEUInt32(UInt32 a_Value)
81  {
82  VERIFY(m_Out.WriteBEUInt32(a_Value));
83  }
84 
85 
86  inline void WriteBEInt64(Int64 a_Value)
87  {
88  VERIFY(m_Out.WriteBEInt64(a_Value));
89  }
90 
91 
92  inline void WriteBEUInt64(UInt64 a_Value)
93  {
94  VERIFY(m_Out.WriteBEUInt64(a_Value));
95  }
96 
97 
98  inline void WriteBEFloat(float a_Value)
99  {
100  VERIFY(m_Out.WriteBEFloat(a_Value));
101  }
102 
103 
104  inline void WriteBEDouble(double a_Value)
105  {
106  VERIFY(m_Out.WriteBEDouble(a_Value));
107  }
108 
109 
110  inline void WriteVarInt32(UInt32 a_Value)
111  {
112  VERIFY(m_Out.WriteVarInt32(a_Value));
113  }
114 
115 
116  inline void WriteString(const AString & a_Value)
117  {
118  VERIFY(m_Out.WriteVarUTF8String(a_Value));
119  }
120 
121 
122  inline void WriteBuf(const char * a_Data, size_t a_Size)
123  {
124  VERIFY(m_Out.Write(a_Data, a_Size));
125  }
126 
127 
129  inline void WritePosition64(int a_BlockX, int a_BlockY, int a_BlockZ)
130  {
131  VERIFY(m_Out.WritePosition64(a_BlockX, a_BlockY, a_BlockZ));
132  }
133 
135  void WriteByteAngle(double a_Angle);
136 
138  void WriteFPInt(double a_Value);
139 
141  void WriteUUID(const cUUID & a_UUID);
142 
144 
147  static AString PacketTypeToStr(cProtocol::ePacketType a_PacketType);
148 
149 protected:
152 
155 
158 
162 } ;
163 
164 
165 
166 
cProtocol::ePacketType GetPacketType() const
Definition: Packetizer.h:143
bool WriteVarInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:660
void WriteBEInt8(Int8 a_Value)
Definition: Packetizer.h:56
void WriteFPInt(double a_Value)
Writes the double value as a 27:5 fixed-point integer.
Definition: Packetizer.cpp:35
bool WriteBEDouble(double a_Value)
Definition: ByteBuffer.cpp:635
#define VERIFY(x)
Definition: Globals.h:339
signed char Int8
Definition: Globals.h:110
void WriteUUID(const cUUID &a_UUID)
Writes the specified UUID as a 128-bit BigEndian integer.
Definition: Packetizer.cpp:44
signed short Int16
Definition: Globals.h:109
bool WriteVarUTF8String(const AString &a_Value)
Definition: ByteBuffer.cpp:704
void WriteBuf(const char *a_Data, size_t a_Size)
Definition: Packetizer.h:122
static AString PacketTypeToStr(cProtocol::ePacketType a_PacketType)
Returns the human-readable representation of the packet type.
Definition: Packetizer.cpp:56
void WriteBEInt16(Int16 a_Value)
Definition: Packetizer.h:62
void WriteBEUInt8(UInt8 a_Value)
Definition: Packetizer.h:50
void WriteString(const AString &a_Value)
Definition: Packetizer.h:116
cProtocol & m_Protocol
The protocol instance in which the packet is being constructed.
Definition: Packetizer.h:151
bool WriteBEInt16(Int16 a_Value)
Definition: ByteBuffer.cpp:542
void WriteBEInt32(Int32 a_Value)
Definition: Packetizer.h:74
bool WritePosition64(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ)
Definition: ByteBuffer.cpp:721
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
Definition: ByteBuffer.cpp:111
~cPacketizer()
Sends the packet via the contained protocol's SendPacket() function.
Definition: Packetizer.cpp:17
Definition: UUID.h:10
bool WriteBEUInt64(UInt64 a_Value)
Definition: ByteBuffer.cpp:609
void WriteBEUInt32(UInt32 a_Value)
Definition: Packetizer.h:80
cByteBuffer & m_Out
The protocol's buffer for the constructed packet data.
Definition: Packetizer.h:154
void WriteByteAngle(double a_Angle)
Writes the specified angle using a single byte.
Definition: Packetizer.cpp:26
unsigned long long UInt64
Definition: Globals.h:112
void WriteBEUInt16(UInt16 a_Value)
Definition: Packetizer.h:68
cCSLock m_Lock
The RAII lock preventing multithreaded access to the protocol buffer while constructing the packet...
Definition: Packetizer.h:157
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
Definition: ByteBuffer.h:29
void WriteBEDouble(double a_Value)
Definition: Packetizer.h:104
bool WriteBEInt64(Int64 a_Value)
Definition: ByteBuffer.cpp:596
bool WriteBEUInt8(UInt8 a_Value)
Definition: ByteBuffer.cpp:530
unsigned short UInt16
Definition: Globals.h:114
unsigned char UInt8
Definition: Globals.h:115
std::string AString
Definition: StringUtils.h:13
bool WriteBEInt8(Int8 a_Value)
Definition: ByteBuffer.cpp:518
cPacketizer(cProtocol &a_Protocol, cProtocol::ePacketType a_PacketType)
Starts serializing a new packet into the protocol's m_OutPacketBuffer.
Definition: Packetizer.h:33
bool WriteBEFloat(float a_Value)
Definition: ByteBuffer.cpp:622
Composes an individual packet in the protocol's m_OutPacketBuffer; sends it just before being destruc...
Definition: Packetizer.h:28
void WritePosition64(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:129
void WriteVarInt32(UInt32 a_Value)
Definition: Packetizer.h:110
bool WriteBEInt32(Int32 a_Value)
Definition: ByteBuffer.cpp:570
signed int Int32
Definition: Globals.h:108
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
void WriteBEFloat(float a_Value)
Definition: Packetizer.h:98
unsigned int UInt32
Definition: Globals.h:113
virtual UInt32 GetPacketID(ePacketType a_Packet)=0
Returns the protocol-specific packet ID given the protocol-agnostic packet enum.
bool WriteBEUInt32(UInt32 a_Value)
Definition: ByteBuffer.cpp:583
void WriteBool(bool a_Value)
Definition: Packetizer.h:45
bool WriteBool(bool a_Value)
Definition: ByteBuffer.cpp:648
void WriteBEUInt64(UInt64 a_Value)
Definition: Packetizer.h:92
ePacketType
Logical types of outgoing packets.
Definition: Protocol.h:68
signed long long Int64
Definition: Globals.h:107
void WriteBEInt64(Int64 a_Value)
Definition: Packetizer.h:86
cProtocol::ePacketType m_PacketType
Type of the contained packet.
Definition: Packetizer.h:161
bool WriteBEUInt16(UInt16 a_Value)
Definition: ByteBuffer.cpp:557