OpenTTD Source  20241121-master-g67a0fccfad
network_server.cpp File Reference

Server part of the network protocol. More...

#include "../stdafx.h"
#include "../strings_func.h"
#include "core/network_game_info.h"
#include "network_admin.h"
#include "network_server.h"
#include "network_udp.h"
#include "network_base.h"
#include "../console_func.h"
#include "../company_base.h"
#include "../command_func.h"
#include "../saveload/saveload.h"
#include "../saveload/saveload_filter.h"
#include "../station_base.h"
#include "../genworld.h"
#include "../company_func.h"
#include "../company_gui.h"
#include "../company_cmd.h"
#include "../roadveh.h"
#include "../order_backup.h"
#include "../core/pool_func.hpp"
#include "../core/random_func.hpp"
#include "../rev.h"
#include "../timer/timer.h"
#include "../timer/timer_game_calendar.h"
#include "../timer/timer_game_economy.h"
#include "../timer/timer_game_realtime.h"
#include <mutex>
#include <condition_variable>
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

struct  PacketWriter
 Writing a savegame directly to a number of packets. More...
 

Functions

static NetworkAuthenticationDefaultPasswordProvider _password_provider (_settings_client.network.server_password)
 Provides the password validation for the game's password.
 
static NetworkAuthenticationDefaultAuthorizedKeyHandler _authorized_key_handler (_settings_client.network.server_authorized_keys)
 Provides the authorized key handling for the game authentication.
 
static NetworkAuthenticationDefaultAuthorizedKeyHandler _rcon_authorized_key_handler (_settings_client.network.rcon_authorized_keys)
 Provides the authorized key validation for rcon.
 
static void NetworkHandleCommandQueue (NetworkClientSocket *cs)
 Handle the command-queue of a socket. More...
 
static NetworkErrorCode GetErrorForAuthenticationMethod (NetworkAuthenticationMethod method)
 
void NetworkServerSendChat (NetworkAction action, DestType desttype, int dest, const std::string &msg, ClientID from_id, int64_t data, bool from_admin)
 Send an actual chat message. More...
 
void NetworkServerSendExternalChat (const std::string &source, TextColour colour, const std::string &user, const std::string &msg)
 Send a chat message from external source. More...
 
void NetworkPopulateCompanyStats (NetworkCompanyStats *stats)
 Populate the company stats. More...
 
void NetworkUpdateClientInfo (ClientID client_id)
 Send updated client info of a particular client. More...
 
static void NetworkAutoCleanCompanies ()
 Remove companies that have not been used depending on the autoclean_companies setting and values for autoclean_protected, which removes any company, and autoclean_novehicles, which removes companies without vehicles.
 
bool NetworkMakeClientNameUnique (std::string &name)
 Check whether a name is unique, and otherwise try to make it unique. More...
 
bool NetworkServerChangeClientName (ClientID client_id, const std::string &new_name)
 Change the client name of the given client. More...
 
void NetworkServer_Tick (bool send_frame)
 This is called every tick if this is a _network_server. More...
 
static void NetworkRestartMap ()
 Helper function to restart the map.
 
void ChangeNetworkRestartTime (bool reset)
 Reset the automatic network restart time interval. More...
 
static void NetworkCheckRestartMapYear ()
 Check if we want to restart the map based on the year.
 
void NetworkServerShowStatusToConsole ()
 Show the status message of all clients on the console.
 
void NetworkServerSendConfigUpdate ()
 Send Config Update.
 
void NetworkServerUpdateGameInfo ()
 Update the server's NetworkServerGameInfo due to changes in settings.
 
void NetworkServerDoMove (ClientID client_id, CompanyID company_id)
 Handle the tid-bits of moving a client from one company to another. More...
 
void NetworkServerSendRcon (ClientID client_id, TextColour colour_code, const std::string &string)
 Send an rcon reply to the client. More...
 
void NetworkServerKickClient (ClientID client_id, const std::string &reason)
 Kick a single client. More...
 
uint NetworkServerKickOrBanIP (ClientID client_id, bool ban, const std::string &reason)
 Ban, or kick, everyone joined from the given client's IP. More...
 
uint NetworkServerKickOrBanIP (const std::string &ip, bool ban, const std::string &reason)
 Kick or ban someone based on an IP address. More...
 
