25 #include "utils/stk_process.hpp"
26 #include "utils/synchronised.hpp"
27 #include "utils/time.hpp"
29 #include "irrString.h"
34 #define WIN32_LEAN_AND_MEAN
35 #include <enet/enet.h>
65 enum ENetCommandType :
unsigned int
81 std::thread m_client_loop_thread;
94 std::vector<std::tuple<ENetPeer*,
95 ENetPacket*, uint32_t,
96 ENetCommandType, ENetAddress> > m_enet_cmd;
102 std::map<ENetPeer*, std::shared_ptr<STKPeer> >
m_peers;
108 uint32_t m_next_unique_host_id = 0;
111 uint32_t m_host_id = 0;
144 std::atomic<uint32_t> m_client_ping;
146 std::atomic<uint32_t> m_upload_speed;
148 std::atomic<uint32_t> m_download_speed;
150 std::atomic<uint32_t> m_players_in_game;
152 std::atomic<uint32_t> m_players_waiting;
154 std::atomic<uint32_t> m_total_players;
156 std::atomic<int64_t> m_network_timer;
158 std::unique_ptr<NetworkTimerSynchronizer> m_nts;
167 void handleDirectSocketRequest(
Network* direct_socket,
168 std::shared_ptr<ServerLobby> sl,
169 std::map<std::string, uint64_t>& ctp);
171 void mainLoop(ProcessType pt);
173 void getIPFromStun(
int socket,
const std::string& stun_address,
182 static std::shared_ptr<LobbyProtocol> create(
ChildLoop* cl = NULL);
187 ProcessType pt = STKProcess::getType();
188 assert(m_stk_host[pt] != NULL);
189 return m_stk_host[pt];
192 static STKHost *getByType(ProcessType pt)
194 assert(m_stk_host[pt] != NULL);
195 return m_stk_host[pt];
198 static void destroy()
200 ProcessType pt = STKProcess::getType();
201 assert(m_stk_host[pt] != NULL);
202 delete m_stk_host[pt];
203 m_stk_host[pt] = NULL;
208 {
return m_stk_host[STKProcess::getType()] != NULL; }
210 static void clear() { memset(m_stk_host, 0,
sizeof(m_stk_host)); }
213 {
return *m_public_address.get(); }
215 const std::string& getPublicIPv6Address()
const
216 {
return m_public_ipv6_address; }
218 std::string getValidPublicAddress()
const;
221 {
return m_stun_ipv4.get(); }
224 {
return m_stun_ipv6.get(); }
226 uint16_t getPrivatePort()
const;
228 void setPublicAddress(
short family);
230 void disconnectAllPeers(
bool timeout_waiting =
false);
241 m_shutdown.store(
true);
247 bool reliable =
true);
249 void sendPacketToAllPeers(
NetworkString *data,
bool reliable =
true);
251 void sendPacketToAllPeersWith(std::function<
bool(
STKPeer*)> predicate,
261 { m_authorised.store(authorised); }
263 std::vector<std::shared_ptr<NetworkPlayerProfile> >
264 getAllPlayerProfiles()
const;
266 std::set<uint32_t> getAllPlayerOnlineIds()
const;
268 std::shared_ptr<STKPeer> findPeerByHostId(uint32_t
id)
const;
270 std::shared_ptr<STKPeer> findPeerByName(
const core::stringw& name)
const;
273 bool reliable =
true);
275 void setupClient(
int peer_count,
int channel_limit,
276 uint32_t max_incoming_bandwidth,
277 uint32_t max_outgoing_bandwidth);
279 void startListening();
281 void stopListening();
285 std::shared_ptr<STKPeer> getServerPeerForClient()
const;
287 void setErrorMessage(
const irr::core::stringw &message);
289 void addEnetCommand(ENetPeer* peer, ENetPacket* packet, uint32_t i,
290 ENetCommandType ect, ENetAddress ea)
292 std::lock_guard<std::mutex> lock(m_enet_cmd_mutex);
293 m_enet_cmd.emplace_back(peer, packet, i, ect, ea);
298 {
return m_error_message; }
304 int receiveRawPacket(
char *buffer,
int buffer_len,
310 Network* getNetwork()
const {
return m_network; }
313 std::vector<std::shared_ptr<STKPeer> >
getPeers()
const
315 std::lock_guard<std::mutex> lock(m_peers_mutex);
316 std::vector<std::shared_ptr<STKPeer> > peers;
317 for (
auto p : m_peers)
319 peers.push_back(p.second);
327 assert(m_next_unique_host_id >= 0);
328 return m_next_unique_host_id;
331 void setNextHostId(uint32_t
id) { m_next_unique_host_id = id; }
336 std::lock_guard<std::mutex> lock(m_peers_mutex);
337 return (
unsigned)m_peers.size();
346 void sendToServer(
NetworkString *data,
bool reliable =
true);
348 bool isClientServer()
const;
350 void initClientNetwork(ENetEvent& event,
Network* new_network);
352 std::map<uint32_t, uint32_t> getPeerPings()
355 uint32_t getClientPingToServer()
const
356 {
return m_client_ping.load(std::memory_order_relaxed); }
359 {
return m_nts.get(); }
361 uint64_t getNetworkTimer()
const
364 void setNetworkTimer(uint64_t ticks)
366 m_network_timer.store(
370 std::pair<int, int> getAllPlayersTeamInfo()
const;
373 unsigned getUploadSpeed()
const {
return m_upload_speed.load(); }
376 unsigned getDownloadSpeed()
const {
return m_download_speed.load(); }
378 void updatePlayers(
unsigned* ingame = NULL,
379 unsigned* waiting = NULL,
380 unsigned* total = NULL);
382 uint32_t getPlayersInGame()
const {
return m_players_in_game.load(); }
384 uint32_t getWaitingPlayers()
const {
return m_players_waiting.load(); }
386 uint32_t getTotalPlayers()
const {
return m_total_players.load(); }
388 std::vector<std::shared_ptr<NetworkPlayerProfile> >
389 getPlayersForNewGame(
bool* has_always_on_spectators = NULL)
const;
391 void replaceNetwork(
Network* new_network)
393 m_network = new_network;
398 ChildLoop* getChildLoop()
const {
return m_client_loop; }
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
Definition: child_loop.hpp:34
Used to store the needed data about the players that join a game. This class stores all the possible ...
Definition: game_setup.hpp:43
Base class for both client and server lobby. The lobbies are started when a server opens a game,...
Definition: lobby_protocol.hpp:47
Contains the profile of a player.
Definition: network_player_profile.hpp:42
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
Definition: network_timer_synchronizer.hpp:34
Definition: network.hpp:46
Represents the local host.
Definition: stk_host.hpp:73
static bool existHost()
Checks if the STKHost has been created.
Definition: stk_host.hpp:207
std::atomic_bool m_shutdown
Flag which is set from the protocol manager thread which triggers a shutdown of the STKHost (and the ...
Definition: stk_host.hpp:118
void setMyHostId(uint32_t my_host_id)
Sets the global host id of this host (client use).
Definition: stk_host.hpp:341
std::thread m_listening_thread
Id of thread listening to enet events.
Definition: stk_host.hpp:114
std::atomic_bool m_authorised
True if this local host is authorised to control a server.
Definition: stk_host.hpp:121
void requestShutdown()
Requests that the network infrastructure is to be shut down.
Definition: stk_host.hpp:239
unsigned int getNextHostId() const
Returns the next (unique) host id.
Definition: stk_host.hpp:325
unsigned int getPeerCount() const
Returns the number of currently connected peers.
Definition: stk_host.hpp:334
std::unique_ptr< SocketAddress > m_public_address
The public address found by stun (if WAN is used).
Definition: stk_host.hpp:131
std::unique_ptr< SocketAddress > m_stun_ipv4
The public IPv4 address stun server used.
Definition: stk_host.hpp:137
std::string m_public_ipv6_address
The public IPv6 address found by stun (if WAN is used).
Definition: stk_host.hpp:134
const irr::core::stringw & getErrorMessage() const
Returns the last error (or "" if no error has happened).
Definition: stk_host.hpp:297
std::mutex m_peers_mutex
Make sure the removing or adding a peer is thread-safe.
Definition: stk_host.hpp:90
static bool m_enable_console
If a network console should be started.
Definition: stk_host.hpp:177
std::atomic< uint64_t > m_exit_timeout
Use as a timeout to waiting a disconnect event when exiting.
Definition: stk_host.hpp:124
std::map< ENetPeer *, std::shared_ptr< STKPeer > > m_peers
The list of peers connected to this instance.
Definition: stk_host.hpp:102
uint32_t getMyHostId() const
Returns the host id of this host.
Definition: stk_host.hpp:344
static STKHost * get()
Returns the instance of STKHost.
Definition: stk_host.hpp:185
std::vector< std::shared_ptr< STKPeer > > getPeers() const
Returns a copied list of peers.
Definition: stk_host.hpp:313
bool requestedShutdown() const
Returns true if a shutdown of the network infrastructure was requested.
Definition: stk_host.hpp:302
std::thread m_network_console
Network console thread.
Definition: stk_host.hpp:87
Network * m_network
ENet host interfacing sockets.
Definition: stk_host.hpp:84
std::unique_ptr< SocketAddress > m_stun_ipv6
The public IPv6 address stun server used.
Definition: stk_host.hpp:140
ChildLoop * m_client_loop
Separate process of server instance.
Definition: stk_host.hpp:79
bool isAuthorisedToControl() const
Returns true if this client instance is allowed to control the server.
Definition: stk_host.hpp:257
void setAuthorisedToControl(bool authorised)
Sets if this local host is authorised to control the server.
Definition: stk_host.hpp:260
irr::core::stringw m_error_message
An error message, which is set by a protocol to be displayed in the GUI.
Definition: stk_host.hpp:128
std::mutex m_enet_cmd_mutex
Protect m_enet_cmd from multiple threads usage.
Definition: stk_host.hpp:99
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
Definition: server_lobby.hpp:52
Definition: server.hpp:44
Describes a IPv4 or IPv6 address in sockaddr_in(6) format, suitable in using with sendto.
Definition: socket_address.hpp:47
static uint64_t getMonoTimeMs()
Returns a time based since the starting of stk (monotonic clock).
Definition: time.hpp:113
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
TYPE getAtomic() const
Returns a copy of this variable.
Definition: synchronised.hpp:68