R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Replicator.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by samuelBleau on 26/11/2025.
4** File description:
5** Replicator.hpp
6*/
7
8#ifndef REPLICATOR_HPP
9#define REPLICATOR_HPP
10
11#include <atomic>
12#include <cstdint>
13#include <functional>
14#include <iostream>
15#include <memory>
16#include <string>
17#include <thread>
18#include <vector>
19#include "../common/Logger/Logger.hpp"
22#include "Core/EventBus/EventBus.hpp"
24#include "Events/NetworkEvent/NetworkEvent.hpp"
25#include "IHost.hpp"
26#include "IPeer.hpp"
27#include "NetworkFactory.hpp"
28#include "ThreadSafeQueue.hpp"
29
71 public:
82 explicit Replicator(EventBus &eventBus, bool isSpectator = false);
83
90
104 bool connect(const std::string &host, uint16_t port);
105
112 void disconnect();
113
119 bool isConnected() const;
120
126 bool isAuthenticated() const;
127
133 uint32_t getMyPlayerId() const { return _myPlayerId.load(); }
134
149 void processMessages();
150
160 void startNetworkThread();
161
169 void stopNetworkThread();
170
182 void sendPacket(NetworkMessageType type, const std::vector<uint8_t> &data);
183
194 uint32_t getLatency() const;
195
206 uint32_t getPacketLoss() const;
207
213 bool isSpectator() const;
214
221 bool sendConnectRequest(const std::string &playerName, const std::string &username,
222 const std::string &password);
223
231 bool sendRegisterAccount(const std::string &username, const std::string &password);
232
240 bool sendLoginAccount(const std::string &username, const std::string &password);
241
247 bool sendListRooms();
248
258 bool sendCreateRoom(const std::string &roomName, uint32_t maxPlayers, bool isPrivate,
259 float gameSpeedMultiplier = 1.0f);
260
267 bool sendJoinRoom(const std::string &roomId);
268
275 bool sendAutoMatchmaking();
276
285 bool updateAutoMatchmakingPreference(bool enabled);
286
293 bool sendStartGame();
294
299 bool sendRequestRoomList();
300
305 bool sendLeaveRoom();
306
316 bool sendChatMessage(const std::string &message);
317
327
328 private:
337 void networkThreadLoop(std::stop_token stopToken);
338
345 void onInputEvent(const InputEvent &event);
346 void processIncomingPacket(const std::vector<uint8_t> &packet);
347
349 std::atomic<bool> _connected{false};
350 std::atomic<bool> _authenticated{false};
351 std::atomic<uint32_t> _myPlayerId{0};
352 std::string _lastLoginUsername; // Track username for AUTH_SUCCESS event
353 std::atomic<bool> _autoMatchmakingPreference{false}; // User's auto-matchmaking preference from server
355 std::string _serverHost;
356 uint16_t _serverPort = 0;
357
358 std::atomic<uint32_t> _latency{0};
359 uint32_t _packetLoss = 0;
360
361 std::unique_ptr<IHost> _host;
362 IPeer *_serverPeer = nullptr;
363
364 // Multi-threading components
365 std::jthread _networkThread;
367
368 // Smoothed ping calculation (exponential moving average)
369 static constexpr float PING_SMOOTHING_FACTOR = 0.3f; // 30% new, 70% old
370 std::atomic<float> _smoothedLatency{0.0f};
371};
372
373#endif
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
Interface representing a remote peer in the network.
Definition IPeer.hpp:40
Event representing a player input action.
Client-server network replication manager with dedicated network thread.
bool isConnected() const
Check if connected to server.
bool _isSpectator
uint16_t _serverPort
bool sendStartGame()
Send start game request to server.
bool sendLoginAccount(const std::string &username, const std::string &password)
Send login request to server.
bool sendListRooms()
Send list rooms request to server.
bool sendAutoMatchmaking()
Send auto-matchmaking request to server.
bool connect(const std::string &host, uint16_t port)
Connect to the game server.
void startNetworkThread()
Start the dedicated network thread.
void processMessages()
Process incoming network messages.
bool getAutoMatchmakingPreference() const
Get the user's auto-matchmaking preference from server.
std::unique_ptr< IHost > _host
ThreadSafeQueue< NetworkEvent > _incomingMessages
Queue for messages from network thread.
void disconnect()
Disconnect from the server.
std::atomic< bool > _autoMatchmakingPreference
void networkThreadLoop(std::stop_token stopToken)
Network thread main loop.
EventBus & _eventBus
uint32_t getPacketLoss() const
Get packet loss rate as percentage.
void onInputEvent(const InputEvent &event)
Handle an incoming packet from the network.
void processIncomingPacket(const std::vector< uint8_t > &packet)
bool sendRequestRoomList()
Request the list of available rooms from server.
std::jthread _networkThread
Dedicated network thread.
bool isSpectator() const
Check if in spectator mode.
uint32_t getMyPlayerId() const
Get the player ID assigned by server.
void sendPacket(NetworkMessageType type, const std::vector< uint8_t > &data)
Send a packet to the server.
static constexpr float PING_SMOOTHING_FACTOR
bool sendRegisterAccount(const std::string &username, const std::string &password)
Send register account request to server.
uint32_t _packetLoss
std::atomic< uint32_t > _myPlayerId
bool sendLeaveRoom()
Send request to leave current room.
bool updateAutoMatchmakingPreference(bool enabled)
Update auto-matchmaking preference on server.
uint32_t getLatency() const
Get current latency in milliseconds.
~Replicator()
Destructor.
std::atomic< float > _smoothedLatency
IPeer * _serverPeer
bool sendCreateRoom(const std::string &roomName, uint32_t maxPlayers, bool isPrivate, float gameSpeedMultiplier=1.0f)
Send create room request to server.
std::string _lastLoginUsername
void stopNetworkThread()
Stop the dedicated network thread.
std::string _serverHost
std::atomic< bool > _authenticated
bool isAuthenticated() const
Check if authenticated with server.
bool sendConnectRequest(const std::string &playerName, const std::string &username, const std::string &password)
Send connect request to server with player name.
bool sendJoinRoom(const std::string &roomId)
Send join room request to server.
std::atomic< bool > _connected
bool sendChatMessage(const std::string &message)
Send chat message to server.
std::atomic< uint32_t > _latency
Thread-safe queue for inter-thread communication.
NetworkMessageType
Types of network messages exchanged between client and server.