|
R-Type
Distributed multiplayer game engine in C++
|
Client-server network replication manager with dedicated network thread. More...
#include <Replicator.hpp>

Public Member Functions | |
| Replicator (EventBus &eventBus, bool isSpectator=false) | |
| Constructor with EventBus reference. | |
| ~Replicator () | |
| Destructor. | |
| bool | connect (const std::string &host, uint16_t port) |
| Connect to the game server. | |
| void | disconnect () |
| Disconnect from the server. | |
| bool | isConnected () const |
| Check if connected to server. | |
| bool | isAuthenticated () const |
| Check if authenticated with server. | |
| uint32_t | getMyPlayerId () const |
| Get the player ID assigned by server. | |
| void | processMessages () |
| Process incoming network messages. | |
| void | startNetworkThread () |
| Start the dedicated network thread. | |
| void | stopNetworkThread () |
| Stop the dedicated network thread. | |
| void | sendPacket (NetworkMessageType type, const std::vector< uint8_t > &data) |
| Send a packet to the server. | |
| uint32_t | getLatency () const |
| Get current latency in milliseconds. | |
| uint32_t | getPacketLoss () const |
| Get packet loss rate as percentage. | |
| bool | isSpectator () const |
| Check if in spectator mode. | |
| bool | sendConnectRequest (const std::string &playerName, const std::string &username, const std::string &password) |
| Send connect request to server with player name. | |
| bool | sendRegisterAccount (const std::string &username, const std::string &password) |
| Send register account 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 | sendCreateRoom (const std::string &roomName, uint32_t maxPlayers, bool isPrivate, float gameSpeedMultiplier=1.0f) |
| Send create room request to server. | |
| bool | sendJoinRoom (const std::string &roomId) |
| Send join room request to server. | |
| bool | sendAutoMatchmaking () |
| Send auto-matchmaking request to server. | |
| bool | updateAutoMatchmakingPreference (bool enabled) |
| Update auto-matchmaking preference on server. | |
| bool | sendStartGame () |
| Send start game request to server. | |
| bool | sendRequestRoomList () |
| Request the list of available rooms from server. | |
| bool | sendLeaveRoom () |
| Send request to leave current room. | |
| bool | sendChatMessage (const std::string &message) |
| Send chat message to server. | |
| bool | getAutoMatchmakingPreference () const |
| Get the user's auto-matchmaking preference from server. | |
Private Member Functions | |
| void | networkThreadLoop (std::stop_token stopToken) |
| Network thread main loop. | |
| void | onInputEvent (const InputEvent &event) |
| Handle an incoming packet from the network. | |
| void | processIncomingPacket (const std::vector< uint8_t > &packet) |
Private Attributes | |
| EventBus & | _eventBus |
| std::atomic< bool > | _connected {false} |
| std::atomic< bool > | _authenticated {false} |
| std::atomic< uint32_t > | _myPlayerId {0} |
| std::string | _lastLoginUsername |
| std::atomic< bool > | _autoMatchmakingPreference {false} |
| bool | _isSpectator |
| std::string | _serverHost |
| uint16_t | _serverPort = 0 |
| std::atomic< uint32_t > | _latency {0} |
| uint32_t | _packetLoss = 0 |
| std::unique_ptr< IHost > | _host |
| IPeer * | _serverPeer = nullptr |
| std::jthread | _networkThread |
| Dedicated network thread. | |
| ThreadSafeQueue< NetworkEvent > | _incomingMessages |
| Queue for messages from network thread. | |
| std::atomic< float > | _smoothedLatency {0.0f} |
Static Private Attributes | |
| static constexpr float | PING_SMOOTHING_FACTOR = 0.3f |
Client-server network replication manager with dedicated network thread.
The Replicator is responsible for:
Multi-threading architecture:
Communication flow:
EventBus integration:
Definition at line 70 of file Replicator.hpp.
|
explicit |
Constructor with EventBus reference.
Initializes the Replicator and subscribes to necessary events.
| eventBus | Event bus for inter-component communication |
| isSpectator | Whether this client is in spectator mode (read-only) |
Definition at line 13 of file Replicator.cpp.
| Replicator::~Replicator | ( | ) |
Destructor.
Properly disconnects from server and frees network resources.
Definition at line 15 of file Replicator.cpp.
References disconnect().

| bool Replicator::connect | ( | const std::string & | host, |
| uint16_t | port | ||
| ) |
Connect to the game server.
Establishes a UDP/TCP connection with the specified server. Publishes a ConnectionEvent based on the result.
Definition at line 19 of file Replicator.cpp.
References _connected, _host, _serverHost, _serverPeer, _serverPort, createAddress(), and startNetworkThread().

