Pyrogenesis  trunk
NetServerTurnManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 <map>
22 #include "ps/CStr.h"
23 
24 class CNetServerWorker;
25 
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.
30  *
31  * Thread-safety:
32  * - This is constructed and used by CNetServerWorker in the network server thread.
33  */
35 {
37 public:
39 
40  void NotifyFinishedClientCommands(int client, u32 turn);
41 
42  void NotifyFinishedClientUpdate(int client, const CStrW& playername, u32 turn, const CStr& hash);
43 
44  /**
45  * Inform the turn manager of a new client who will be sending commands.
46  */
47  void InitialiseClient(int client, u32 turn);
48 
49  /**
50  * Inform the turn manager that a previously-initialised client has left the game
51  * and will no longer be sending commands.
52  */
53  void UninitialiseClient(int client);
54 
55  void SetTurnLength(u32 msecs);
56 
57  /**
58  * Returns the latest turn for which all clients are ready;
59  * they will have already been told to execute this turn.
60  */
62 
63  /**
64  * Returns the turn length that was used for the given turn.
65  * Requires turn <= GetReadyTurn().
66  */
68 
69 private:
70  void CheckClientsReady();
71 
72  /// The latest turn for which we have received all commands from all clients
74 
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;
77 
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;
81 
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;
84 
85  // Map of client ID -> playername
86  std::map<u32, CStrW> m_ClientPlayernames;
87 
88  // Current turn length
90 
91  // Turn lengths for all previously executed turns
92  std::vector<u32> m_SavedTurnLengths;
93 
95 
97 };
98 
99 #endif // INCLUDED_NETSERVERTURNMANAGER
std::map< u32, std::map< int, std::string > > m_ClientStateHashes
Definition: NetServerTurnManager.h:83
std::vector< u32 > m_SavedTurnLengths
Definition: NetServerTurnManager.h:92
NONCOPYABLE(CNetServerTurnManager)
void UninitialiseClient(int client)
Inform the turn manager that a previously-initialised client has left the game and will no longer be ...
Definition: NetServerTurnManager.cpp:151
std::map< u32, CStrW > m_ClientPlayernames
Definition: NetServerTurnManager.h:86
u32 GetSavedTurnLength(u32 turn)
Returns the turn length that was used for the given turn.
Definition: NetServerTurnManager.cpp:169
u32 m_TurnLength
Definition: NetServerTurnManager.h:89
uint32_t u32
Definition: types.h:39
void SetTurnLength(u32 msecs)
Definition: NetServerTurnManager.cpp:164
bool m_HasSyncError
Definition: NetServerTurnManager.h:96
void NotifyFinishedClientUpdate(int client, const CStrW &playername, u32 turn, const CStr &hash)
Definition: NetServerTurnManager.cpp:80
u32 m_ReadyTurn
The latest turn for which we have received all commands from all clients.
Definition: NetServerTurnManager.h:73
void InitialiseClient(int client, u32 turn)
Inform the turn manager of a new client who will be sending commands.
Definition: NetServerTurnManager.cpp:142
void NotifyFinishedClientCommands(int client, u32 turn)
Definition: NetServerTurnManager.cpp:41
std::map< int, u32 > m_ClientsReady
Definition: NetServerTurnManager.h:76
CNetServerTurnManager(CNetServerWorker &server)
Definition: NetServerTurnManager.cpp:32
CNetServerWorker & m_NetServer
Definition: NetServerTurnManager.h:94
std::map< int, u32 > m_ClientsSimulated
Definition: NetServerTurnManager.h:80
void CheckClientsReady()
Definition: NetServerTurnManager.cpp:56
u32 GetReadyTurn()
Returns the latest turn for which all clients are ready; they will have already been told to execute ...
Definition: NetServerTurnManager.h:61
Network server worker thread.
Definition: NetServer.h:153
The server-side counterpart to CNetClientTurnManager.
Definition: NetServerTurnManager.h:34