26#include "utils/no_copy.hpp"
27#include "utils/time.hpp"
81 ENetAddress m_address;
89 std::atomic_bool m_spectator;
91 std::atomic_bool m_disconnected;
93 std::atomic_bool m_warned_for_high_ping;
95 std::atomic<uint8_t> m_always_spectate;
100 std::unique_ptr<SocketAddress> m_socket_address;
104 std::vector<std::shared_ptr<NetworkPlayerProfile> > m_players;
106 uint64_t m_connected_time;
108 std::atomic<int64_t> m_last_activity;
110 std::atomic<int64_t> m_last_message;
112 int m_consecutive_messages;
117 std::unique_ptr<Crypto> m_crypto;
119 std::deque<uint32_t> m_previous_pings;
121 std::atomic<uint32_t> m_average_ping;
123 std::atomic<int> m_packet_loss;
125 std::set<unsigned> m_available_kart_ids;
127 std::string m_user_version;
133 std::array<int, AS_TOTAL> m_addons_scores;
140 bool encrypted =
true);
153 std::vector<std::shared_ptr<NetworkPlayerProfile> >& getPlayerProfiles()
154 {
return m_players; }
156 bool hasPlayerProfiles()
const {
return !m_players.empty(); }
158 void cleanPlayerProfiles() { m_players.clear(); }
160 void addPlayer(std::shared_ptr<NetworkPlayerProfile> p)
161 { m_players.push_back(p); }
163 void setValidated(
bool val) {
m_validated.store(val); }
171 float getConnectedTime()
const
174 void setAvailableKartsTracks(std::set<std::string>& k,
175 std::set<std::string>& t)
178 void eraseServerKarts(
const std::set<std::string>& server_karts,
179 std::set<std::string>& karts_erase)
const
183 for (
const std::string& server_kart : server_karts)
188 karts_erase.insert(server_kart);
193 void eraseServerTracks(
const std::set<std::string>& server_tracks,
194 std::set<std::string>& tracks_erase)
const
198 for (
const std::string& server_track : server_tracks)
203 tracks_erase.insert(server_track);
208 std::pair<std::set<std::string>, std::set<std::string> >
211 void setPingInterval(uint32_t interval)
212 { enet_peer_ping_interval(
m_enet_peer, interval); }
216 Crypto* getCrypto()
const {
return m_crypto.get(); }
218 void setCrypto(std::unique_ptr<Crypto>&& c);
220 uint32_t getAveragePing()
const {
return m_average_ping.load(); }
222 ENetPeer* getENetPeer()
const {
return m_enet_peer; }
228 void setSpectator(
bool val) { m_spectator.store(val); }
230 bool isSpectator()
const {
return m_spectator.load(); }
232 bool isDisconnected()
const {
return m_disconnected.load(); }
234 void setDisconnected(
bool val) {
return m_disconnected.store(val); }
236 bool hasWarnedForHighPing()
const {
return m_warned_for_high_ping.load(); }
238 void setWarnedForHighPing(
bool val) { m_warned_for_high_ping.store(val); }
240 void clearAvailableKartIDs() { m_available_kart_ids.clear(); }
242 void addAvailableKartID(
unsigned id) { m_available_kart_ids.insert(
id); }
244 bool availableKartID(
unsigned id)
245 {
return m_available_kart_ids.find(
id) != m_available_kart_ids.end(); }
247 const std::set<unsigned>& getAvailableKartIDs()
const
248 {
return m_available_kart_ids; }
250 void setUserVersion(
const std::string& uv) { m_user_version = uv; }
252 const std::string& getUserVersion()
const {
return m_user_version; }
254 void updateLastActivity()
257 int idleForSeconds()
const
263 return (
int)(diff / 1000);
266 void setClientCapabilities(std::set<std::string>& caps)
269 const std::set<std::string>& getClientCapabilities()
const
272 bool isAIPeer()
const {
return m_user_version ==
"AI"; }
274 void setPacketLoss(
int loss) { m_packet_loss.store(loss); }
276 int getPacketLoss()
const {
return m_packet_loss.load(); }
278 const std::array<int, AS_TOTAL>& getAddonsScores()
const
279 {
return m_addons_scores; }
281 void setAddonsScores(
const std::array<int, AS_TOTAL>& scores)
282 { m_addons_scores = scores; }
284 void updateLastMessage()
287 int64_t getLastMessage()
const
288 {
return m_last_message; }
290 void updateConsecutiveMessages(
bool too_fast)
293 m_consecutive_messages++;
295 m_consecutive_messages = 0;
298 int getConsecutiveMessages()
const {
return m_consecutive_messages; }
300 const SocketAddress& getAddress()
const {
return *m_socket_address.get(); }
303 { m_always_spectate.store(mode); }
305 bool alwaysSpectate()
const
306 {
return m_always_spectate.load() !=
ASM_NONE; }
308 void resetAlwaysSpectateFull()
310 if (m_always_spectate.load() ==
ASM_FULL)
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
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Represents the local host.
Definition: stk_host.hpp:73
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
std::pair< std::set< std::string >, std::set< std::string > > m_available_kts
Available karts and tracks from this peer.
Definition: stk_peer.hpp:115
uint32_t getPing()
Returns the ping to this peer from host, it waits for 3 seconds for a stable ping returned by enet me...
Definition: stk_peer.cpp:165
bool isSamePeer(const STKPeer *peer) const
Returns if this STKPeer is the same as the given peer.
Definition: stk_peer.cpp:148
void reset()
Forcefully disconnects a peer (used by server).
Definition: stk_peer.cpp:89
void kick()
Kick this peer (used by server).
Definition: stk_peer.cpp:77
std::atomic_bool m_waiting_for_game
True if this peer is waiting for game.
Definition: stk_peer.hpp:87
void sendPacket(NetworkString *data, bool reliable=true, bool encrypted=true)
Sends a packet to this host.
Definition: stk_peer.cpp:103
bool isValidated() const
Returns if the client is validated by server.
Definition: stk_peer.hpp:166
bool isConnected() const
Returns if the peer is connected or not.
Definition: stk_peer.cpp:139
std::atomic_bool m_validated
True if this peer is validated by server.
Definition: stk_peer.hpp:84
std::set< std::string > m_client_capabilities
List of client capabilities set when connecting it, to determine features available in same version.
Definition: stk_peer.hpp:131
uint32_t m_host_id
Host id of this peer.
Definition: stk_peer.hpp:98
ENetPeer * m_enet_peer
Pointer to the corresponding ENet peer data structure.
Definition: stk_peer.hpp:79
uint32_t getHostId() const
Returns the host id of this peer.
Definition: stk_peer.hpp:169
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:106
PeerDisconnectInfo
Definition: stk_peer.hpp:48
@ PDI_TIMEOUT
Timeout disconnected (default in enet).
Definition: stk_peer.hpp:49
@ PDI_KICK
Kick disconnection.
Definition: stk_peer.hpp:51
@ PDI_NORMAL
Normal disconnction with acknowledgement.
Definition: stk_peer.hpp:50
@ PDI_KICK_HIGH_PING
Too high ping, kicked by server.
Definition: stk_peer.hpp:52
AlwaysSpectateMode
Definition: stk_peer.hpp:65
@ ASM_COMMAND
Set by player through command.
Definition: stk_peer.hpp:67
@ ASM_FULL
Set by server because too many players joined.
Definition: stk_peer.hpp:68
@ ASM_NONE
Default, not spectating at all.
Definition: stk_peer.hpp:66
Declares the general types that are used by the network.