OpenTTD Source  12.0-beta2
network_server.cpp File Reference
#include "../stdafx.h"
#include "../strings_func.h"
#include "../date_func.h"
#include "core/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 "../roadveh.h"
#include "../order_backup.h"
#include "../core/pool_func.hpp"
#include "../core/random_func.hpp"
#include "../rev.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 void NetworkHandleCommandQueue (NetworkClientSocket *cs)
 Handle the command-queue of a socket. More...
 
void NetworkServerSendChat (NetworkAction action, DestType desttype, int dest, const std::string &msg, ClientID from_id, int64 data, bool from_admin)
 Send an actual chat message. 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 NetworkCheckRestartMap ()
 Check if we want to restart the map.
 
static void NetworkAutoCleanCompanies ()
 Check if the server has autoclean_companies activated Two things happen: 1) If a company is not protected, it is closed after 1 year (for example) 2) If a company is protected, protection is disabled after 3 years (for example) (and item 1. More...
 
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 NetworkServerSetCompanyPassword (CompanyID company_id, const std::string &password, bool already_hashed)
 Set/Reset a company password on the server end. More...
 
void NetworkServer_Tick (bool send_frame)
 This is called every tick if this is a _network_server. More...
 
void NetworkServerYearlyLoop ()
 Yearly "callback". More...
 
void NetworkServerMonthlyLoop ()
 Monthly "callback". More...
 
void NetworkServerDailyLoop ()
 Daily "callback". More...
 
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 NetworkServerUpdateCompanyPassworded (CompanyID company_id, bool passworded)
 Tell that a particular company is (not) passworded. More...
 
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.
 
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...
 

Detailed Description

Server part of the network protocol.

Definition in file network_server.cpp.

Function Documentation

◆ NetworkAutoCleanCompanies()

◆ 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 1975 of file network_server.cpp.

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

Referenced by AdminCompanyResetCallback().

◆ 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 1621 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 1541 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 1634 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 1580 of file network_server.cpp.

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

◆ NetworkServerDailyLoop()

void NetworkServerDailyLoop ( )

Daily "callback".

Called whenever the date changes.

Definition at line 1781 of file network_server.cpp.

References _date, ADMIN_FREQUENCY_DAILY, ADMIN_FREQUENCY_WEEKLY, and NetworkAdminUpdate().

◆ 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 1868 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 1914 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 1926 of file network_server.cpp.

Referenced by AdminClientBanCallback().

◆ 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 1937 of file network_server.cpp.

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

◆ NetworkServerMonthlyLoop()

void NetworkServerMonthlyLoop ( )

Monthly "callback".

Called whenever the month changes.

Definition at line 1773 of file network_server.cpp.

References _cur_month, ADMIN_FREQUENCY_MONTHLY, ADMIN_FREQUENCY_QUARTERLY, NetworkAdminUpdate(), and NetworkAutoCleanCompanies().

◆ NetworkServerNewCompany()

void NetworkServerNewCompany ( const Company c,
NetworkClientInfo ci 
)

◆ NetworkServerSendChat()

void NetworkServerSendChat ( NetworkAction  action,
DestType  desttype,
int  dest,
const std::string &  msg,
ClientID  from_id,
int64  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 1153 of file network_server.cpp.

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

Referenced by NetworkServerNewCompany().

◆ 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 1904 of file network_server.cpp.

Referenced by IConsolePrint().

◆ NetworkServerSetCompanyPassword()

void NetworkServerSetCompanyPassword ( CompanyID  company_id,
const std::string &  password,
bool  already_hashed 
)

Set/Reset a company password on the server end.

Parameters
company_idID of the company the password should be changed for.
passwordThe new password.
already_hashedIs the given password already hashed?

Definition at line 1604 of file network_server.cpp.

References _network_company_states, _settings_client, _settings_game, GameSettings::game_creation, GenerateCompanyPasswordHash(), GameCreationSettings::generation_seed, Company::IsValidHumanID(), ClientSettings::network, NetworkSettings::network_id, NetworkServerUpdateCompanyPassworded(), and NetworkCompanyState::password.

Referenced by AdminCompanyUnlockCallback(), and NetworkChangeCompanyPassword().

◆ NetworkServerUpdateCompanyPassworded()

void NetworkServerUpdateCompanyPassworded ( CompanyID  company_id,
bool  passworded 
)

Tell that a particular company is (not) passworded.

Parameters
company_idThe company that got/removed the password.
passwordedWhether the password was received or removed.

Definition at line 1848 of file network_server.cpp.

References _network_company_passworded, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), NetworkAdminCompanyUpdate(), NetworkCompanyIsPassworded(), SB(), SetWindowClassesDirty(), and WC_COMPANY.

Referenced by NetworkAutoCleanCompanies(), NetworkServerNewCompany(), and NetworkServerSetCompanyPassword().

◆ NetworkServerYearlyLoop()

void NetworkServerYearlyLoop ( )

Yearly "callback".

Called whenever the year changes.

Definition at line 1766 of file network_server.cpp.

References ADMIN_FREQUENCY_ANUALLY, NetworkAdminUpdate(), and NetworkCheckRestartMap().

◆ NetworkUpdateClientInfo()

void NetworkUpdateClientInfo ( ClientID  client_id)

Send updated client info of a particular client.

Parameters
client_idThe client to send it for.

Definition at line 1426 of file network_server.cpp.

References _date, _date_fract, NetworkClientInfo::client_playas, Debug, NetworkClientInfo::GetByClientID(), NetworkAdminClientUpdate(), and ServerNetworkGameSocketHandler::STATUS_AUTHORIZED.

Referenced by NetworkServerDoMove(), and NetworkServerNewCompany().

Variable Documentation

◆ _networkclientsocket_pool

NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket")

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

Yes... The pool with clients.