Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
NetworkSyncSystem.cpp
Go to the documentation of this file.
1
10#include "Systems/NetworkSyncSystem.hpp"
11
12#include "Game/Room.hpp"
13
14namespace rtp::server
15{
16
18 // Public API
20
22 ecs::Registry &registry)
23 : _network(network)
24 , _registry(registry) {};
25
27 {
28 (void)dt;
29 };
30
32 ecs::Entity entity)
33 {
34 _sessionToEntity[sessionId] = entity;
35 }
36
37 void NetworkSyncSystem::unbindSession(uint32_t sessionId)
38 {
39 _sessionToEntity.erase(sessionId);
40 }
41
42 void NetworkSyncSystem::handleInput(uint32_t sessionId,
43 const net::Packet &packet)
44 {
45 net::InputPayload payload;
46 net::Packet tempPacket = packet;
47 tempPacket >> payload;
48 log::info("InputTick: session={} mask={}", sessionId,
49 (int)payload.inputMask);
50 if (_sessionToEntity.find(sessionId) == _sessionToEntity.end()) {
51 return;
52 }
53
54 ecs::Entity entity = _sessionToEntity[sessionId];
55
56 auto inputsRes =
58 if (inputsRes) {
59 auto &inputs = inputsRes->get();
60 if (inputs.has(entity)) {
61 inputs[entity].lastMask = inputs[entity].mask;
62 inputs[entity].mask = payload.inputMask;
63 return;
64 }
65 }
66
68 inputData.mask = payload.inputMask;
70 inputData);
71 }
72
73 void NetworkSyncSystem::handleDisconnect(uint32_t sessionId)
74 {
75 if (_sessionToEntity.find(sessionId) != _sessionToEntity.end()) {
76 ecs::Entity entity = _sessionToEntity[sessionId];
77 _sessionToEntity.erase(sessionId);
78 log::info("Destroyed entity {} for disconnected session {}",
79 entity.index(), sessionId);
80 }
81 }
82
83 uint32_t
85 const net::Packet &packet)
86 {
87 net::ConnectPayload payload;
88 net::Packet tempPacket = packet;
89 tempPacket >> payload;
90 log::info("Player new connection [not logged]: session={}",
91 payload.sessionId);
92 return {sessionId};
93 }
94
96 const net::Packet &packet,
98 {
99 for (const auto &[sessionId, entity] : _sessionToEntity) {
100 if (entity.index() == entityId) {
101 log::info("sendPacketToEntity: sending packet to session {} "
102 "for entity {}",
103 sessionId, entityId);
104 _network.sendPacket(sessionId, packet, mode);
105 return;
106 }
107 }
108 log::warning("sendPacketToEntity: no session bound to entity {}",
109 entityId);
110 }
111
113 const net::Packet &packet,
114 net::NetworkMode mode)
115 {
116 _network.sendPacket(sessionId, packet, mode);
117 }
118
120 const std::vector<uint32_t> &sessions, const net::Packet &packet,
121 net::NetworkMode mode)
122 {
123 for (uint32_t sessionId : sessions) {
124 _network.sendPacket(sessionId, packet, mode);
125 }
126 }
127}
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
constexpr std::uint32_t index(void) const
auto add(Entity entity, Args &&...args) -> std::expected< std::reference_wrapper< T >, rtp::Error >
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
Network packet with header and serializable body.
Definition Packet.hpp:471
void handleDisconnect(uint32_t sessionId)
Handle disconnection of a client.
void sendPacketToSessions(const std::vector< uint32_t > &sessions, const net::Packet &packet, net::NetworkMode mode)
Send a packet to multiple sessions.
ServerNetwork & _network
Reference to the server network manager.
uint32_t handlePlayerConnection(uint32_t sessionId, const net::Packet &packet)
Handle new player connection.
void handleInput(uint32_t sessionId, const net::Packet &packet)
Handle input received from a client.
ecs::Registry & _registry
Reference to the entity registry.
void sendPacketToSession(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to a specific session.
NetworkSyncSystem(ServerNetwork &network, ecs::Registry &registry)
Constructor for NetworkSyncSystem.
std::unordered_map< uint32_t, ecs::Entity > _sessionToEntity
Map of session IDs to entities (with generation)
void update(float dt) override
Update system logic for one frame.
void sendPacketToEntity(uint32_t entityId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to the entity associated with the given ID.
void unbindSession(uint32_t sessionId)
void bindSessionToEntity(uint32_t sessionId, ecs::Entity entity)
Bind a network session to an entity.
Implementation ASIO du serveur réseau (TCP + UDP)
void sendPacket(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to a specific session.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
NetworkMode
Enum representing network transmission modes.
Definition INetwork.hpp:25
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
Component to handle network input for server entities.
uint8_t mask
Input mask for filtering input types.
uint8_t lastMask
Previous input mask for edge detection.
Player connection data sended Client OpCode.
Definition Packet.hpp:174
uint32_t sessionId
Session identifier.
Definition Packet.hpp:175
Client input state data.
Definition Packet.hpp:460
uint8_t inputMask
Bitmask of input states.
Definition Packet.hpp:461