Cuberite
A lightweight, fast and extensible game server for Minecraft
Packetizer.cpp
Go to the documentation of this file.
1 
2 // Packetizer.cpp
3 
4 // Implements the cPacketizer class representing a wrapper for sending a single packet over a protocol.
5 
6 #include "Globals.h"
7 #include "Packetizer.h"
8 #include "../UUID.h"
9 
10 
11 
12 
13 
15 // cPacketizer:
16 
18 {
19  m_Protocol.SendPacket(*this);
20 }
21 
22 
23 
24 
25 
26 void cPacketizer::WriteByteAngle(double a_Angle)
27 {
28  WriteBEInt8(static_cast<Int8>(255 * a_Angle / 360));
29 }
30 
31 
32 
33 
34 
35 void cPacketizer::WriteFPInt(double a_Value)
36 {
37  WriteBEInt32(static_cast<Int32>(a_Value * 32));
38 }
39 
40 
41 
42 
43 
44 void cPacketizer::WriteUUID(const cUUID & a_UUID)
45 {
46  for (auto val : a_UUID.ToRaw())
47  {
49  }
50 }
51 
52 
53 
54 
55 
57 {
58  switch (a_PacketType)
59  {
60  case cProtocol::pktAttachEntity: return "pktAttachEntity";
61  case cProtocol::pktBlockAction: return "pktBlockAction";
62  case cProtocol::pktBlockBreakAnim: return "pktBlockBreakAnim";
63  case cProtocol::pktBlockChange: return "pktBlockChange";
64  case cProtocol::pktBlockChanges: return "pktBlockChanges";
65  case cProtocol::pktBossBar: return "pktBossBar";
66  case cProtocol::pktCameraSetTo: return "pktCameraSetTo";
67  case cProtocol::pktChatRaw: return "pktChatRaw";
68  case cProtocol::pktCollectEntity: return "pktCollectEntity";
69  case cProtocol::pktDestroyEntity: return "pktDestroyEntity";
70  case cProtocol::pktDifficulty: return "pktDifficulty";
71  case cProtocol::pktDisconnectDuringLogin: return "pktDisconnectDuringLogin";
72  case cProtocol::pktDisconnectDuringGame: return "pktDisconnectDuringGame";
73  case cProtocol::pktDisplayObjective: return "pktDisplayObjective";
74  case cProtocol::pktEditSign: return "pktEditSign";
75  case cProtocol::pktEncryptionRequest: return "pktEncryptionRequest";
76  case cProtocol::pktEntityAnimation: return "pktEntityAnimation";
77  case cProtocol::pktEntityEffect: return "pktEntityEffect";
78  case cProtocol::pktEntityEquipment: return "pktEntityEquipment";
79  case cProtocol::pktEntityHeadLook: return "pktEntityHeadLook";
80  case cProtocol::pktEntityLook: return "pktEntityLook";
81  case cProtocol::pktEntityMeta: return "pktEntityMeta";
82  case cProtocol::pktEntityProperties: return "pktEntityProperties";
83  case cProtocol::pktEntityRelMove: return "pktEntityRelMove";
84  case cProtocol::pktEntityRelMoveLook: return "pktEntityRelMoveLook";
85  case cProtocol::pktEntityStatus: return "pktEntityStatus";
86  case cProtocol::pktEntityVelocity: return "pktEntityVelocity";
87  case cProtocol::pktExperience: return "pktExperience";
88  case cProtocol::pktExplosion: return "pktExplosion";
89  case cProtocol::pktGameMode: return "pktGameMode";
90  case cProtocol::pktHeldItemChange: return "pktHeldItemChange";
91  case cProtocol::pktHorseWindowOpen: return "pktHorseWindowOpen";
92  case cProtocol::pktInventorySlot: return "pktInventorySlot";
93  case cProtocol::pktJoinGame: return "pktJoinGame";
94  case cProtocol::pktKeepAlive: return "pktKeepAlive";
95  case cProtocol::pktLeashEntity: return "pktLeashEntity";
96  case cProtocol::pktLoginSuccess: return "pktLoginSuccess";
97  case cProtocol::pktMapData: return "pktMapData";
98  case cProtocol::pktParticleEffect: return "pktParticleEffect";
99  case cProtocol::pktPingResponse: return "pktPingResponse";
100  case cProtocol::pktPlayerAbilities: return "pktPlayerAbilities";
101  case cProtocol::pktPlayerList: return "pktPlayerList";
102  case cProtocol::pktPlayerListHeaderFooter: return "pktPlayerListHeaderFooter";
103  case cProtocol::pktPlayerMoveLook: return "pktPlayerMoveLook";
104  case cProtocol::pktPluginMessage: return "pktPluginMessage";
105  case cProtocol::pktRemoveEntityEffect: return "pktRemoveEntityEffect";
106  case cProtocol::pktResourcePack: return "pktResourcePack";
107  case cProtocol::pktRespawn: return "pktRespawn";
108  case cProtocol::pktScoreboardObjective: return "pktScoreboardObjective";
109  case cProtocol::pktSpawnObject: return "pktSpawnObject";
110  case cProtocol::pktSoundEffect: return "pktSoundEffect";
111  case cProtocol::pktSoundParticleEffect: return "pktSoundParticleEffect";
112  case cProtocol::pktSpawnExperienceOrb: return "pktSpawnExperienceOrb";
113  case cProtocol::pktSpawnGlobalEntity: return "pktSpawnGlobalEntity";
114  case cProtocol::pktSpawnMob: return "pktSpawnMob";
115  case cProtocol::pktSpawnOtherPlayer: return "pktSpawnOtherPlayer";
116  case cProtocol::pktSpawnPainting: return "pktSpawnPainting";
117  case cProtocol::pktSpawnPosition: return "pktSpawnPosition";
118  case cProtocol::pktStartCompression: return "pktStartCompression";
119  case cProtocol::pktStatistics: return "pktStatistics";
120  case cProtocol::pktStatusResponse: return "pktStatusResponse";
121  case cProtocol::pktTabCompletionResults: return "pktTabCompletionResults";
122  case cProtocol::pktTeleportEntity: return "pktTeleportEntity";
123  case cProtocol::pktTimeUpdate: return "pktTimeUpdate";
124  case cProtocol::pktTitle: return "pktTitle";
125  case cProtocol::pktUnloadChunk: return "pktUnloadChunk";
126  case cProtocol::pktUnlockRecipe: return "pktUnlockRecipe";
127  case cProtocol::pktUpdateBlockEntity: return "pktUpdateBlockEntity";
128  case cProtocol::pktUpdateHealth: return "pktUpdateHealth";
129  case cProtocol::pktUpdateScore: return "pktUpdateScore";
130  case cProtocol::pktUpdateSign: return "pktUpdateSign";
131  case cProtocol::pktUseBed: return "pktUseBed";
132  case cProtocol::pktWeather: return "pktWeather";
133  case cProtocol::pktWindowItems: return "pktWindowItems";
134  case cProtocol::pktWindowClose: return "pktWindowClose";
135  case cProtocol::pktWindowOpen: return "pktWindowOpen";
136  case cProtocol::pktWindowProperty: return "pktWindowProperty";
137  }
138  return fmt::format(FMT_STRING("Unknown packet type: 0x{:02x}"), a_PacketType);
139 }
#define VERIFY(x)
Definition: Globals.h:280
signed char Int8
Definition: Globals.h:154
signed int Int32
Definition: Globals.h:152
std::string AString
Definition: StringUtils.h:11
bool WriteBEUInt8(UInt8 a_Value)
Definition: ByteBuffer.cpp:615
void WriteUUID(const cUUID &a_UUID)
Writes the specified UUID as a 128-bit BigEndian integer.
Definition: Packetizer.cpp:44
void WriteBEInt32(Int32 a_Value)
Definition: Packetizer.h:105
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
~cPacketizer()
Sends the packet via the contained protocol's SendPacket() function.
Definition: Packetizer.cpp:17
void WriteFPInt(double a_Value)
Writes the double value as a 27:5 fixed-point integer.
Definition: Packetizer.cpp:35
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 void SendPacket(cPacketizer &a_Packet)=0
Sends a single packet contained within the cPacketizer class.
ePacketType
Logical types of outgoing packets.
Definition: Protocol.h:57
@ pktUnloadChunk
Definition: Protocol.h:123
@ pktEntityHeadLook
Definition: Protocol.h:77
@ pktSoundEffect
Definition: Protocol.h:108
@ pktEntityRelMove
Definition: Protocol.h:81
@ pktSoundParticleEffect
Definition: Protocol.h:109
@ pktEntityVelocity
Definition: Protocol.h:84
@ pktEntityMeta
Definition: Protocol.h:79
@ pktAttachEntity
Definition: Protocol.h:58
@ pktBlockChanges
Definition: Protocol.h:62
@ pktDisplayObjective
Definition: Protocol.h:71
@ pktChatRaw
Definition: Protocol.h:65
@ pktUseBed
Definition: Protocol.h:129
@ pktMapData
Definition: Protocol.h:95
@ pktResourcePack
Definition: Protocol.h:104
@ pktSpawnGlobalEntity
Definition: Protocol.h:111
@ pktKeepAlive
Definition: Protocol.h:92
@ pktInventorySlot
Definition: Protocol.h:90
@ pktSpawnPainting
Definition: Protocol.h:114
@ pktParticleEffect
Definition: Protocol.h:96
@ pktSpawnOtherPlayer
Definition: Protocol.h:113
@ pktCameraSetTo
Definition: Protocol.h:64
@ pktEntityEffect
Definition: Protocol.h:75
@ pktTeleportEntity
Definition: Protocol.h:120
@ pktUpdateBlockEntity
Definition: Protocol.h:125
@ pktPluginMessage
Definition: Protocol.h:102
@ pktSpawnExperienceOrb
Definition: Protocol.h:110
@ pktSpawnObject
Definition: Protocol.h:107
@ pktPlayerMoveLook
Definition: Protocol.h:101
@ pktEditSign
Definition: Protocol.h:72
@ pktStatistics
Definition: Protocol.h:117
@ pktSpawnPosition
Definition: Protocol.h:115
@ pktTimeUpdate
Definition: Protocol.h:121
@ pktStatusResponse
Definition: Protocol.h:118
@ pktBossBar
Definition: Protocol.h:63
@ pktPlayerList
Definition: Protocol.h:99
@ pktEntityRelMoveLook
Definition: Protocol.h:82
@ pktJoinGame
Definition: Protocol.h:91
@ pktExplosion
Definition: Protocol.h:86
@ pktDifficulty
Definition: Protocol.h:68
@ pktDestroyEntity
Definition: Protocol.h:67
@ pktUpdateHealth
Definition: Protocol.h:126
@ pktUpdateScore
Definition: Protocol.h:127
@ pktHorseWindowOpen
Definition: Protocol.h:89
@ pktPingResponse
Definition: Protocol.h:97
@ pktEntityEquipment
Definition: Protocol.h:76
@ pktWindowOpen
Definition: Protocol.h:133
@ pktUpdateSign
Definition: Protocol.h:128
@ pktRespawn
Definition: Protocol.h:105
@ pktWindowProperty
Definition: Protocol.h:134
@ pktEntityProperties
Definition: Protocol.h:80
@ pktEntityStatus
Definition: Protocol.h:83
@ pktEncryptionRequest
Definition: Protocol.h:73
@ pktScoreboardObjective
Definition: Protocol.h:106
@ pktCollectEntity
Definition: Protocol.h:66
@ pktBlockChange
Definition: Protocol.h:61
@ pktRemoveEntityEffect
Definition: Protocol.h:103
@ pktUnlockRecipe
Definition: Protocol.h:124
@ pktBlockBreakAnim
Definition: Protocol.h:60
@ pktTabCompletionResults
Definition: Protocol.h:119
@ pktHeldItemChange
Definition: Protocol.h:88
@ pktEntityAnimation
Definition: Protocol.h:74
@ pktWindowClose
Definition: Protocol.h:132
@ pktSpawnMob
Definition: Protocol.h:112
@ pktStartCompression
Definition: Protocol.h:116
@ pktLoginSuccess
Definition: Protocol.h:94
@ pktPlayerListHeaderFooter
Definition: Protocol.h:100
@ pktEntityLook
Definition: Protocol.h:78
@ pktDisconnectDuringGame
Definition: Protocol.h:70
@ pktTitle
Definition: Protocol.h:122
@ pktPlayerAbilities
Definition: Protocol.h:98
@ pktWeather
Definition: Protocol.h:130
@ pktExperience
Definition: Protocol.h:85
@ pktGameMode
Definition: Protocol.h:87
@ pktLeashEntity
Definition: Protocol.h:93
@ pktDisconnectDuringLogin
Definition: Protocol.h:69
@ pktBlockAction
Definition: Protocol.h:59
@ pktWindowItems
Definition: Protocol.h:131
Definition: UUID.h:11
std::array< Byte, 16 > ToRaw() const
Converts UUID to raw memory representation, respecting UUID variant.
Definition: UUID.cpp:214