Line data Source code
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" 23 : #include "simulation2/helpers/SimulationCommand.h" 24 : 25 : #include <map> 26 : #include <string> 27 : #include <unordered_map> 28 : #include <vector> 29 : 30 : class CNetServerSession; 31 : class CNetServerWorker; 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 : */ 41 : class CNetServerTurnManager 42 : { 43 : NONCOPYABLE(CNetServerTurnManager); 44 : public: 45 : CNetServerTurnManager(CNetServerWorker& server); 46 : 47 : void NotifyFinishedClientCommands(CNetServerSession& session, turn_id_t turn); 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 : */ 68 0 : turn_id_t GetReadyTurn() { return m_ReadyTurn; } 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 : 76 : private: 77 : void CheckClientsReady(); 78 : 79 0 : struct Client 80 : { 81 : CStrW playerName; 82 : // Latest turn for which all commands have been received. 83 : turn_id_t readyTurn; 84 : // Last known simulated turn. 85 : turn_id_t simulatedTurn; 86 : bool isObserver; 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 99 : turn_id_t m_ReadyTurn; 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 : 107 : CNetServerWorker& m_NetServer; 108 : }; 109 : 110 : #endif // INCLUDED_NETSERVERTURNMANAGER