| void Replicator::disconnect | ( | ) |
Disconnect from the server.
Closes the connection cleanly and cleans up resources. Publishes a ConnectionEvent::DISCONNECTED.
Definition at line 45 of file Replicator.cpp.
References _authenticated, _connected, _serverPeer, CONNECTED, CONNECTING, CONNECTION_SUCCEEDED, IPeer::disconnectNow(), IPeer::getState(), and stopNetworkThread().
Referenced by ~Replicator().

|
inline |
Get the user's auto-matchmaking preference from server.
Returns the auto-matchmaking preference loaded from the server after login. This preference is persisted in the user's account on the server.
Definition at line 326 of file Replicator.hpp.
References _autoMatchmakingPreference.
| uint32_t Replicator::getLatency | ( | ) | const |
Get current latency in milliseconds.
Calculates average RTT (Round-Trip Time) based on PING/PONG.
Definition at line 541 of file Replicator.cpp.
References _latency.
Referenced by GameLoop::update().
|
inline |
Get the player ID assigned by server.
Definition at line 133 of file Replicator.hpp.
References _myPlayerId.
Referenced by GameLoop::run().
| uint32_t Replicator::getPacketLoss | ( | ) | const |
Get packet loss rate as percentage.
Calculates the ratio of lost packets over the last 100 sent.
Definition at line 545 of file Replicator.cpp.
References _packetLoss.
| bool Replicator::isAuthenticated | ( | ) | const |
Check if authenticated with server.
Definition at line 67 of file Replicator.cpp.
References _authenticated.
| bool Replicator::isConnected | ( | ) | const |
Check if connected to server.
Definition at line 63 of file Replicator.cpp.
References _connected.
| bool Replicator::isSpectator | ( | ) | const |
Check if in spectator mode.
Definition at line 549 of file Replicator.cpp.
References _isSpectator.
Referenced by GameLoop::processInput().
|
private |
Network thread main loop.
Continuously polls the network for incoming packets. Runs in a dedicated thread until stop is requested.
| stopToken | Token to check for stop requests |
Definition at line 89 of file Replicator.cpp.
References _authenticated, _autoMatchmakingPreference, _connected, _eventBus, _host, _incomingMessages, _lastLoginUsername, _latency, _myPlayerId, _serverPeer, _smoothedLatency, APPLY_AUTO_MATCHMAKING_PREF, AUTH_SUCCESS, CONNECT, RType::Messages::Connection::HandshakeResponse::deserialize(), RType::Messages::S2C::LoginResponse::deserialize(), RType::Messages::S2C::RegisterResponse::deserialize(), DISCONNECT, NetworkMessages::getMessageType(), NetworkMessages::getPayload(), IPeer::getRoundTripTime(), NetworkMessages::HANDSHAKE_RESPONSE, LOG_DEBUG, LOG_ERROR, LOG_INFO, LOG_WARNING, LOGIN_FAILED, NetworkMessages::LOGIN_RESPONSE, PING_SMOOTHING_FACTOR, EventBus::publish(), ThreadSafeQueue< T >::push(), RECEIVE, REGISTER_FAILED, NetworkMessages::REGISTER_RESPONSE, REGISTER_SUCCESS, NetworkMessages::S2C_GAME_START, NetworkMessages::S2C_GAME_STATE, NetworkEvent::setMessageContent(), and WORLD_STATE.
Referenced by startNetworkThread().

|
private |
Handle an incoming packet from the network.
| packet | Raw packet data |
Decodes the packet and creates a NetworkEvent.
Definition at line 610 of file Replicator.cpp.
|
private |
Definition at line 615 of file Replicator.cpp.
| void Replicator::processMessages | ( | ) |
Process incoming network messages.
Processes messages from the network thread queue and publishes them on the EventBus. Must be called every frame from the game thread.
Actions performed:
Definition at line 250 of file Replicator.cpp.
References _eventBus, _incomingMessages, NetworkMessages::getMessageType(), NetworkMessages::getPayload(), LOG_DEBUG, LOG_ERROR, LOG_INFO, EventBus::publish(), NetworkMessages::S2C_GAME_START, NetworkMessages::S2C_GAME_STATE, NetworkMessages::S2C_ROOM_LIST, NetworkMessages::S2C_ROOM_STATE, and ThreadSafeQueue< T >::tryPop().
Referenced by GameLoop::run().

| bool Replicator::sendAutoMatchmaking | ( | ) |
Send auto-matchmaking request to server.
Automatically joins the first available room or creates a new one.
Definition at line 472 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_AUTO_MATCHMAKING, NetworkMessages::createMessage(), createPacket(), LOG_ERROR, LOG_INFO, RELIABLE, IPeer::send(), and RType::Messages::C2S::AutoMatchmaking::serialize().
Referenced by GameLoop::handleUIEvent().

