6#include <SFML/Network/Packet.hpp>
14enum class PacketType : std::uint8_t
39 PacketType packetType = PacketType::NONE;
42inline sf::Packet& operator<<(sf::Packet& packetReceived,
Packet& packet)
44 const auto packetType =
static_cast<std::uint8_t
>(packet.packetType);
45 packetReceived << packetType;
46 return packetReceived;
49inline sf::Packet& operator>>(sf::Packet& packetReceived, Packet& packet)
51 std::uint8_t packetType;
52 packetReceived >> packetType;
53 packet.packetType =
static_cast<PacketType
>(packetType);
54 return packetReceived;
61template<PacketType type>
67template<
typename T,
size_t N>
68sf::Packet& operator<<(sf::Packet& packet,
const std::array<T, N>& t)
77template <
typename T,
size_t N>
78sf::Packet& operator>>(sf::Packet& packet, std::array<T, N>& t)
92 std::array<std::uint8_t,
sizeof(
ClientId)> clientId{};
93 std::array<std::uint8_t,
sizeof(
unsigned long)> startTime{};
96inline sf::Packet& operator<<(sf::Packet& packet,
const JoinPacket& joinPacket)
98 return packet << joinPacket.clientId << joinPacket.startTime;
101inline sf::Packet& operator>>(sf::Packet& packet, JoinPacket& joinPacket)
103 return packet >> joinPacket.clientId >> joinPacket.startTime;
111 std::array<std::uint8_t,
sizeof(
ClientId)> clientId{};
112 std::array<std::uint8_t,
sizeof(
unsigned short)> udpPort{};
116inline sf::Packet& operator<<(sf::Packet& packet,
const JoinAckPacket& spawnPlayerPacket)
118 return packet << spawnPlayerPacket.clientId << spawnPlayerPacket.udpPort;
121inline sf::Packet& operator>>(sf::Packet& packet, JoinAckPacket& joinPacket)
123 return packet >> joinPacket.clientId >> joinPacket.udpPort;
131 std::array<std::uint8_t,
sizeof(
ClientId)> clientId{};
133 std::array<std::uint8_t,
sizeof(
core::Vec2f)> pos{};
137inline sf::Packet& operator<<(sf::Packet& packet,
const SpawnPlayerPacket& spawnPlayerPacket)
139 return packet << spawnPlayerPacket.clientId << spawnPlayerPacket.playerNumber <<
140 spawnPlayerPacket.pos << spawnPlayerPacket.angle;
143inline sf::Packet& operator>>(sf::Packet& packet, SpawnPlayerPacket& spawnPlayerPacket)
145 return packet >> spawnPlayerPacket.clientId >> spawnPlayerPacket.playerNumber >>
146 spawnPlayerPacket.pos >> spawnPlayerPacket.angle;
157 std::array<std::uint8_t,
sizeof(Frame)> currentFrame{};
158 std::array<std::uint8_t, maxInputNmb> inputs{};
161inline sf::Packet& operator<<(sf::Packet& packet,
const PlayerInputPacket& playerInputPacket)
163 return packet << playerInputPacket.playerNumber <<
164 playerInputPacket.currentFrame << playerInputPacket.inputs;
167inline sf::Packet& operator>>(sf::Packet& packet, PlayerInputPacket& playerInputPacket)
169 return packet >> playerInputPacket.playerNumber >>
170 playerInputPacket.currentFrame >> playerInputPacket.inputs;
185 std::array<std::uint8_t,
sizeof(Frame)> newValidateFrame{};
189inline sf::Packet& operator<<(sf::Packet& packet,
const ValidateFramePacket& validateFramePacket)
191 return packet << validateFramePacket.newValidateFrame << validateFramePacket.physicsState;
194inline sf::Packet& operator>>(sf::Packet& packet, ValidateFramePacket& ValidateFramePacket)
196 return packet >> ValidateFramePacket.newValidateFrame >> ValidateFramePacket.physicsState;
207inline sf::Packet& operator<<(sf::Packet& packet,
const WinGamePacket& winGamePacket)
209 return packet << winGamePacket.winner;
212inline sf::Packet& operator>>(sf::Packet& packet, WinGamePacket& winGamePacket)
214 return packet >> winGamePacket.winner;
222 std::array<std::uint8_t,
sizeof(
unsigned long long)> time{};
223 std::array<std::uint8_t,
sizeof(
ClientId)> clientId{};
226inline sf::Packet& operator<<(sf::Packet& packet,
const PingPacket& pingPacket)
228 return packet << pingPacket.time << pingPacket.clientId;
231inline sf::Packet& operator>>(sf::Packet& packet, PingPacket& pingPacket)
233 return packet >> pingPacket.time >> pingPacket.clientId;
236inline void GeneratePacket(sf::Packet& packet, Packet& sendingPacket)
238 packet << sendingPacket;
239 switch (sendingPacket.packetType)
241 case PacketType::JOIN:
243 const auto& packetTmp =
static_cast<JoinPacket&
>(sendingPacket);
247 case PacketType::SPAWN_PLAYER:
249 const auto& packetTmp =
static_cast<SpawnPlayerPacket&
>(sendingPacket);
253 case PacketType::INPUT:
255 const auto& packetTmp =
static_cast<PlayerInputPacket&
>(sendingPacket);
259 case PacketType::VALIDATE_STATE:
261 const auto& packetTmp =
static_cast<ValidateFramePacket&
>(sendingPacket);
265 case PacketType::START_GAME:
269 case PacketType::JOIN_ACK:
271 const auto& packetTmp =
static_cast<JoinAckPacket&
>(sendingPacket);
275 case PacketType::WIN_GAME:
277 const auto& packetTmp =
static_cast<WinGamePacket&
>(sendingPacket);
281 case PacketType::PING:
283 const auto& packetTmp =
static_cast<PingPacket&
>(sendingPacket);
293inline std::unique_ptr<Packet> GenerateReceivedPacket(sf::Packet& packet)
297 switch (packetTmp.packetType)
299 case PacketType::JOIN:
301 auto joinPacket = std::make_unique<JoinPacket>();
302 joinPacket->packetType = packetTmp.packetType;
303 packet >> *joinPacket;
306 case PacketType::SPAWN_PLAYER:
308 auto spawnPlayerPacket = std::make_unique<SpawnPlayerPacket>();
309 spawnPlayerPacket->packetType = packetTmp.packetType;
310 packet >> *spawnPlayerPacket;
311 return spawnPlayerPacket;
313 case PacketType::INPUT:
315 auto playerInputPacket = std::make_unique<PlayerInputPacket>();
316 playerInputPacket->packetType = packetTmp.packetType;
317 packet >> *playerInputPacket;
318 return playerInputPacket;
320 case PacketType::VALIDATE_STATE:
322 auto validateFramePacket = std::make_unique<ValidateFramePacket>();
323 validateFramePacket->packetType = packetTmp.packetType;
324 packet >> *validateFramePacket;
325 return validateFramePacket;
327 case PacketType::START_GAME:
329 auto startGamePacket = std::make_unique<StartGamePacket>();
330 startGamePacket->packetType = packetTmp.packetType;
331 return startGamePacket;
333 case PacketType::JOIN_ACK:
335 auto joinAckPacket = std::make_unique<JoinAckPacket>();
336 joinAckPacket->packetType = packetTmp.packetType;
337 packet >> *joinAckPacket;
338 return joinAckPacket;
340 case PacketType::WIN_GAME:
342 auto winGamePacket = std::make_unique<WinGamePacket>();
343 winGamePacket->packetType = packetTmp.packetType;
344 packet >> *winGamePacket;
345 return winGamePacket;
347 case PacketType::PING:
349 auto pingPacket = std::make_unique<PingPacket>();
350 pingPacket->packetType = packetTmp.packetType;
351 packet >> *pingPacket;
366 virtual void SendReliablePacket(std::unique_ptr<Packet> packet) = 0;
367 virtual void SendUnreliablePacket(std::unique_ptr<Packet> packet) = 0;
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
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