bool NetworkCompanyHasClients (CompanyID company)
 Check whether a particular company has clients. More...
 
void NetworkPrintClients ()
 Print all the clients to the console.
 
std::string_view NetworkGetPublicKeyOfClient (ClientID client_id)
 Get the public key of the client with the given id. More...
 
void NetworkServerNewCompany (const Company *c, NetworkClientInfo *ci)
 Perform all the server specific administration of a new company. More...
 

Variables

static ClientID _network_client_id = CLIENT_ID_FIRST
 The identifier counter for new clients (is never decreased)
 
NetworkClientSocketPool _networkclientsocket_pool ("NetworkClientSocket")
 Make very sure the preconditions given in network_type.h are actually followed. More...
 
static IntervalTimer< TimerGameRealtime_network_restart_map_timer ({std::chrono::hours::zero(), TimerGameRealtime::UNPAUSED}, [](auto) { if(!_network_server) return;if(_settings_client.network.restart_hours==0) return;Debug(net, 3, "Auto-restarting map: {} hours played", _settings_client.network.restart_hours);NetworkRestartMap();})
 Timer to restart a network server automatically based on real-time hours played. More...
 
static IntervalTimer< TimerGameCalendar_calendar_network_yearly ({ TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE }, [](auto) { if(!_network_server) return;NetworkCheckRestartMapYear();})
 Calendar yearly "callback". More...
 
static IntervalTimer< TimerGameEconomy_economy_network_yearly ({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::NONE}, [](auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);})
 Economy yearly "callback". More...
 
static IntervalTimer< TimerGameEconomy_network_quarterly ({TimerGameEconomy::QUARTER, TimerGameEconomy::Priority::NONE}, [](auto) { if(!_network_server) return;NetworkAutoCleanCompanies();NetworkAdminUpdate(ADMIN_FREQUENCY_QUARTERLY);})
 Quarterly "callback". More...
 
static IntervalTimer< TimerGameEconomy_network_monthly ({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::NONE}, [](auto) { if(!_network_server) return;NetworkAutoCleanCompanies();NetworkAdminUpdate(ADMIN_FREQUENCY_MONTHLY);})
 Economy monthly "callback". More...
 
static IntervalTimer< TimerGameEconomy_network_weekly ({TimerGameEconomy::WEEK, TimerGameEconomy::Priority::NONE}, [](auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);})
 Economy weekly "callback". More...
 
static IntervalTimer< TimerGameEconomy_economy_network_daily ({TimerGameEconomy::DAY, TimerGameEconomy::Priority::NONE}, [](auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_DAILY);})
 Daily "callback". More...
 

Detailed Description

Server part of the network protocol.

Definition in file network_server.cpp.

Function Documentation

◆ ChangeNetworkRestartTime()

void ChangeNetworkRestartTime ( bool  reset)

Reset the automatic network restart time interval.

Parameters
resetWhether to reset the timer to zero.

Definition at line 1853 of file network_server.cpp.

References _network_restart_map_timer, _network_server, _settings_client, ClientSettings::network, NetworkSettings::restart_hours, and TimerGameRealtime::UNPAUSED.

Referenced by NetworkOnGameStart().

◆ NetworkCompanyHasClients()

bool NetworkCompanyHasClients ( CompanyID  company)

Check whether a particular company has clients.

Parameters
companyThe company to check.
Returns
True if at least one client is joined to the company.

Definition at line 2091 of file network_server.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientinfo_pool >::Iterate().

Referenced by AdminCompanyResetCallback().

◆ NetworkGetPublicKeyOfClient()

std::string_view NetworkGetPublicKeyOfClient ( ClientID  client_id)

Get the public key of the client with the given id.

Parameters
client_idThe id of the client.
Returns
View of the public key, which is empty when the client does not exist.

Definition at line 2139 of file network_server.cpp.

◆ NetworkHandleCommandQueue()

static void NetworkHandleCommandQueue ( NetworkClientSocket cs)
static

Handle the command-queue of a socket.

Parameters
csThe socket to handle the queue for.

Definition at line 1675 of file network_server.cpp.

◆ NetworkMakeClientNameUnique()

bool NetworkMakeClientNameUnique ( std::string &  name)

Check whether a name is unique, and otherwise try to make it unique.

