Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetServerTurnManager.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_NETSERVERTURNMANAGER
19#define INCLUDED_NETSERVERTURNMANAGER
20
21#include "lib/code_annotation.h"
22#include "ps/CStr.h"
24
25#include <map>
26#include <string>
27#include <unordered_map>
28#include <vector>
29
32
33/**
34 * The server-side counterpart to CNetClientTurnManager.
35 * Records the turn state of each client, and sends turn advancement messages
36 * when clients are ready.
37 *
38 * Thread-safety:
39 * - This is constructed and used by CNetServerWorker in the network server thread.
40 */
42{
44public:
46
48
49 void NotifyFinishedClientUpdate(CNetServerSession& session, turn_id_t turn, const CStr& hash);
50
51 /**
52 * Inform the turn manager of a new client
53 * @param observer - whether this client is an observer.
54 */
55 void InitialiseClient(int client, turn_id_t turn, bool observer);
56
57 /**
58 * Inform the turn manager that a previously-initialised client has left the game.
59 */
60 void UninitialiseClient(int client);
61
62 void SetTurnLength(std::uint32_t msecs);
63
64 /**
65 * Returns the latest turn for which all clients are ready;
66 * they will have already been told to execute this turn.
67 */
69
70 /**
71 * Returns the turn length that was used for the given turn.
72 * Requires turn <= GetReadyTurn().
73 */
74 std::uint32_t GetSavedTurnLength(turn_id_t turn);
75
76private:
77 void CheckClientsReady();
78
79 struct Client
80 {
82 // Latest turn for which all commands have been received.
84 // Last known simulated turn.
87 bool isOOS = false;
88 };
89
90 std::unordered_map<int, Client> m_ClientsData;
91
92 // Cached value - is any client OOS? This is reset when the OOS client leaves.
93 bool m_HasSyncError = false;
94
95 // Map of turn -> {Client ID -> state hash}; old indexes <= min(m_ClientsSimulated) are deleted
96 std::map<turn_id_t, std::map<int, std::string>> m_ClientStateHashes;
97
98 /// The latest turn for which we have received all commands from all clients
100
101 // Current turn length
102 std::uint32_t m_TurnLength;
103
104 // Turn lengths for all previously executed turns
105 std::vector<std::uint32_t> m_SavedTurnLengths;
106
108};
109
110#endif // INCLUDED_NETSERVERTURNMANAGER
std::int32_t turn_id_t
At 1000 turns/second, a 32-bit counter would overflow in ~49 days.
Definition: SimulationCommand.h:31
The server's end of a network session.
Definition: NetServerSession.h:39
The server-side counterpart to CNetClientTurnManager.
Definition: NetServerTurnManager.h:42
bool m_HasSyncError
Definition: NetServerTurnManager.h:93
void CheckClientsReady()
Definition: NetServerTurnManager.cpp:80
std::vector< std::uint32_t > m_SavedTurnLengths
Definition: NetServerTurnManager.h:105
std::unordered_map< int, Client > m_ClientsData
Definition: NetServerTurnManager.h:90
void SetTurnLength(std::uint32_t msecs)
Definition: NetServerTurnManager.cpp:222
turn_id_t GetReadyTurn()
Returns the latest turn for which all clients are ready; they will have already been told to execute ...
Definition: NetServerTurnManager.h:68
void NotifyFinishedClientCommands(CNetServerSession &session, turn_id_t turn)
Definition: NetServerTurnManager.cpp:54
CNetServerWorker & m_NetServer
Definition: NetServerTurnManager.h:107
void InitialiseClient(int client, turn_id_t turn, bool observer)
Inform the turn manager of a new client.
Definition: NetServerTurnManager.cpp:186
void UninitialiseClient(int client)
Inform the turn manager that a previously-initialised client has left the game.
Definition: NetServerTurnManager.cpp:197
std::uint32_t GetSavedTurnLength(turn_id_t turn)
Returns the turn length that was used for the given turn.
Definition: NetServerTurnManager.cpp:227
std::uint32_t m_TurnLength
Definition: NetServerTurnManager.h:102
NONCOPYABLE(CNetServerTurnManager)
void NotifyFinishedClientUpdate(CNetServerSession &session, turn_id_t turn, const CStr &hash)
Definition: NetServerTurnManager.cpp:111
turn_id_t m_ReadyTurn
The latest turn for which we have received all commands from all clients.
Definition: NetServerTurnManager.h:99
CNetServerTurnManager(CNetServerWorker &server)
Definition: NetServerTurnManager.cpp:43
std::map< turn_id_t, std::map< int, std::string > > m_ClientStateHashes
Definition: NetServerTurnManager.h:96
Network server worker thread.
Definition: NetServer.h:113
Definition: NetServerTurnManager.h:80
turn_id_t readyTurn
Definition: NetServerTurnManager.h:83
bool isObserver
Definition: NetServerTurnManager.h:86
CStrW playerName
Definition: NetServerTurnManager.h:81
turn_id_t simulatedTurn
Definition: NetServerTurnManager.h:85
bool isOOS
Definition: NetServerTurnManager.h:87