SuperTuxKart
server.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2013-2015 Glenn De Jonghe
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 
19 #ifndef HEADER_SERVER_HPP
20 #define HEADER_SERVER_HPP
21 
27 #include "race/race_manager.hpp"
28 #include "utils/types.hpp"
29 
30 #include <irrString.h>
31 
32 #include <map>
33 #include <string>
34 #include <tuple>
35 
36 class Track;
37 class XMLNode;
38 class SocketAddress;
39 
43 class Server
44 {
45 public:
46 
47 protected:
49  irr::core::stringw m_name;
50 
52  std::string m_lower_case_name;
53 
54  std::string m_server_owner_lower_case_name;
55 
56  std::string m_lower_case_player_names;
57 
62  std::unique_ptr<SocketAddress> m_ipv6_address;
63 
64  uint32_t m_server_id;
65  uint32_t m_server_owner;
66 
69 
72 
73  int m_current_ai;
74 
75  uint32_t m_bookmark_id;
76 
78  std::unique_ptr<SocketAddress> m_address;
79 
83  uint16_t m_private_port;
84 
85  unsigned m_server_mode;
86 
87  RaceManager::Difficulty m_difficulty;
88 
89  bool m_password_protected;
90 
91  /* WAN server only, show the owner name of server, can only be seen
92  * for localhost or if you are friend with the server owner. */
93  core::stringw m_server_owner_name;
94 
95  /* WAN server only, distance based on IP latitude and longitude. */
96  float m_distance;
97 
98  /* WAN server only, true if hosted officially by stk team. */
99  bool m_official;
100 
101  bool m_supports_encrytion;
102 
103  bool m_game_started;
104 
105  bool m_ipv6_connection;
106 
107  bool m_reconnect_when_quit_lobby;
108 
109  bool m_aes_gcm_128bit_tag;
110 
111  std::vector<std::tuple<
112  /*rank*/int, core::stringw, /*scores*/double, /*playing time*/float
113  > > m_players;
114 
115  std::string m_current_track;
116 
117  std::string m_country_code;
118 public:
119 
121  Server(const XMLNode& server_info);
122  Server(unsigned server_id, const irr::core::stringw &name,
123  int max_players, int current_players, unsigned difficulty,
124  unsigned server_mode, const SocketAddress &address,
125  bool password_protected, bool game_started,
126  const std::string& current_track = "");
127  // ------------------------------------------------------------------------
128  virtual ~Server();
129  // ------------------------------------------------------------------------
131  const SocketAddress& getAddress() const { return *m_address.get(); }
132  // ------------------------------------------------------------------------
133  void setAddress(const SocketAddress& addr);
134  // ------------------------------------------------------------------------
136  const std::string& getLowerCaseName() const { return m_lower_case_name; }
137  // ------------------------------------------------------------------------
139  const irr::core::stringw& getName() const { return m_name; }
140  // ------------------------------------------------------------------------
142  const uint32_t getServerId() const { return m_server_id; }
143  // ------------------------------------------------------------------------
145  const uint32_t getServerOwner() const { return m_server_owner; }
146  // ------------------------------------------------------------------------
147  uint16_t getPrivatePort() const { return m_private_port; }
148  // ------------------------------------------------------------------------
150  const int getMaxPlayers() const { return m_max_players; }
151  // ------------------------------------------------------------------------
153  const int getCurrentPlayers() const { return m_current_players; }
154  // ------------------------------------------------------------------------
155  unsigned getServerMode() const { return m_server_mode; }
156  // ------------------------------------------------------------------------
157  RaceManager::Difficulty getDifficulty() const { return m_difficulty; }
158  // ------------------------------------------------------------------------
159  bool isPasswordProtected() const { return m_password_protected; }
160  // ------------------------------------------------------------------------
161  const core::stringw& getServerOwnerName() const
162  { return m_server_owner_name; }
163  // ------------------------------------------------------------------------
164  const std::string& getServerOwnerLowerCaseName() const
165  { return m_server_owner_lower_case_name; }
166  // ------------------------------------------------------------------------
167  float getDistance() const { return m_distance; }
168  // ------------------------------------------------------------------------
169  bool supportsEncryption() const { return m_supports_encrytion; }
170  // ------------------------------------------------------------------------
171  bool isOfficial() const { return m_official; }
172  // ------------------------------------------------------------------------
173  bool isGameStarted() const { return m_game_started; }
174  // ------------------------------------------------------------------------
175  const std::vector<std::tuple<int, core::stringw, double, float> >&
176  getPlayers() const { return m_players; }
177  // ------------------------------------------------------------------------
178  void setServerId(unsigned id) { m_server_id = id; }
179  // ------------------------------------------------------------------------
180  void setPrivatePort(uint16_t port) { m_private_port = port; }
181  // ------------------------------------------------------------------------
182  void setSupportsEncryption(bool val)
183  {
184  m_supports_encrytion = val;
185  // We assume that a custom-defined server supports AES GCM 128bit tag
186  if (m_supports_encrytion)
187  m_aes_gcm_128bit_tag = true;
188  }
189  // ------------------------------------------------------------------------
190  bool searchByName(const std::string& lower_case_word);
191  // ------------------------------------------------------------------------
192  Track* getCurrentTrack() const;
193  // ------------------------------------------------------------------------
194  const std::string& getCountryCode() const { return m_country_code; }
195  // ------------------------------------------------------------------------
196  void setIPV6Connection(bool val)
197  {
198  if (!m_ipv6_address)
199  m_ipv6_connection = false;
200  else
201  m_ipv6_connection = val;
202  }
203  // ------------------------------------------------------------------------
204  bool useIPV6Connection() const { return m_ipv6_connection; }
205  // ------------------------------------------------------------------------
206  void setIPV6Address(const SocketAddress& addr);
207  // ------------------------------------------------------------------------
208  SocketAddress* getIPV6Address() const
209  {
210  if (!m_ipv6_address)
211  return NULL;
212  return m_ipv6_address.get();
213  }
214  // ------------------------------------------------------------------------
215  virtual void saveServer() const {}
216  // ------------------------------------------------------------------------
217  void setIsPasswordProtected(bool password_protected) { m_password_protected = password_protected; }
218  // ------------------------------------------------------------------------
219  bool reconnectWhenQuitLobby() const { return m_reconnect_when_quit_lobby; }
220  // ------------------------------------------------------------------------
221  void setReconnectWhenQuitLobby(bool val)
222  { m_reconnect_when_quit_lobby = val; }
223  // ------------------------------------------------------------------------
224  std::string getBookmarkKey() const;
225  // ------------------------------------------------------------------------
226  const int getCurrentAI() const { return m_current_ai; }
227  // ------------------------------------------------------------------------
228  uint32_t getBookmarkID() const { return m_bookmark_id; }
229  // ------------------------------------------------------------------------
230  void setBookmarkID(uint32_t id) { m_bookmark_id = id; }
231  // ------------------------------------------------------------------------
232  bool supportsAESGCM128BitTag() const { return m_aes_gcm_128bit_tag; }
233 }; // Server
234 
235 class UserDefinedServer : public Server
236 {
237 public:
238  UserDefinedServer(const core::stringw& name, const SocketAddress& ipv4,
239  bool password_protected = false)
240  : Server(0, name, 0, 0, 0, 0, ipv4, password_protected, false) {}
241  // ------------------------------------------------------------------------
242  virtual void saveServer() const;
243 }; // UserDefinedServer
244 
245 #endif // HEADER_SERVER_HPP
Difficulty
Game difficulty.
Definition: race_manager.hpp:231
Definition: server.hpp:44
const int getCurrentPlayers() const
Returns the number of currently connected players.
Definition: server.hpp:153
Server(const XMLNode &server_info)
Initialises the object from an XML node.
Definition: server.cpp:39
int m_current_players
The number of players currently on the server.
Definition: server.hpp:71
int m_max_players
The maximum number of players that the server supports.
Definition: server.hpp:68
const uint32_t getServerOwner() const
Returns the user id in STK addon server of the server owner (WAN).
Definition: server.hpp:145
const uint32_t getServerId() const
Returns the ID of this server.
Definition: server.hpp:142
const int getMaxPlayers() const
Returns the maximum number of players allowed on this server.
Definition: server.hpp:150
irr::core::stringw m_name
The server name to be displayed.
Definition: server.hpp:49
std::unique_ptr< SocketAddress > m_address
The public ip address and port of this server.
Definition: server.hpp:78
std::string m_lower_case_name
Name in lower case for comparisons.
Definition: server.hpp:52
const SocketAddress & getAddress() const
Returns IPv4 address and port of this server.
Definition: server.hpp:131
const std::string & getLowerCaseName() const
Returns the lower case name of the server.
Definition: server.hpp:136
uint16_t m_private_port
This is the private port of the server.
Definition: server.hpp:83
const irr::core::stringw & getName() const
Returns the name of the server.
Definition: server.hpp:139
std::unique_ptr< SocketAddress > m_ipv6_address
We need to use full socket address structure instead of string to hold it, because for local link add...
Definition: server.hpp:62
Describes a IPv4 or IPv6 address in sockaddr_in(6) format, suitable in using with sendto.
Definition: socket_address.hpp:47
Definition: track.hpp:115
Definition: server.hpp:236
utility class used to parse XML files
Definition: xml_node.hpp:48
Declares the general types that are used by the network.