Pyrogenesis  trunk
NetServerTurnManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 "ps/CStr.h"
22 
23 #include <map>
24 #include <unordered_map>
25 
26 class CNetServerWorker;
27 class CNetServerSession;
28 
29 /**
30  * The server-side counterpart to CNetClientTurnManager.
31  * Records the turn state of each client, and sends turn advancement messages
32  * when clients are ready.
33  *
34  * Thread-safety:
35  * - This is constructed and used by CNetServerWorker in the network server thread.
36  */
38 {
40 public:
42 
44 
45  void NotifyFinishedClientUpdate(CNetServerSession& session, u32 turn, const CStr& hash);
46 
47  /**
48  * Inform the turn manager of a new client
49  * @param observer - whether this client is an observer.
50  */
51  void InitialiseClient(int client, u32 turn, bool observer);
52 
53  /**
54  * Inform the turn manager that a previously-initialised client has left the game.
55  */
56  void UninitialiseClient(int client);
57 
58  void SetTurnLength(u32 msecs);
59 
60  /**
61  * Returns the latest turn for which all clients are ready;
62  * they will have already been told to execute this turn.
63  */
65 
66  /**
67  * Returns the turn length that was used for the given turn.
68  * Requires turn <= GetReadyTurn().
69  */
71 
72 private:
73  void CheckClientsReady();
74 
75  struct Client
76  {
77  CStrW playerName;
78  // Latest turn for which all commands have been received.
80  // Last known simulated turn.
82  bool isObserver;
83  bool isOOS = false;
84  };
85 
86  std::unordered_map<int, Client> m_ClientsData;
87 
88  // Cached value - is any client OOS? This is reset when the OOS client leaves.
89  bool m_HasSyncError = false;
90 
91  // Map of turn -> {Client ID -> state hash}; old indexes <= min(m_ClientsSimulated) are deleted
92  std::map<u32, std::map<int, std::string>> m_ClientStateHashes;
93 
94  /// The latest turn for which we have received all commands from all clients
96 
97  // Current turn length
99 
100  // Turn lengths for all previously executed turns
101  std::vector<u32> m_SavedTurnLengths;
102 
104 };
105 
106 #endif // INCLUDED_NETSERVERTURNMANAGER
bool isObserver
Definition: NetServerTurnManager.h:82
std::map< u32, std::map< int, std::string > > m_ClientStateHashes
Definition: NetServerTurnManager.h:92
std::vector< u32 > m_SavedTurnLengths
Definition: NetServerTurnManager.h:101
NONCOPYABLE(CNetServerTurnManager)
void UninitialiseClient(int client)
Inform the turn manager that a previously-initialised client has left the game.
Definition: NetServerTurnManager.cpp:195
Definition: NetServerTurnManager.h:75
u32 GetSavedTurnLength(u32 turn)
Returns the turn length that was used for the given turn.
Definition: NetServerTurnManager.cpp:222
u32 m_TurnLength
Definition: NetServerTurnManager.h:98
void InitialiseClient(int client, u32 turn, bool observer)
Inform the turn manager of a new client.
Definition: NetServerTurnManager.cpp:184
uint32_t u32
Definition: types.h:39
void SetTurnLength(u32 msecs)
Definition: NetServerTurnManager.cpp:217
bool m_HasSyncError
Definition: NetServerTurnManager.h:89
u32 m_ReadyTurn
The latest turn for which we have received all commands from all clients.
Definition: NetServerTurnManager.h:95
CStrW playerName
Definition: NetServerTurnManager.h:77
std::unordered_map< int, Client > m_ClientsData
Definition: NetServerTurnManager.h:86
void NotifyFinishedClientUpdate(CNetServerSession &session, u32 turn, const CStr &hash)
Definition: NetServerTurnManager.cpp:107
CNetServerTurnManager(CNetServerWorker &server)
Definition: NetServerTurnManager.cpp:37
u32 simulatedTurn
Definition: NetServerTurnManager.h:81
CNetServerWorker & m_NetServer
Definition: NetServerTurnManager.h:103
u32 readyTurn
Definition: NetServerTurnManager.h:79
bool isOOS
Definition: NetServerTurnManager.h:83
void CheckClientsReady()
Definition: NetServerTurnManager.cpp:75
u32 GetReadyTurn()
Returns the latest turn for which all clients are ready; they will have already been told to execute ...
Definition: NetServerTurnManager.h:64
void NotifyFinishedClientCommands(CNetServerSession &session, u32 turn)
Definition: NetServerTurnManager.cpp:48
Network server worker thread.
Definition: NetServer.h:212
The server&#39;s end of a network session.
Definition: NetSession.h:154
The server-side counterpart to CNetClientTurnManager.
Definition: NetServerTurnManager.h:37