GPR5100 - Rollback
Loading...
Searching...
No Matches
packet_type.h
Go to the documentation of this file.
1
4#pragma once
5
6#include <SFML/Network/Packet.hpp>
7
8#include "game/game_globals.h"
9#include <memory>
10#include <chrono>
11
12namespace game
13{
14enum class PacketType : std::uint8_t
15{
16 JOIN = 0u,
17 SPAWN_PLAYER,
18 INPUT,
19 SPAWN_BULLET,
20 VALIDATE_STATE,
21 START_GAME,
22 JOIN_ACK,
23 WIN_GAME,
24 PING,
25 NONE,
26};
27
31using PhysicsState = std::uint16_t;
32
36struct Packet
37{
38 virtual ~Packet() = default;
39 PacketType packetType = PacketType::NONE;
40};
41
42inline sf::Packet& operator<<(sf::Packet& packetReceived, Packet& packet)
43{
44 const auto packetType = static_cast<std::uint8_t>(packet.packetType);
45 packetReceived << packetType;
46 return packetReceived;
47}
48
49inline sf::Packet& operator>>(sf::Packet& packetReceived, Packet& packet)
50{
51 std::uint8_t packetType;
52 packetReceived >> packetType;
53 packet.packetType = static_cast<PacketType>(packetType);
54 return packetReceived;
55}
56
61template<PacketType type>
63{
64 TypedPacket() { packetType = type; }
65};
66
67template<typename T, size_t N>
68sf::Packet& operator<<(sf::Packet& packet, const std::array<T, N>& t)
69{
70 for (auto& tmp : t)
71 {
72 packet << tmp;
73 }
74 return packet;
75}
76
77template <typename T, size_t N>
78sf::Packet& operator>>(sf::Packet& packet, std::array<T, N>& t)
79{
80 for (auto& tmp : t)
81 {
82 packet >> tmp;
83 }
84 return packet;
85}
86
90struct JoinPacket : TypedPacket<PacketType::JOIN>
91{
92 std::array<std::uint8_t, sizeof(ClientId)> clientId{};
93 std::array<std::uint8_t, sizeof(unsigned long)> startTime{};
94};
95
96inline sf::Packet& operator<<(sf::Packet& packet, const JoinPacket& joinPacket)
97{
98 return packet << joinPacket.clientId << joinPacket.startTime;
99}
100
101inline sf::Packet& operator>>(sf::Packet& packet, JoinPacket& joinPacket)
102{
103 return packet >> joinPacket.clientId >> joinPacket.startTime;
104}
105
109struct JoinAckPacket : TypedPacket<PacketType::JOIN_ACK>
110{
111 std::array<std::uint8_t, sizeof(ClientId)> clientId{};
112 std::array<std::uint8_t, sizeof(unsigned short)> udpPort{};
113};
114
115
116inline sf::Packet& operator<<(sf::Packet& packet, const JoinAckPacket& spawnPlayerPacket)
117{
118 return packet << spawnPlayerPacket.clientId << spawnPlayerPacket.udpPort;
119}
120
121inline sf::Packet& operator>>(sf::Packet& packet, JoinAckPacket& joinPacket)
122{
123 return packet >> joinPacket.clientId >> joinPacket.udpPort;
124}
125
129struct SpawnPlayerPacket : TypedPacket<PacketType::SPAWN_PLAYER>
130{
131 std::array<std::uint8_t, sizeof(ClientId)> clientId{};
132 PlayerNumber playerNumber = INVALID_PLAYER;
133 std::array<std::uint8_t, sizeof(core::Vec2f)> pos{};
134 std::array<std::uint8_t, sizeof(core::Degree)> angle{};
135};
136
137inline sf::Packet& operator<<(sf::Packet& packet, const SpawnPlayerPacket& spawnPlayerPacket)
138{
139 return packet << spawnPlayerPacket.clientId << spawnPlayerPacket.playerNumber <<
140 spawnPlayerPacket.pos << spawnPlayerPacket.angle;
141}
142
143inline sf::Packet& operator>>(sf::Packet& packet, SpawnPlayerPacket& spawnPlayerPacket)
144{
145 return packet >> spawnPlayerPacket.clientId >> spawnPlayerPacket.playerNumber >>
146 spawnPlayerPacket.pos >> spawnPlayerPacket.angle;
147}
148
149
154struct PlayerInputPacket : TypedPacket<PacketType::INPUT>
155{
156 PlayerNumber playerNumber = INVALID_PLAYER;
157 std::array<std::uint8_t, sizeof(Frame)> currentFrame{};
158 std::array<std::uint8_t, maxInputNmb> inputs{};
159};
160
161inline sf::Packet& operator<<(sf::Packet& packet, const PlayerInputPacket& playerInputPacket)
162{
163 return packet << playerInputPacket.playerNumber <<
164 playerInputPacket.currentFrame << playerInputPacket.inputs;
165}
166
167inline sf::Packet& operator>>(sf::Packet& packet, PlayerInputPacket& playerInputPacket)
168{
169 return packet >> playerInputPacket.playerNumber >>
170 playerInputPacket.currentFrame >> playerInputPacket.inputs;
171}
172
176struct StartGamePacket : TypedPacket<PacketType::START_GAME>
177{
178};
179
183struct ValidateFramePacket : TypedPacket<PacketType::VALIDATE_STATE>
184{
185 std::array<std::uint8_t, sizeof(Frame)> newValidateFrame{};
186 std::array<std::uint8_t, sizeof(PhysicsState) * maxPlayerNmb> physicsState{};
187};
188
189inline sf::Packet& operator<<(sf::Packet& packet, const ValidateFramePacket& validateFramePacket)
190{
191 return packet << validateFramePacket.newValidateFrame << validateFramePacket.physicsState;
192}
193
194inline sf::Packet& operator>>(sf::Packet& packet, ValidateFramePacket& ValidateFramePacket)
195{
196 return packet >> ValidateFramePacket.newValidateFrame >> ValidateFramePacket.physicsState;
197}
198
202struct WinGamePacket : TypedPacket<PacketType::WIN_GAME>
203{
205};
206
207inline sf::Packet& operator<<(sf::Packet& packet, const WinGamePacket& winGamePacket)
208{
209 return packet << winGamePacket.winner;
210}
211
212inline sf::Packet& operator>>(sf::Packet& packet, WinGamePacket& winGamePacket)
213{
214 return packet >> winGamePacket.winner;
215}
216
220struct PingPacket : TypedPacket<PacketType::PING>
221{
222 std::array<std::uint8_t, sizeof(unsigned long long)> time{};
223 std::array<std::uint8_t, sizeof(ClientId)> clientId{};
224};
225
226inline sf::Packet& operator<<(sf::Packet& packet, const PingPacket& pingPacket)
227{
228 return packet << pingPacket.time << pingPacket.clientId;
229}
230
231inline sf::Packet& operator>>(sf::Packet& packet, PingPacket& pingPacket)
232{
233 return packet >> pingPacket.time >> pingPacket.clientId;
234}
235
236inline void GeneratePacket(sf::Packet& packet, Packet& sendingPacket)
237{
238 packet << sendingPacket;
239 switch (sendingPacket.packetType)
240 {
241 case PacketType::JOIN:
242 {
243 const auto& packetTmp = static_cast<JoinPacket&>(sendingPacket);
244 packet << packetTmp;
245 break;
246 }
247 case PacketType::SPAWN_PLAYER:
248 {
249 const auto& packetTmp = static_cast<SpawnPlayerPacket&>(sendingPacket);
250 packet << packetTmp;
251 break;
252 }
253 case PacketType::INPUT:
254 {
255 const auto& packetTmp = static_cast<PlayerInputPacket&>(sendingPacket);
256 packet << packetTmp;
257 break;
258 }
259 case PacketType::VALIDATE_STATE:
260 {
261 const auto& packetTmp = static_cast<ValidateFramePacket&>(sendingPacket);
262 packet << packetTmp;
263 break;
264 }
265 case PacketType::START_GAME:
266 {
267 break;
268 }
269 case PacketType::JOIN_ACK:
270 {
271 const auto& packetTmp = static_cast<JoinAckPacket&>(sendingPacket);
272 packet << packetTmp;
273 break;
274 }
275 case PacketType::WIN_GAME:
276 {
277 const auto& packetTmp = static_cast<WinGamePacket&>(sendingPacket);
278 packet << packetTmp;
279 break;
280 }
281 case PacketType::PING:
282 {
283 const auto& packetTmp = static_cast<PingPacket&>(sendingPacket);
284 packet << packetTmp;
285 break;
286 }
287
288 default:
289 break;
290 }
291}
292
293inline std::unique_ptr<Packet> GenerateReceivedPacket(sf::Packet& packet)
294{
295 Packet packetTmp;
296 packet >> packetTmp;
297 switch (packetTmp.packetType)
298 {
299 case PacketType::JOIN:
300 {
301 auto joinPacket = std::make_unique<JoinPacket>();
302 joinPacket->packetType = packetTmp.packetType;
303 packet >> *joinPacket;
304 return joinPacket;
305 }
306 case PacketType::SPAWN_PLAYER:
307 {
308 auto spawnPlayerPacket = std::make_unique<SpawnPlayerPacket>();
309 spawnPlayerPacket->packetType = packetTmp.packetType;
310 packet >> *spawnPlayerPacket;
311 return spawnPlayerPacket;
312 }
313 case PacketType::INPUT:
314 {
315 auto playerInputPacket = std::make_unique<PlayerInputPacket>();
316 playerInputPacket->packetType = packetTmp.packetType;
317 packet >> *playerInputPacket;
318 return playerInputPacket;
319 }
320 case PacketType::VALIDATE_STATE:
321 {
322 auto validateFramePacket = std::make_unique<ValidateFramePacket>();
323 validateFramePacket->packetType = packetTmp.packetType;
324 packet >> *validateFramePacket;
325 return validateFramePacket;
326 }
327 case PacketType::START_GAME:
328 {
329 auto startGamePacket = std::make_unique<StartGamePacket>();
330 startGamePacket->packetType = packetTmp.packetType;
331 return startGamePacket;
332 }
333 case PacketType::JOIN_ACK:
334 {
335 auto joinAckPacket = std::make_unique<JoinAckPacket>();
336 joinAckPacket->packetType = packetTmp.packetType;
337 packet >> *joinAckPacket;
338 return joinAckPacket;
339 }
340 case PacketType::WIN_GAME:
341 {
342 auto winGamePacket = std::make_unique<WinGamePacket>();
343 winGamePacket->packetType = packetTmp.packetType;
344 packet >> *winGamePacket;
345 return winGamePacket;
346 }
347 case PacketType::PING:
348 {
349 auto pingPacket = std::make_unique<PingPacket>();
350 pingPacket->packetType = packetTmp.packetType;
351 packet >> *pingPacket;
352 return pingPacket;
353 }
354 default:;
355 }
356 return nullptr;
357}
358
363{
364public:
365 virtual ~PacketSenderInterface() = default;
366 virtual void SendReliablePacket(std::unique_ptr<Packet> packet) = 0;
367 virtual void SendUnreliablePacket(std::unique_ptr<Packet> packet) = 0;
368};
369}
Degree is an utility class that describes degree angles (0 to 360). It can be easily converted to Rad...
Definition: angle.h:56
PacketSenderInterface is a interface for any Server or Client who wants to send and receive packets.
Definition: packet_type.h:363
constexpr auto INVALID_PLAYER
INVALID_PLAYER is an integer constant that defines an invalid player number.
Definition: game_globals.h:26
ClientId
ClientId is a type used to define the client identification. It is given by the server to clients.
Definition: game_globals.h:31
std::uint8_t PlayerNumber
PlayerNumber is a type used to define the number of the player. Starting from 0 to maxPlayerNmb.
Definition: game_globals.h:22
constexpr std::uint32_t maxPlayerNmb
mmaxPlayerNmb is a integer constant that defines the maximum number of player per game
Definition: game_globals.h:37
std::uint16_t PhysicsState
PhysicsState is the type of the physics state checksum.
Definition: packet_type.h:31
Vec2f is a utility class that represents a mathematical 2d vector.
Definition: vec2.h:12
JoinAckPacket is a TCP Packet that is sent by the server to the client to answer a join packet.
Definition: packet_type.h:110
JoinPacket is a TCP Packet that is sent by a client to the server to join a game.
Definition: packet_type.h:91
Packet is a interface that defines what a packet with a PacketType.
Definition: packet_type.h:37
PingPacket is an UDP Packet sent by the client to the server and resend by the server to measure the ...
Definition: packet_type.h:221
PlayerInputPacket is a UDP Packet sent by the player client and then replicated by the server to all ...
Definition: packet_type.h:155
SpawnPlayerPacket is a TCP Packet sent by the server to all clients to notify of the spawn of a new p...
Definition: packet_type.h:130
StartGamePacket is a TCP Packet send by the server to start a game at a given time.
Definition: packet_type.h:177
TypedPacket is a template class that sets the packetType of Packet automatically at construction with...
Definition: packet_type.h:63
ValidateFramePacket is an UDP packet that is sent by the server to validate the last physics state of...
Definition: packet_type.h:184
WinGamePacket is a TCP Packet sent by the server to notify the clients that a certain player has won.
Definition: packet_type.h:203