SuperTuxKart
network_config.hpp
Go to the documentation of this file.
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
22 #ifndef HEADER_NETWORK_CONFIG
23 #define HEADER_NETWORK_CONFIG
24 
25 #include "race/race_manager.hpp"
26 #include "utils/stk_process.hpp"
27 #include "utils/no_copy.hpp"
28 
29 #include "irrString.h"
30 #include <array>
31 #include <atomic>
32 #include <cstring>
33 #include <memory>
34 #include <set>
35 #include <tuple>
36 #include <vector>
37 #include <utility>
38 
39 namespace Online
40 {
41  class XMLRequest;
42 }
43 
44 namespace GUIEngine
45 {
46  class Screen;
47 }
48 
49 class InputDevice;
50 class PlayerProfile;
51 
52 class NetworkConfig : public NoCopy
53 {
54 public:
55  enum IPType : int
56  {
57  IP_NONE, IP_V4, IP_V6, IP_V6_NAT64, IP_DUAL_STACK
58  };
59 private:
61  static NetworkConfig *m_network_config[PT_COUNT];
62 
63  static bool m_system_ipv4;
64 
65  static bool m_system_ipv6;
66 
67  enum NetworkType : int
68  {
69  NETWORK_NONE, NETWORK_WAN, NETWORK_LAN
70  };
71 
72  std::atomic<IPType> m_ip_type;
73 
75  std::atomic<NetworkType> m_network_type;
76 
81 
83  std::atomic_bool m_is_server;
84 
88 
89  bool m_done_adding_network_players;
90 
94 
97 
101  unsigned m_num_fixed_ai;
102 
104  uint16_t m_client_port;
105 
107  uint32_t m_cur_user_id;
108  std::string m_cur_user_token;
109 
111  std::string m_server_id_file;
112 
113  std::vector<std::tuple<InputDevice*, PlayerProfile*, HandicapLevel> > m_network_players;
114 
115  NetworkConfig();
116 
117  uint32_t m_joined_server_version;
118 
121 
124  std::set<std::string> m_server_capabilities;
125 
129  std::string m_nat64_prefix;
130  std::array<uint32_t, 8> m_nat64_prefix_data;
131  // ------------------------------------------------------------------------
132  static void fillStunList(std::vector<std::pair<std::string, int> >* l,
133  const std::string& dns);
134 
135 public:
136  static void initSystemIP();
137  // ------------------------------------------------------------------------
139  static NetworkConfig *get()
140  {
141  ProcessType type = STKProcess::getType();
142  if (!m_network_config[type])
143  m_network_config[type] = new NetworkConfig();
144  return m_network_config[type];
145  } // get
146  // ------------------------------------------------------------------------
147  static NetworkConfig* getByType(ProcessType type)
148  {
149  return m_network_config[type];
150  } // get
151  // ------------------------------------------------------------------------
152  static void clearDetectIPThread(bool quit_stk);
153  // ------------------------------------------------------------------------
154  static void queueIPDetection();
155  // ------------------------------------------------------------------------
156  static void destroy()
157  {
158  ProcessType type = STKProcess::getType();
159  delete m_network_config[type]; // It's ok to delete NULL
160  m_network_config[type] = NULL;
161  if (type == PT_MAIN)
162  clearDetectIPThread(true/*quit_stk*/);
163  } // destroy
164  // ------------------------------------------------------------------------
165  static void clear()
166  {
167  memset(m_network_config, 0, sizeof(m_network_config));
168  } // clear
169  // ------------------------------------------------------------------------
171  void setIsServer(bool b)
172  {
173  m_is_server = b;
174  } // setIsServer
175  // ------------------------------------------------------------------------
177  void setClientPort(uint16_t port) { m_client_port = port; }
178  // ------------------------------------------------------------------------
180  uint16_t getClientPort() const { return m_client_port; }
181  // ------------------------------------------------------------------------
184  // ------------------------------------------------------------------------
186  bool isPublicServer() const { return m_is_public_server; }
187  // ------------------------------------------------------------------------
190  bool isNetworking() const { return m_network_type!=NETWORK_NONE; }
191  // ------------------------------------------------------------------------
193  bool isLAN() const { return m_network_type == NETWORK_LAN; }
194  // ------------------------------------------------------------------------
196  bool isWAN() const { return m_network_type == NETWORK_WAN; }
197  // ------------------------------------------------------------------------
199  void setIsLAN() { m_network_type = NETWORK_LAN; }
200  // ------------------------------------------------------------------------
202  void setIsWAN() { m_network_type = NETWORK_WAN; }
203  // ------------------------------------------------------------------------
204  void unsetNetworking();
205  // ------------------------------------------------------------------------
206  std::vector<std::tuple<InputDevice*, PlayerProfile*, HandicapLevel> >&
207  getNetworkPlayers() { return m_network_players; }
208  // ------------------------------------------------------------------------
209  bool isAddingNetworkPlayers() const
210  { return !m_done_adding_network_players; }
211  // ------------------------------------------------------------------------
212  void doneAddingNetworkPlayers() { m_done_adding_network_players = true; }
213  // ------------------------------------------------------------------------
214  bool addNetworkPlayer(InputDevice* device, PlayerProfile* profile,
215  HandicapLevel h)
216  {
217  for (auto& p : m_network_players)
218  {
219  if (std::get<0>(p) == device && !m_network_ai_instance)
220  return false;
221  if (std::get<1>(p) == profile)
222  return false;
223  }
224  m_network_players.emplace_back(device, profile, h);
225  return true;
226  }
227  // ------------------------------------------------------------------------
228  bool playerExists(PlayerProfile* profile) const
229  {
230  for (auto& p : m_network_players)
231  {
232  if (std::get<1>(p) == profile)
233  return true;
234  }
235  return false;
236  }
237  // ------------------------------------------------------------------------
238  void cleanNetworkPlayers()
239  {
240  m_network_players.clear();
241  m_done_adding_network_players = false;
242  }
243  // ------------------------------------------------------------------------
245  bool isServer() const { return m_is_server; }
246  // ------------------------------------------------------------------------
248  bool isClient() const { return !m_is_server; }
249  // ------------------------------------------------------------------------
251  void setAutoConnect(bool b) { m_auto_connect = b; }
252  // ------------------------------------------------------------------------
255  bool isAutoConnect() const { return m_auto_connect; }
256  // ------------------------------------------------------------------------
257  void setNetworkAIInstance(bool b) { m_network_ai_instance = b; }
258  // ------------------------------------------------------------------------
259  bool isNetworkAIInstance() const { return m_network_ai_instance; }
260  // ------------------------------------------------------------------------
261  void setCurrentUserId(uint32_t id) { m_cur_user_id = id ; }
262  // ------------------------------------------------------------------------
263  void setCurrentUserToken(const std::string& t) { m_cur_user_token = t; }
264  // ------------------------------------------------------------------------
265  uint32_t getCurrentUserId() const { return m_cur_user_id; }
266  // ------------------------------------------------------------------------
267  const std::string& getCurrentUserToken() const { return m_cur_user_token; }
268  // ------------------------------------------------------------------------
269  void setUserDetails(std::shared_ptr<Online::XMLRequest> r,
270  const std::string& name);
271  // ------------------------------------------------------------------------
272  void setServerDetails(std::shared_ptr<Online::XMLRequest> r,
273  const std::string& name);
274  // ------------------------------------------------------------------------
275  void setServerIdFile(const std::string& id) { m_server_id_file = id; }
276  // ------------------------------------------------------------------------
277  const std::string& getServerIdFile() const { return m_server_id_file; }
278  // ------------------------------------------------------------------------
279  std::vector<GUIEngine::Screen*> getResetScreens(bool lobby = false) const;
280  // ------------------------------------------------------------------------
281  void setJoinedServerVersion(uint32_t v) { m_joined_server_version = v; }
282  // ------------------------------------------------------------------------
283  uint32_t getJoinedServerVersion() const { return m_joined_server_version; }
284  // ------------------------------------------------------------------------
285  void clearActivePlayersForClient() const;
286  // ------------------------------------------------------------------------
287  void setStateFrequency(int frequency) { m_state_frequency = frequency; }
288  // ------------------------------------------------------------------------
289  int getStateFrequency() const { return m_state_frequency; }
290  // ------------------------------------------------------------------------
291  bool roundValuesNow() const;
292  // ------------------------------------------------------------------------
293  void setServerCapabilities(std::set<std::string>& caps)
294  { m_server_capabilities = std::move(caps); }
295  // ------------------------------------------------------------------------
296  void clearServerCapabilities() { m_server_capabilities.clear(); }
297  // ------------------------------------------------------------------------
298  const std::set<std::string>& getServerCapabilities() const
299  { return m_server_capabilities; }
300  // ------------------------------------------------------------------------
301  void getIPDetectionResult(uint64_t timeout);
302  // ------------------------------------------------------------------------
303  IPType getIPType() const { return m_ip_type.load(); }
304  // ------------------------------------------------------------------------
305  void setIPType(IPType ip_type) { m_ip_type.store(ip_type); }
306  // ------------------------------------------------------------------------
307  const std::string& getNAT64Prefix() const { return m_nat64_prefix; }
308  // ------------------------------------------------------------------------
309  const std::array<uint32_t, 8>& getNAT64PrefixData() const
310  { return m_nat64_prefix_data; }
311  // ------------------------------------------------------------------------
312  void initClientPort();
313  // ------------------------------------------------------------------------
314  void setNumFixedAI(unsigned num) { m_num_fixed_ai = num; }
315  // ------------------------------------------------------------------------
316  unsigned getNumFixedAI() const { return m_num_fixed_ai; }
317  // ------------------------------------------------------------------------
318  static const std::vector<std::pair<std::string, int> >&
319  getStunList(bool ipv4);
320  // ------------------------------------------------------------------------
321  void setTuxHitboxAddon(bool val) { m_tux_hitbox_addon = val; }
322  // ------------------------------------------------------------------------
323  bool useTuxHitboxAddon() const { return m_tux_hitbox_addon; }
324 }; // class NetworkConfig
325 
326 #endif // HEADER_NETWORK_CONFIG
base class for input devices
Definition: input_device.hpp:48
This class is the interface between STK and the online code, particularly STKHost.
Definition: network_config.hpp:53
void initClientPort()
Separated from constructor because this needs to be run after user config is load.
Definition: network_config.cpp:169
void clearActivePlayersForClient() const
Called before (re)starting network race, must be used before adding split screen players.
Definition: network_config.cpp:262
bool isClient() const
Returns if this instance is a client.
Definition: network_config.hpp:248
static NetworkConfig * get()
Singleton get, which creates this object if necessary.
Definition: network_config.hpp:139
uint32_t m_cur_user_id
Used by wan server.
Definition: network_config.hpp:107
bool m_network_ai_instance
True if this STK instance is an AI instance which is used for server AI.
Definition: network_config.hpp:93
bool m_is_public_server
If set it allows clients to connect directly to this server without using the stk server in between.
Definition: network_config.hpp:80
void setIsLAN()
Set that this is a LAN networked game.
Definition: network_config.hpp:199
std::string m_nat64_prefix
For IPv6 only network we try to detect the NAT64 prefix so we can use it to connect to ipv4 only serv...
Definition: network_config.hpp:129
bool m_auto_connect
True if a client should connect to the first server it finds and immediately start a race.
Definition: network_config.hpp:87
bool isPublicServer() const
Returns if connections directly to the server are to be accepted.
Definition: network_config.hpp:186
int m_state_frequency
Set by client or server which is required to be the same.
Definition: network_config.hpp:120
bool roundValuesNow() const
True when client needs to round the bodies phyiscal info for current ticks, server doesn't as it will...
Definition: network_config.cpp:279
bool isServer() const
Returns if this instance is a server.
Definition: network_config.hpp:245
uint16_t m_client_port
The LAN port on which a client is waiting for a server connection.
Definition: network_config.hpp:104
void unsetNetworking()
Set that this is not a networked game.
Definition: network_config.cpp:178
void setIsPublicServer()
Sets that this server can be contacted directly.
Definition: network_config.hpp:183
void getIPDetectionResult(uint64_t timeout)
Use stun servers to detect current ip type.
Definition: network_config.cpp:379
bool isNetworking() const
Return if a network setting is happening.
Definition: network_config.hpp:190
static NetworkConfig * m_network_config[PT_COUNT]
The singleton instance.
Definition: network_config.hpp:61
void setIsWAN()
Set that this is a WAN networked game.
Definition: network_config.hpp:202
std::set< std::string > m_server_capabilities
List of server capabilities set when joining it, to determine features available in same version.
Definition: network_config.hpp:124
std::atomic< NetworkType > m_network_type
Keeps the type of network connection: none (yet), LAN or WAN.
Definition: network_config.hpp:75
bool isAutoConnect() const
Returns if an immediate connection to the first server was requested.
Definition: network_config.hpp:255
static void queueIPDetection()
Use stun servers to detect current ip type.
Definition: network_config.cpp:328
std::atomic_bool m_is_server
True if this host is a server, false otherwise.
Definition: network_config.hpp:83
NetworkConfig()
Constructor.
Definition: network_config.cpp:146
unsigned m_num_fixed_ai
No.
Definition: network_config.hpp:101
void setAutoConnect(bool b)
Sets if a client should immediately connect to the first server.
Definition: network_config.hpp:251
std::string m_server_id_file
Used by client server to determine if the child server is created.
Definition: network_config.hpp:111
void setClientPort(uint16_t port)
Sets the port on which a client listens for server connection.
Definition: network_config.hpp:177
uint16_t getClientPort() const
Returns the port on which a client listens for server connections.
Definition: network_config.hpp:180
bool isLAN() const
Return true if it's a networked game with a LAN server.
Definition: network_config.hpp:193
bool isWAN() const
Return true if it's a networked game but with a WAN server.
Definition: network_config.hpp:196
void setIsServer(bool b)
Sets if this instance is a server or client.
Definition: network_config.hpp:171
bool m_tux_hitbox_addon
When live join is disabled addon kart will use their real hitbox.
Definition: network_config.hpp:96
static void initSystemIP()
Initialize detection of system IPv4 or IPv6 support.
Definition: network_config.cpp:95
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Class for managing player profiles (name, usage frequency, etc.).
Definition: player_profile.hpp:55
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33
HandicapLevel
Handicap per player.
Definition: remote_kart_info.hpp:43