| bool Replicator::sendChatMessage | ( | const std::string & | message | ) |
Send chat message to server.
Sends a chat message to be broadcast to other players in the room. Messages starting with "/" are treated as commands.
| message | The message text to send |
Definition at line 579 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_CHAT_MESSAGE, NetworkMessages::createMessage(), createPacket(), LOG_DEBUG, LOG_ERROR, LOG_INFO, RELIABLE, IPeer::send(), and RType::Messages::C2S::C2SChatMessage::serialize().
Referenced by GameLoop::run().

| bool Replicator::sendConnectRequest | ( | const std::string & | playerName, |
| const std::string & | username, | ||
| const std::string & | password | ||
| ) |
Send connect request to server with player name.
| playerName | The player's name/pseudo |
Definition at line 339 of file Replicator.cpp.
References _connected, _serverPeer, ConnectionMessages::HandshakeRequestData::clientVersion, NetworkMessages::createMessage(), createPacket(), NetworkMessages::HANDSHAKE_REQUEST, ConnectionMessages::HandshakeRequestData::password, ConnectionMessages::HandshakeRequestData::playerName, RELIABLE, IPeer::send(), ConnectionMessages::HandshakeRequestData::timestamp, and ConnectionMessages::HandshakeRequestData::username.

| bool Replicator::sendCreateRoom | ( | const std::string & | roomName, |
| uint32_t | maxPlayers, | ||
| bool | isPrivate, | ||
| float | gameSpeedMultiplier = 1.0f |
||
| ) |
Send create room request to server.
| roomName | Name of the room to create |
| maxPlayers | Maximum number of players |
| isPrivate | Whether the room is private |
| gameSpeedMultiplier | Game speed multiplier (0.25 to 1.0, default 1.0) |
Definition at line 430 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_CREATE_ROOM, NetworkMessages::createMessage(), createPacket(), RELIABLE, IPeer::send(), and RType::Messages::C2S::CreateRoom::serialize().
Referenced by GameLoop::handleUIEvent().

| bool Replicator::sendJoinRoom | ( | const std::string & | roomId | ) |
Send join room request to server.
| roomId | ID of the room to join |
Definition at line 450 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_JOIN_ROOM, NetworkMessages::createMessage(), createPacket(), LOG_ERROR, LOG_INFO, RELIABLE, IPeer::send(), and RType::Messages::C2S::JoinRoom::serialize().
Referenced by GameLoop::handleUIEvent().

| bool Replicator::sendLeaveRoom | ( | ) |
Send request to leave current room.
Definition at line 558 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_LEAVE_ROOM, NetworkMessages::createMessage(), createPacket(), LOG_INFO, RELIABLE, IPeer::send(), and RType::Messages::C2S::LeaveRoom::serialize().
Referenced by GameLoop::handleUIEvent().

| bool Replicator::sendListRooms | ( | ) |
Send list rooms request to server.
Definition at line 411 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_LIST_ROOMS, NetworkMessages::createMessage(), createPacket(), RELIABLE, IPeer::send(), and RType::Messages::C2S::ListRooms::serialize().
Referenced by sendRequestRoomList().

| bool Replicator::sendLoginAccount | ( | const std::string & | username, |
| const std::string & | password | ||
| ) |
Send login request to server.
| username | Account username |
| password | Account password |
Definition at line 388 of file Replicator.cpp.
References _connected, _lastLoginUsername, _serverPeer, NetworkMessages::createMessage(), createPacket(), LOG_ERROR, NetworkMessages::LOGIN_REQUEST, RELIABLE, IPeer::send(), and RType::Messages::C2S::LoginAccount::serialize().
Referenced by GameLoop::handleUIEvent().

| void Replicator::sendPacket | ( | NetworkMessageType | type, |
| const std::vector< uint8_t > & | data | ||
| ) |
Send a packet to the server.
Serializes and sends a message to the server asynchronously.
| type | Message type (PLAYER_INPUT, PING, etc.) |
| data | Packet data (serialized) |
Definition at line 329 of file Replicator.cpp.
References _connected, _serverPeer, createPacket(), RELIABLE, and IPeer::send().
Referenced by GameLoop::processInput().

| bool Replicator::sendRegisterAccount | ( | const std::string & | username, |
| const std::string & | password | ||
| ) |
Send register account request to server.
| username | Username for new account |
| password | Password for new account |
Definition at line 367 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::createMessage(), createPacket(), LOG_ERROR, NetworkMessages::REGISTER_REQUEST, RELIABLE, IPeer::send(), and RType::Messages::C2S::RegisterAccount::serialize().
Referenced by GameLoop::handleUIEvent().

