Merge 'remotes/trunk'
[0ad.git] / source / network / NetServerTurnManager.h
blob670e830f21ed6a4187212f1acfa368ac1155648f
1 /* Copyright (C) 2017 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_NETSERVERTURNMANAGER
19 #define INCLUDED_NETSERVERTURNMANAGER
21 #include <map>
22 #include "ps/CStr.h"
24 class CNetServerWorker;
26 /**
27 * The server-side counterpart to CNetClientTurnManager.
28 * Records the turn state of each client, and sends turn advancement messages
29 * when all clients are ready.
31 * Thread-safety:
32 * - This is constructed and used by CNetServerWorker in the network server thread.
34 class CNetServerTurnManager
36 NONCOPYABLE(CNetServerTurnManager);
37 public:
38 CNetServerTurnManager(CNetServerWorker& server);
40 void NotifyFinishedClientCommands(int client, u32 turn);
42 void NotifyFinishedClientUpdate(int client, const CStrW& playername, u32 turn, const CStr& hash);
44 /**
45 * Inform the turn manager of a new client who will be sending commands.
47 void InitialiseClient(int client, u32 turn);
49 /**
50 * Inform the turn manager that a previously-initialised client has left the game
51 * and will no longer be sending commands.
53 void UninitialiseClient(int client);
55 void SetTurnLength(u32 msecs);
57 /**
58 * Returns the latest turn for which all clients are ready;
59 * they will have already been told to execute this turn.
61 u32 GetReadyTurn() { return m_ReadyTurn; }
63 /**
64 * Returns the turn length that was used for the given turn.
65 * Requires turn <= GetReadyTurn().
67 u32 GetSavedTurnLength(u32 turn);
69 private:
70 void CheckClientsReady();
72 /// The latest turn for which we have received all commands from all clients
73 u32 m_ReadyTurn;
75 // Client ID -> ready turn number (the latest turn for which all commands have been received from that client)
76 std::map<int, u32> m_ClientsReady;
78 // Client ID -> last known simulated turn number (for which we have the state hash)
79 // (the client has reached the start of this turn, not done the update for it yet)
80 std::map<int, u32> m_ClientsSimulated;
82 // Map of turn -> {Client ID -> state hash}; old indexes <= min(m_ClientsSimulated) are deleted
83 std::map<u32, std::map<int, std::string>> m_ClientStateHashes;
85 // Map of client ID -> playername
86 std::map<u32, CStrW> m_ClientPlayernames;
88 // Current turn length
89 u32 m_TurnLength;
91 // Turn lengths for all previously executed turns
92 std::vector<u32> m_SavedTurnLengths;
94 CNetServerWorker& m_NetServer;
96 bool m_HasSyncError;
99 #endif // INCLUDED_NETSERVERTURNMANAGER