Parameters
new_nameThe name to check/modify.
Returns
True if an unique name was achieved.

Definition at line 1614 of file network_server.cpp.

References CLIENT_ID_SERVER, NetworkClientInfo::client_name, NetworkClientInfo::GetByClientID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientinfo_pool >::Iterate(), MAX_CLIENTS, and NETWORK_CLIENT_NAME_LENGTH.

Referenced by NetworkUpdateClientName().

◆ NetworkPopulateCompanyStats()

◆ NetworkServer_Tick()

void NetworkServer_Tick ( bool  send_frame)

This is called every tick if this is a _network_server.

Parameters
send_frameWhether to send the frame to the clients.

Definition at line 1685 of file network_server.cpp.

References _frame_counter, _last_sync_frame, _settings_client, NetworkSettings::bytes_per_frame, NetworkSettings::bytes_per_frame_burst, ClientSettings::network, and NetworkSettings::sync_freq.

◆ NetworkServerChangeClientName()

bool NetworkServerChangeClientName ( ClientID  client_id,
const std::string &  new_name 
)

Change the client name of the given client.

Parameters
client_idthe client to change the name of
new_namethe new name for the client
Returns
true iff the name was changed

Definition at line 1653 of file network_server.cpp.

References NetworkClientInfo::GetByClientID(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientinfo_pool >::Iterate().

◆ NetworkServerDoMove()

void NetworkServerDoMove ( ClientID  client_id,
CompanyID  company_id 
)

Handle the tid-bits of moving a client from one company to another.

Parameters
client_idid of the client we want to move.
company_idid of the company we want to move the client to.
Returns
void

Definition at line 1983 of file network_server.cpp.

References _network_dedicated, CLIENT_ID_SERVER, NetworkClientInfo::client_playas, COMPANY_SPECTATOR, NetworkClientInfo::GetByClientID(), NetworkUpdateClientInfo(), and SetLocalCompany().

Referenced by NetworkClientListWindow::OnClickCompanyJoin().

◆ NetworkServerKickClient()

void NetworkServerKickClient ( ClientID  client_id,
const std::string &  reason 
)

Kick a single client.

Parameters
client_idThe client to kick.
reasonIn case of kicking a client, specifies the reason for kicking the client.

Definition at line 2030 of file network_server.cpp.

References CLIENT_ID_SERVER.

Referenced by AdminClientKickCallback(), and NetworkServerKickOrBanIP().

◆ NetworkServerKickOrBanIP() [1/2]

uint NetworkServerKickOrBanIP ( ClientID  client_id,
bool  ban,
const std::string &  reason 
)

Ban, or kick, everyone joined from the given client's IP.

Parameters
client_idThe client to check for.
banWhether to ban or kick.
reasonIn case of kicking a client, specifies the reason for kicking the client.

Definition at line 2042 of file network_server.cpp.

References NetworkServerKickOrBanIP().

Referenced by AdminClientBanCallback(), and NetworkServerKickOrBanIP().

◆ NetworkServerKickOrBanIP() [2/2]

uint NetworkServerKickOrBanIP ( const std::string &  ip,
bool  ban,
const std::string &  reason 
)

Kick or ban someone based on an IP address.

Parameters
ipThe IP address/range to ban/kick.
banWhether to ban or just kick.
reasonIn case of kicking a client, specifies the reason for kicking the client.

Definition at line 2053 of file network_server.cpp.

References _network_ban_list, _redirect_console_to_client, CLIENT_ID_SERVER, and NetworkServerKickClient().

◆ NetworkServerNewCompany()

void NetworkServerNewCompany ( const Company c,
NetworkClientInfo ci 
)

Perform all the server specific administration of a new company.

Parameters
cThe newly created company; can't be nullptr.
ciThe client information of the client that made the company; can be nullptr.

Definition at line 2151 of file network_server.cpp.

References _network_server, CALCA_ADD, NetworkClientInfo::client_id, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, NetworkServerSendChat(), NetworkUpdateClientInfo(), and NetworkClientInfo::public_key.

Referenced by CmdCompanyCtrl(), and NetworkOnGameStart().

◆ NetworkServerSendChat()

void NetworkServerSendChat ( NetworkAction  action,
DestType  desttype,
int  dest,
const std::string &  msg,
ClientID  from_id,
int64_t  data,
bool  from_admin 
)

Send an actual chat message.

Parameters
actionThe action that's performed.
desttypeThe type of destination.
destThe actual destination index.
msgThe actual message.
from_idThe origin of the message.
dataArbitrary data.
from_adminWhether the origin is an admin or not.

Definition at line 1258 of file network_server.cpp.

References CLIENT_ID_SERVER, DESTTYPE_CLIENT, and NetworkClientInfo::GetByClientID().

Referenced by NetworkServerNewCompany().

◆ NetworkServerSendExternalChat()

void NetworkServerSendExternalChat ( const std::string &  source,
TextColour  colour,
const std::string &  user,
const std::string &  msg 
)

Send a chat message from external source.

Parameters
sourceName of the source this message came from.
colourTextColour to use for the message.
userName of the user who sent the messsage.
msgThe actual message.

Definition at line 1376 of file network_server.cpp.

References ServerNetworkGameSocketHandler::STATUS_AUTHORIZED.

◆ NetworkServerSendRcon()

void NetworkServerSendRcon ( ClientID  client_id,
TextColour  colour_code,
const std::string &  string 
)

Send an rcon reply to the client.

Parameters
client_idThe identifier of the client.
colour_codeThe colour of the text.
stringThe actual reply.

Definition at line 2020 of file network_server.cpp.

Referenced by IConsolePrint().

◆ NetworkUpdateClientInfo()

void NetworkUpdateClientInfo ( ClientID  client_id)

Variable Documentation

◆ _calendar_network_yearly

IntervalTimer<TimerGameCalendar> _calendar_network_yearly({ TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE },[](auto) { if(!_network_server) return;NetworkCheckRestartMapYear();}) ( { TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE ,
[] (auto) { if(!_network_server) return;NetworkCheckRestartMapYear();}   
)
static

Calendar yearly "callback".

Called whenever the calendar year changes.

◆ _economy_network_daily

IntervalTimer<TimerGameEconomy> _economy_network_daily({TimerGameEconomy::DAY, TimerGameEconomy::Priority::NONE},[](auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_DAILY);}) ( {TimerGameEconomy::DAY, TimerGameEconomy::Priority::NONE ,
[] (auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_DAILY);}   
)
static

Daily "callback".

Called whenever the economy date changes.

◆ _economy_network_yearly

IntervalTimer<TimerGameEconomy> _economy_network_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::NONE},[](auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);}) ( {TimerGameEconomy::YEAR, TimerGameEconomy::Priority::NONE ,
[] (auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);}   
)
static

