Codechange: document network related functions/types/variables

This commit is contained in:
Rubidium
2026-03-08 13:29:09 +01:00
committed by rubidium42
parent 856a02ae53
commit 48a8903d85
22 changed files with 269 additions and 50 deletions
+47 -10
View File
@@ -61,7 +61,7 @@ bool _ddc_fastforward = true;
static_assert(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
/** The pool with client information. */
NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
NetworkClientInfoPool _networkclientinfo_pool{"NetworkClientInfo"};
INSTANTIATE_POOL_METHODS(NetworkClientInfo)
bool _networking; ///< are we in networking mode?
@@ -213,6 +213,10 @@ bool NetworkAuthorizedKeys::Remove(std::string_view key)
}
/**
* Get the number of clients that are playing as a spectator.
* @return The number of spectator clients.
*/
uint8_t NetworkSpectatorCount()
{
uint8_t count = 0;
@@ -228,9 +232,15 @@ uint8_t NetworkSpectatorCount()
}
/* This puts a text-message to the console, or in the future, the chat-box,
* (to keep it all a bit more general)
* If 'self_send' is true, this is the client who is sending the message */
/**
* Writes a text-message to the console and the chat box.
* @param action The network action that lead to this message.
* @param colour The color for the message.
* @param self_send Whether the message came from ourselves, or the network.
* @param name The name of the client.
* @param str Arbitrary extra string, depending on the action. For example a complete message or company name.
* @param data Arbitrary extra data, depending on the action. For example a client's ID or an amount of money that was given.
*/
void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, std::string_view name, std::string_view str, StringParameter &&data)
{
std::string message;
@@ -281,7 +291,11 @@ void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send,
NetworkAddChatMessage(colour, _settings_client.gui.network_chat_timeout, message);
}
/* Calculate the frame-lag of a client */
/**
* Calculate the frame-lag of a client.
* @param cs The client's socket.
* @return The number of frames the client is lagging behind.
*/
uint NetworkCalculateLag(const NetworkClientSocket *cs)
{
int lag = cs->last_frame_server - cs->last_frame;
@@ -295,8 +309,10 @@ uint NetworkCalculateLag(const NetworkClientSocket *cs)
}
/* There was a non-recoverable error, drop back to the main menu with a nice
* error */
/**
* There was a non-recoverable error, drop back to the main menu with a nice error.
* @param error_string The error message to show.
*/
void ShowNetworkError(StringID error_string)
{
_switch_mode = SM_MENU;
@@ -621,7 +637,10 @@ void NetworkClose(bool close_admins)
InitializeNetworkPools(close_admins);
}
/* Initializes the network (cleans sockets and stuff) */
/**
* Initializes the network (cleans sockets and stuff)
* @param close_admins Whether to disconnect all the admin connections.
*/
static void NetworkInitialize(bool close_admins = true)
{
InitializeNetworkPools(close_admins);
@@ -635,9 +654,13 @@ static void NetworkInitialize(bool close_admins = true)
/** Non blocking connection to query servers for their game info. */
class TCPQueryConnecter : public TCPServerConnecter {
private:
std::string connection_string;
std::string connection_string; ///< The address that this connecter is trying to query.
public:
/**
* Create the connecter.
* @param connection_string The address to connect to.
*/
TCPQueryConnecter(std::string_view connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
void OnFailure() override
@@ -739,9 +762,13 @@ void NetworkRebuildHostList()
/** Non blocking connection create to actually connect to servers */
class TCPClientConnecter : public TCPServerConnecter {
private:
std::string connection_string;
std::string connection_string; ///< The address that this connecter is trying to connect to.
public:
/**
* Create the connecter.
* @param connection_string The address to connect to.
*/
TCPClientConnecter(std::string_view connection_string) : TCPServerConnecter(connection_string, NETWORK_DEFAULT_PORT), connection_string(connection_string) {}
void OnFailure() override
@@ -825,12 +852,17 @@ void NetworkClientJoinGame()
TCPConnecter::Create<TCPClientConnecter>(_network_join.connection_string);
}
/** Initialise/fill the server's game info. */
static void NetworkInitGameInfo()
{
FillStaticNetworkServerGameInfo();
/* The server is a client too */
_network_game_info.clients_on = _network_dedicated ? 0 : 1;
}
/** Initialise the server's client info. */
static void NetworkInitServerClientInfo()
{
/* There should be always space for the server. */
assert(NetworkClientInfo::CanAllocateItem());
NetworkClientInfo *ci = NetworkClientInfo::Create(CLIENT_ID_SERVER);
@@ -884,6 +916,10 @@ static void CheckClientAndServerName()
}
}
/**
* Run everything related to the network when starting a server.
* @return \c true iff everything required to start the server succeeded.
*/
bool NetworkServerStart()
{
if (!_network_available) return false;
@@ -922,6 +958,7 @@ bool NetworkServerStart()
_network_clients_connected = 0;
NetworkInitGameInfo();
NetworkInitServerClientInfo();
if (_settings_client.network.server_game_type != ServerGameType::Local) {
_network_coordinator_client.Register();