| bool Replicator::sendRequestRoomList | ( | ) |
Request the list of available rooms from server.
Definition at line 553 of file Replicator.cpp.
References LOG_INFO, and sendListRooms().
Referenced by GameLoop::handleUIEvent().

| bool Replicator::sendStartGame | ( | ) |
Send start game request to server.
Only the host of the room can start the game.
Definition at line 519 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_START_GAME, NetworkMessages::createMessage(), createPacket(), LOG_ERROR, LOG_INFO, RELIABLE, IPeer::send(), and RType::Messages::C2S::StartGame::serialize().
Referenced by GameLoop::handleUIEvent().

| void Replicator::startNetworkThread | ( | ) |
Start the dedicated network thread.
Launches a background thread that continuously polls the network for incoming packets and pushes them to the message queue.
Definition at line 71 of file Replicator.cpp.
References _networkThread, and networkThreadLoop().
Referenced by connect().

| void Replicator::stopNetworkThread | ( | ) |
Stop the dedicated network thread.
Signals the network thread to stop and waits for it to join.
Definition at line 81 of file Replicator.cpp.
References _networkThread.
Referenced by disconnect().
| bool Replicator::updateAutoMatchmakingPreference | ( | bool | enabled | ) |
Update auto-matchmaking preference on server.
Sends the user's auto-matchmaking preference to be saved on the server. Does not trigger immediate matchmaking.
| enabled | Whether auto-matchmaking should be enabled |
Definition at line 495 of file Replicator.cpp.
References _connected, _serverPeer, NetworkMessages::C2S_UPDATE_AUTO_MM_PREF, NetworkMessages::createMessage(), createPacket(), LOG_ERROR, LOG_INFO, RELIABLE, IPeer::send(), and RType::Messages::C2S::AutoMatchmaking::serialize().
Referenced by GameLoop::handleUIEvent().

|
private |
Definition at line 350 of file Replicator.hpp.
Referenced by disconnect(), isAuthenticated(), and networkThreadLoop().
|
private |
Definition at line 353 of file Replicator.hpp.
Referenced by getAutoMatchmakingPreference(), and networkThreadLoop().
|
private |
Definition at line 349 of file Replicator.hpp.
Referenced by connect(), disconnect(), isConnected(), networkThreadLoop(), sendAutoMatchmaking(), sendChatMessage(), sendConnectRequest(), sendCreateRoom(), sendJoinRoom(), sendLeaveRoom(), sendListRooms(), sendLoginAccount(), sendPacket(), sendRegisterAccount(), sendStartGame(), and updateAutoMatchmakingPreference().
|
private |
Definition at line 348 of file Replicator.hpp.
Referenced by networkThreadLoop(), and processMessages().
|
private |
Definition at line 361 of file Replicator.hpp.
Referenced by connect(), and networkThreadLoop().
|
private |
Queue for messages from network thread.
Definition at line 366 of file Replicator.hpp.
Referenced by networkThreadLoop(), and processMessages().
|
private |
Definition at line 354 of file Replicator.hpp.
Referenced by isSpectator().
|
private |
Definition at line 352 of file Replicator.hpp.
Referenced by networkThreadLoop(), and sendLoginAccount().
|
private |
Definition at line 358 of file Replicator.hpp.
Referenced by getLatency(), and networkThreadLoop().
|
private |
Definition at line 351 of file Replicator.hpp.
Referenced by getMyPlayerId(), and networkThreadLoop().
|
private |
Dedicated network thread.
Definition at line 365 of file Replicator.hpp.
Referenced by startNetworkThread(), and stopNetworkThread().
|
private |
Definition at line 359 of file Replicator.hpp.
Referenced by getPacketLoss().
|
private |
Definition at line 355 of file Replicator.hpp.
Referenced by connect().
|
private |
Definition at line 362 of file Replicator.hpp.
Referenced by connect(), disconnect(), networkThreadLoop(), sendAutoMatchmaking(), sendChatMessage(), sendConnectRequest(), sendCreateRoom(), sendJoinRoom(), sendLeaveRoom(), sendListRooms(), sendLoginAccount(), sendPacket(), sendRegisterAccount(), sendStartGame(), and updateAutoMatchmakingPreference().
|
private |
Definition at line 356 of file Replicator.hpp.
Referenced by connect().
|
private |
Definition at line 370 of file Replicator.hpp.
Referenced by networkThreadLoop().
|
staticconstexprprivate |
Definition at line 369 of file Replicator.hpp.
Referenced by networkThreadLoop().