Economy yearly "callback".

Called whenever the economy year changes.

◆ _network_monthly

Economy monthly "callback".

Called whenever the economy month changes.

◆ _network_quarterly

Quarterly "callback".

Called whenever the economy quarter changes.

◆ _network_restart_map_timer

IntervalTimer<TimerGameRealtime> _network_restart_map_timer({std::chrono::hours::zero(), TimerGameRealtime::UNPAUSED},[](auto) { if(!_network_server) return; if(_settings_client.network.restart_hours==0) return;Debug(net, 3, "Auto-restarting map: {} hours played", _settings_client.network.restart_hours);NetworkRestartMap();}) ( {std::chrono::hours::zero(), TimerGameRealtime::UNPAUSED ,
[] (auto) { if(!_network_server) return;if(_settings_client.network.restart_hours==0) return;Debug(net, 3, "Auto-restarting map: {} hours played", _settings_client.network.restart_hours);NetworkRestartMap();}   
)
static

Timer to restart a network server automatically based on real-time hours played.

Initialized at zero to disable until settings are loaded.

Referenced by ChangeNetworkRestartTime().

◆ _network_weekly

IntervalTimer<TimerGameEconomy> _network_weekly({TimerGameEconomy::WEEK, TimerGameEconomy::Priority::NONE},[](auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);}) ( {TimerGameEconomy::WEEK, TimerGameEconomy::Priority::NONE ,
[] (auto) { if(!_network_server) return;NetworkAdminUpdate(ADMIN_FREQUENCY_WEEKLY);}   
)
static

Economy weekly "callback".

Called whenever the economy week changes.

◆ _networkclientsocket_pool

NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket") ( "NetworkClientSocket"  )

Make very sure the preconditions given in network_type.h are actually followed.

Yes... The pool with clients.