r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Protocol.hpp
Go to the documentation of this file.
1///
2/// @file Protocol.hpp
3/// @brief This file contains the network protocol
4/// @namespace rnp
5///
6
7#pragma once
8
9#include <array>
10#include <cstdint>
11#include <cstring>
12#include <stdexcept>
13#include <vector>
14
15namespace rnp
16{
17
18 inline constexpr std::size_t MAX_PAYLOAD = 512;
19
20 ///
21 /// @brief Packet types according to RNP specification
22 ///
23 enum class PacketType : std::uint8_t
24 {
25 CONNECT = 0x01,
26 DISCONNECT = 0x02,
27 WORLD_STATE = 0x03,
28 PING = 0x04,
29 PONG = 0x05,
30 PACKET_ERROR = 0x06,
31 ENTITY_EVENT = 0x07,
32 CONNECT_ACCEPT = 0x08,
33 // Lobby system packets
34 LOBBY_LIST_REQUEST = 0x09,
36 LOBBY_CREATE = 0x0B,
38 LOBBY_JOIN = 0x0D,
40 LOBBY_LEAVE = 0x0F,
41 LOBBY_UPDATE = 0x10,
42 GAME_START = 0x11,
43 START_GAME_REQUEST = 0x12,
44 GAME_OVER = 0x13,
45 };
46
47 ///
48 /// @brief Packet flags for reliability and fragmentation
49 ///
50 enum class PacketFlags : std::uint16_t
51 {
52 NONE = 0x0000,
53 RELIABLE = 0x0001,
54 COMPRESSED = 0x0002
55 };
56
57 ///
58 /// @brief Disconnect reason codes
59 ///
60 enum class DisconnectReason : std::uint16_t
61 {
62 UNSPECIFIED = 0,
64 TIMEOUT = 2,
67 SERVER_FULL = 5,
68 BANNED = 6
69 };
70
71 ///
72 /// @brief Error codes
73 ///
74 enum class ErrorCode : std::uint16_t
75 {
76 NONE = 0,
79 RATE_LIMITED = 3,
82 LOBBY_FULL = 6,
84 NOT_IN_LOBBY = 8,
86 };
87
88 ///
89 /// @brief Event types for ENTITY_EVENT packets
90 ///
91 enum class EventType : std::uint8_t
92 {
93 SPAWN = 0x01,
94 DESPAWN = 0x02,
95 DAMAGE = 0x03,
96 SCORE = 0x04,
97 POWERUP = 0x05,
98 INPUT = 0x06,
99 CUSTOM = 0xFF
100 };
101
102 ///
103 /// @brief Entity types for world state
104 ///
105 enum class EntityType : std::uint16_t
106 {
107 PLAYER = 0x01,
108 ENEMY = 0x02,
109 PROJECTILE = 0x03,
110 BOSS = 0x04,
111 };
112
113 ///
114 /// @brief Event record for ENTITY_EVENT packets (TLV format)
115 ///
117 {
119 std::uint32_t entityId;
120 std::vector<std::uint8_t> data;
121 };
122
123 ///
124 /// @brief Packet header according to RNP specification (Big Endian)
125 /// Total size: 7 bytes (1 + 2 + 4)
126 ///
128 {
129 std::uint8_t type;
130 std::uint16_t length;
131 std::uint32_t sessionId;
132 };
133
134 ///
135 /// @brief CONNECT packet payload
136 ///
138 {
139 std::uint8_t nameLen;
140 std::array<char, 32> playerName;
141 std::uint32_t clientCaps;
142 };
143
144 ///
145 /// @brief CONNECT_ACCEPT packet payload
146 ///
148 {
149 std::uint32_t sessionId;
150 std::uint16_t tickRateHz;
151 std::uint16_t mtuPayloadBytes;
152 std::uint32_t serverCaps;
153 };
154
155 ///
156 /// @brief DISCONNECT packet payload
157 ///
159 {
160 std::uint16_t reasonCode; // DisconnectReason
161 };
162
163 ///
164 /// @brief Entity subtype enumeration
165 ///
166 enum class EntitySubtype : std::uint8_t
167 {
168 NONE = 0,
169 // Enemy subtypes
170 ENEMY_BASIC = 1,
171 ENEMY_ADVANCED = 2,
172 ENEMY_BOSS = 3,
173 // Projectile subtypes
176 PROJECTILE_ENEMY = 12,
177 };
178
179 ///
180 /// @brief Entity state for WORLD_STATE packet
181 ///
183 {
184 std::uint32_t id;
185 std::uint16_t type; // EntityType
186 std::uint8_t subtype; // EntitySubtype - specific variant of entity
187 float x, y;
188 float vx, vy;
189 std::uint8_t healthPercent; // Health as percentage (0-100), 255 = no health bar
190 std::uint8_t stateFlags; // Additional flags (beam charge for players, etc.)
191 std::uint32_t score; // Player score (0 for non-player entities)
192 };
193
194 ///
195 /// @brief WORLD_STATE packet payload
196 ///
198 {
199 std::uint32_t serverTick;
200 std::uint16_t entityCount;
201 std::vector<EntityState> entities;
202 };
203
204 ///
205 /// @brief PING/PONG packet payload
206 ///
208 {
209 std::uint32_t nonce;
210 std::uint32_t sendTimeMs;
211 };
212
213 ///
214 /// @brief ERROR packet payload
215 ///
217 {
218 std::uint16_t errorCode; // ErrorCode
219 std::uint16_t msgLen;
220 std::string description;
221 };
222
223 ///
224 /// @brief Lobby status enumeration
225 ///
226 enum class LobbyStatus : std::uint8_t
227 {
228 WAITING = 0,
229 IN_GAME = 1,
230 FINISHED = 2
231 };
232
233 ///
234 /// @brief Lobby information structure
235 ///
237 {
238 std::uint32_t lobbyId;
239 std::array<char, 32> lobbyName;
240 std::uint8_t currentPlayers;
241 std::uint8_t maxPlayers;
242 std::uint8_t gameMode;
243 std::uint8_t status; // LobbyStatus
244 std::uint32_t hostSessionId;
245 std::array<std::array<char, 32>, 8> playerNames; // Max 8 players
246 };
247
248 ///
249 /// @brief LOBBY_LIST_RESPONSE packet payload
250 ///
252 {
253 std::uint16_t lobbyCount;
254 std::vector<LobbyInfo> lobbies;
255 };
256
257 ///
258 /// @brief LOBBY_CREATE packet payload
259 ///
261 {
262 std::uint8_t nameLen;
263 std::array<char, 32> lobbyName;
264 std::uint8_t maxPlayers;
265 std::uint8_t gameMode;
266 };
267
268 ///
269 /// @brief LOBBY_CREATE_RESPONSE packet payload
270 ///
272 {
273 std::uint32_t lobbyId;
274 std::uint8_t success; // 0=failure, 1=success
275 std::uint16_t errorCode; // ErrorCode if success=0
276 };
277
278 ///
279 /// @brief LOBBY_JOIN packet payload
280 ///
282 {
283 std::uint32_t lobbyId;
284 };
285
286 ///
287 /// @brief LOBBY_JOIN_RESPONSE packet payload
288 ///
290 {
291 std::uint32_t lobbyId;
292 std::uint8_t success; // 0=failure, 1=success
293 std::uint16_t errorCode; // ErrorCode if success=0
294 LobbyInfo lobbyInfo; // Current lobby state if success=1
295 };
296
297 ///
298 /// @brief LOBBY_UPDATE packet payload
299 ///
304
305 ///
306 /// @brief GAME_START packet payload
307 ///
309 {
310 std::uint32_t lobbyId;
311 };
312
313 ///
314 /// @brief START_GAME_REQUEST packet payload (client requests to start game)
315 ///
317 {
318 std::uint32_t lobbyId;
319 };
320
321} // namespace rnp
ErrorCode
Error codes.
Definition Protocol.hpp:75
EventType
Event types for ENTITY_EVENT packets.
Definition Protocol.hpp:92
PacketFlags
Packet flags for reliability and fragmentation.
Definition Protocol.hpp:51
DisconnectReason
Disconnect reason codes.
Definition Protocol.hpp:61
EntityType
Entity types for world state.
Definition Protocol.hpp:106
PacketType
Packet types according to RNP specification.
Definition Protocol.hpp:24
EntitySubtype
Entity subtype enumeration.
Definition Protocol.hpp:167
LobbyStatus
Lobby status enumeration.
Definition Protocol.hpp:227
constexpr std::size_t MAX_PAYLOAD
Definition Protocol.hpp:18
Entity state for WORLD_STATE packet.
Definition Protocol.hpp:183
std::uint32_t id
Definition Protocol.hpp:184
std::uint16_t type
Definition Protocol.hpp:185
std::uint32_t score
Definition Protocol.hpp:191
std::uint8_t stateFlags
Definition Protocol.hpp:190
std::uint8_t healthPercent
Definition Protocol.hpp:189
std::uint8_t subtype
Definition Protocol.hpp:186
Event record for ENTITY_EVENT packets (TLV format)
Definition Protocol.hpp:117
std::uint32_t entityId
Definition Protocol.hpp:119
std::vector< std::uint8_t > data
Definition Protocol.hpp:120
Lobby information structure.
Definition Protocol.hpp:237
std::uint32_t hostSessionId
Definition Protocol.hpp:244
std::array< char, 32 > lobbyName
Definition Protocol.hpp:239
std::uint8_t status
Definition Protocol.hpp:243
std::array< std::array< char, 32 >, 8 > playerNames
Definition Protocol.hpp:245
std::uint32_t lobbyId
Definition Protocol.hpp:238
std::uint8_t currentPlayers
Definition Protocol.hpp:240
std::uint8_t maxPlayers
Definition Protocol.hpp:241
std::uint8_t gameMode
Definition Protocol.hpp:242
CONNECT_ACCEPT packet payload.
Definition Protocol.hpp:148
std::uint32_t sessionId
Definition Protocol.hpp:149
std::uint16_t tickRateHz
Definition Protocol.hpp:150
std::uint16_t mtuPayloadBytes
Definition Protocol.hpp:151
std::uint32_t serverCaps
Definition Protocol.hpp:152
CONNECT packet payload.
Definition Protocol.hpp:138
std::array< char, 32 > playerName
Definition Protocol.hpp:140
std::uint32_t clientCaps
Definition Protocol.hpp:141
std::uint8_t nameLen
Definition Protocol.hpp:139
DISCONNECT packet payload.
Definition Protocol.hpp:159
std::uint16_t reasonCode
Definition Protocol.hpp:160
ERROR packet payload.
Definition Protocol.hpp:217
std::uint16_t msgLen
Definition Protocol.hpp:219
std::uint16_t errorCode
Definition Protocol.hpp:218
std::string description
Definition Protocol.hpp:220
GAME_START packet payload.
Definition Protocol.hpp:309
std::uint32_t lobbyId
Definition Protocol.hpp:310
Packet header according to RNP specification (Big Endian) Total size: 7 bytes (1 + 2 + 4)
Definition Protocol.hpp:128
std::uint32_t sessionId
Definition Protocol.hpp:131
std::uint8_t type
Definition Protocol.hpp:129
std::uint16_t length
Definition Protocol.hpp:130
LOBBY_CREATE_RESPONSE packet payload.
Definition Protocol.hpp:272
LOBBY_CREATE packet payload.
Definition Protocol.hpp:261
std::array< char, 32 > lobbyName
Definition Protocol.hpp:263
std::uint8_t maxPlayers
Definition Protocol.hpp:264
std::uint8_t gameMode
Definition Protocol.hpp:265
LOBBY_JOIN_RESPONSE packet payload.
Definition Protocol.hpp:290
LOBBY_JOIN packet payload.
Definition Protocol.hpp:282
std::uint32_t lobbyId
Definition Protocol.hpp:283
LOBBY_LIST_RESPONSE packet payload.
Definition Protocol.hpp:252
std::vector< LobbyInfo > lobbies
Definition Protocol.hpp:254
LOBBY_UPDATE packet payload.
Definition Protocol.hpp:301
PING/PONG packet payload.
Definition Protocol.hpp:208
std::uint32_t sendTimeMs
Definition Protocol.hpp:210
std::uint32_t nonce
Definition Protocol.hpp:209
START_GAME_REQUEST packet payload (client requests to start game)
Definition Protocol.hpp:317
WORLD_STATE packet payload.
Definition Protocol.hpp:198
std::uint16_t entityCount
Definition Protocol.hpp:200
std::uint32_t serverTick
Definition Protocol.hpp:199
std::vector< EntityState > entities
Definition Protocol.hpp:201