OpenTTD Source
20241108-master-g80f628063a
|
Network functions used by other parts of OpenTTD. More...
#include "network_type.h"
#include "../console_type.h"
#include "../gfx_type.h"
#include "../openttd.h"
#include "../company_type.h"
#include "../string_type.h"
Go to the source code of this file.
Functions | |
uint8_t | NetworkSpectatorCount () |
bool | NetworkIsValidClientName (const std::string_view client_name) |
Check whether the given client name is deemed valid for use in network games. More... | |
bool | NetworkValidateOurClientName () |
Convenience method for NetworkValidateClientName on _settings_client.network.client_name. More... | |
bool | NetworkValidateClientName (std::string &client_name) |
Trim the given client name in place, i.e. More... | |
bool | NetworkValidateServerName (std::string &server_name) |
Trim the given server name in place, i.e. More... | |
void | NetworkUpdateClientName (const std::string &client_name) |
Send the server our name as callback from the setting. More... | |
void | NetworkUpdateServerGameType () |
The setting server_game_type was updated; possibly we need to take some action. | |
bool | NetworkCompanyHasClients (CompanyID company) |
Check whether a particular company has clients. More... | |
void | NetworkReboot () |
void | NetworkDisconnect (bool close_admins=true) |
We want to disconnect from the host/clients. More... | |
void | NetworkGameLoop () |
void | NetworkBackgroundLoop () |
We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer. More... | |
std::string_view | ParseFullConnectionString (const std::string &connection_string, uint16_t &port, CompanyID *company_id=nullptr) |
Converts a string to ip/port/company Format: IP:port::company. More... | |
void | NetworkPopulateCompanyStats (NetworkCompanyStats *stats) |
Populate the company stats. More... | |
void | NetworkUpdateClientInfo (ClientID client_id) |
Send updated client info of a particular client. More... | |
void | NetworkClientsToSpectators (CompanyID cid) |
Move the clients of a company to the spectators. More... | |
bool | NetworkClientConnectGame (const std::string &connection_string, CompanyID default_company, const std::string &join_server_password="") |
Join a client to the server at with the given connection string. More... | |
void | NetworkClientJoinGame () |
Actually perform the joining to the server. More... | |
void | NetworkClientRequestMove (CompanyID company) |
Notify the server of this client wanting to be moved to another company. More... | |
void | NetworkClientSendRcon (const std::string &password, const std::string &command) |
Send a remote console command. More... | |
void | NetworkClientSendChat (NetworkAction action, DestType type, int dest, const std::string &msg, int64_t data=0) |
Send a chat message. More... | |
bool | NetworkClientPreferTeamChat (const NetworkClientInfo *cio) |
Tell whether the client has team members who they can chat to. More... | |
uint | NetworkMaxCompaniesAllowed () |
Get the maximum number of companies that are allowed by the server. More... | |
bool | NetworkMaxCompaniesReached () |
Check if max_companies has been reached on the server (local check only). 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 | NetworkHandlePauseChange (PauseMode prev_mode, PauseMode changed_mode) |
Handle the pause mode change so we send the right messages to the chat. More... | |
void | NetworkOnGameStart () |
Perform tasks when the server is started. More... | |
void | NetworkServerSendConfigUpdate () |
Send Config Update. | |
void | NetworkServerUpdateGameInfo () |
Update the server's NetworkServerGameInfo due to changes in settings. | |
void | NetworkServerShowStatusToConsole () |
Show the status message of all clients on the console. | |
bool | NetworkServerStart () |
void | NetworkServerNewCompany (const Company *company, NetworkClientInfo *ci) |
Perform all the server specific administration of a new company. More... | |
bool | NetworkServerChangeClientName (ClientID client_id, const std::string &new_name) |
Change the client name of the given client. More... | |
bool | NetworkCanJoinCompany (CompanyID company_id) |
Returns whether the given company can be joined by this client. 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 | NetworkServerSendChat (NetworkAction action, DestType type, int dest, const std::string &msg, ClientID from_id, int64_t data=0, bool from_admin=false) |
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 | 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... | |
void | NetworkInitChatMessage () |
Initialize all buffers of the chat visualisation. | |
void | NetworkReInitChatBoxSize () |
Initialize all font-dependent chat box sizes. | |
void CDECL | NetworkAddChatMessage (TextColour colour, uint duration, const std::string &message) |
Add a text message to the 'chat window' to be shown. More... | |
void | NetworkUndrawChatMessage () |
Hide the chatbox. | |
void | NetworkAfterNewGRFScan () |
Rebuild the GRFConfig's of the servers in the game list as we did a rescan and might have found new NewGRFs. | |
Variables | |
ClientID | _network_own_client_id |
Uncomment the following define to enable command replaying. More... | |
ClientID | _redirect_console_to_client |
If not invalid, redirect the console output to a client. | |
uint8_t | _network_reconnect |
Reconnect timeout. | |
StringList | _network_bind_list |
The addresses to bind on. | |
StringList | _network_host_list |
The servers we know. | |
StringList | _network_ban_list |
The banned clients. | |
Network functions used by other parts of OpenTTD.
Definition in file network_func.h.
void CDECL NetworkAddChatMessage | ( | TextColour | colour, |
uint | duration, | ||
const std::string & | message | ||
) |
Add a text message to the 'chat window' to be shown.
colour | The colour this message is to be shown in |
duration | The duration of the chat message in seconds |
message | message itself |
Definition at line 86 of file network_chat_gui.cpp.
References _chatmessage_dirty, _chatmessage_dirty_time, _chatmsg_list, ChatMessage::colour, MAX_CHAT_MESSAGES, ChatMessage::message, and ChatMessage::remove_time.
void NetworkBackgroundLoop | ( | ) |
We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer.
For example stuff needed for finding servers or downloading content.
Definition at line 1082 of file network.cpp.
References _network_content_client, _network_coordinator_client, TCPConnecter::CheckCallbacks(), NetworkHTTPSocketHandler::HTTPReceive(), NetworkBackgroundUDPLoop(), ClientNetworkContentSocketHandler::SendReceive(), ClientNetworkCoordinatorSocketHandler::SendReceive(), and QueryNetworkGameSocketHandler::SendReceive().
bool NetworkCanJoinCompany | ( | CompanyID | company_id | ) |
Returns whether the given company can be joined by this client.
company_id | The id of the company. |
true
when this company is allowed to join, otherwise false
. Definition at line 143 of file network.cpp.
References _network_own_client_id, NetworkClientInfo::CanJoinCompany(), and NetworkClientInfo::GetByClientID().
Referenced by CompanyWindow::OnPaint().
bool NetworkClientConnectGame | ( | const std::string & | connection_string, |
CompanyID | default_company, | ||
const std::string & | join_server_password | ||
) |
Join a client to the server at with the given connection string.
The default for the passwords is nullptr
. When the server needs a password and none is given, the user is asked to enter the password in the GUI. This function will return false whenever some information required to join is not correct such as the company number or the client's name, or when there is not networking avalabile at all. If the function returns false the connection with the existing server is not disconnected. It will return true when it starts the actual join process, i.e. when it actually shows the join status window.
connection_string | The IP address, port and company number to join as. |
default_company | The company number to join as when none is given. |
join_server_password | The password for the server. |
Definition at line 787 of file network.cpp.
References _network_available, _network_join, NetworkJoinInfo::company, ServerAddress::connection_string, NetworkJoinInfo::connection_string, Debug, NETWORK_DEFAULT_PORT, NetworkValidateOurClientName(), ServerAddress::Parse(), and NetworkJoinInfo::server_password.
void NetworkClientJoinGame | ( | ) |
Actually perform the joining to the server.
Use NetworkClientConnectGame when you want to connect to a specific server/company. This function assumes _network_join is already fully set up.
Definition at line 820 of file network.cpp.
References NetworkDisconnect().
bool NetworkClientPreferTeamChat | ( | const NetworkClientInfo * | cio | ) |
Tell whether the client has team members who they can chat to.
cio | client to check members of. |
Definition at line 1373 of file network_client.cpp.
References _settings_client, NetworkClientInfo::client_playas, ClientSettings::gui, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientinfo_pool >::Iterate(), and GUISettings::prefer_teamchat.
void NetworkClientRequestMove | ( | CompanyID | company_id | ) |
Notify the server of this client wanting to be moved to another company.
company_id | id of the company the client wishes to be moved to. |
Definition at line 1256 of file network_client.cpp.
References ClientNetworkGameSocketHandler::SendMove().
Referenced by NetworkClientListWindow::OnClickCompanyJoin().
void NetworkClientSendChat | ( | NetworkAction | action, |
DestType | type, | ||
int | dest, | ||
const std::string & | msg, | ||
int64_t | data | ||
) |
Send a chat message.
action | The action associated with the message. |
type | The destination type. |
dest | The destination index, be it a company index or client id. |
msg | The actual message. |
data | Arbitrary extra data. |
Definition at line 1363 of file network_client.cpp.
References ClientNetworkGameSocketHandler::SendChat().
void NetworkClientSendRcon | ( | const std::string & | password, |
const std::string & | command | ||
) |
Send a remote console command.
password | The password. |
command | The command to execute. |
Definition at line 1247 of file network_client.cpp.
References ClientNetworkGameSocketHandler::SendRCon().
void NetworkClientsToSpectators | ( | CompanyID | cid | ) |
Move the clients of a company to the spectators.
cid | The company to move the clients of. |
Definition at line 1265 of file network_client.cpp.
References _current_company, _local_company, COMPANY_SPECTATOR, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientinfo_pool >::Iterate(), and SetLocalCompany().
Referenced by ChangeOwnershipOfCompanyItems().
bool NetworkCompanyHasClients | ( | CompanyID | company | ) |
Check whether a particular company has clients.
company | The company to check. |
Definition at line 2091 of file network_server.cpp.
Referenced by AdminCompanyResetCallback().
void NetworkDisconnect | ( | bool | close_admins | ) |
We want to disconnect from the host/clients.
close_admins | Whether the admin sockets need to be closed as well. |
Definition at line 1001 of file network.cpp.
References _network_server, CloseWindowById(), ServerNetworkAdminSocketHandler::IterateActive(), NetworkClose(), NetworkUDPInitialize(), WC_NETWORK_STATUS_WINDOW, and WN_NETWORK_STATUS_WINDOW_JOIN.
Referenced by NetworkClientJoinGame(), and NetworkShutDown().
std::string_view NetworkGetPublicKeyOfClient | ( | ClientID | client_id | ) |
Get the public key of the client with the given id.
client_id | The id of the client. |
Definition at line 2139 of file network_server.cpp.
Handle the pause mode change so we send the right messages to the chat.
prev_mode | The previous pause mode. |
changed_mode | The pause mode that got changed. |
Definition at line 356 of file network.cpp.
References _networking, _pause_mode, PM_PAUSED_ACTIVE_CLIENTS, PM_PAUSED_GAME_SCRIPT, PM_PAUSED_JOIN, PM_PAUSED_LINK_GRAPH, PM_PAUSED_NORMAL, PM_UNPAUSED, and SetDParam().
Referenced by CmdPause().
bool NetworkIsValidClientName | ( | const std::string_view | client_name | ) |
Check whether the given client name is deemed valid for use in network games.
An empty name (null or '') is not valid as that is essentially no name at all. A name starting with white space is not valid for tab completion purposes.
client_name | The client name to check for validity. |
Definition at line 1287 of file network_client.cpp.
Referenced by NetworkValidateClientName().
uint NetworkMaxCompaniesAllowed | ( | ) |
Get the maximum number of companies that are allowed by the server.
Definition at line 1389 of file network_client.cpp.
References _network_server, _network_server_max_companies, _settings_client, NetworkSettings::max_companies, and ClientSettings::network.
Referenced by NetworkMaxCompaniesReached().
bool NetworkMaxCompaniesReached | ( | ) |
Check if max_companies has been reached on the server (local check only).
Definition at line 1398 of file network_client.cpp.
References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetNumItems(), and NetworkMaxCompaniesAllowed().
Referenced by NetworkClientListWindow::RebuildList().
void NetworkOnGameStart | ( | ) |
Perform tasks when the server is started.
This consists of things like putting the server's client in a valid company and resetting the restart time.
Definition at line 947 of file network.cpp.
References _network_dedicated, _network_server, ChangeNetworkRestartTime(), CLIENT_ID_SERVER, NetworkClientInfo::client_playas, NetworkClientInfo::GetByClientID(), GetFirstPlayableCompanyID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, CompanyProperties::name, CompanyProperties::name_1, NetworkServerNewCompany(), and NetworkServerUpdateGameInfo().
Referenced by OnStartGame().
void NetworkPopulateCompanyStats | ( | NetworkCompanyStats * | stats | ) |
Populate the company stats.
stats | the stats to update |
Definition at line 1499 of file network_server.cpp.
References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Iterate(), MAX_COMPANIES, and VEH_TRAIN.
Referenced by ServerNetworkAdminSocketHandler::SendCompanyStats().
bool NetworkServerChangeClientName | ( | ClientID | client_id, |
const std::string & | new_name | ||
) |
Change the client name of the given client.
client_id | the client to change the name of |
new_name | the new name for the client |
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().
Handle the tid-bits of moving a client from one company to another.
client_id | id of the client we want to move. |
company_id | id of the company we want to move the client to. |
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().
void NetworkServerKickClient | ( | ClientID | client_id, |
const std::string & | reason | ||
) |
Kick a single client.
client_id | The client to kick. |
reason | In 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().
uint NetworkServerKickOrBanIP | ( | ClientID | client_id, |
bool | ban, | ||
const std::string & | reason | ||
) |
Ban, or kick, everyone joined from the given client's IP.
client_id | The client to check for. |
ban | Whether to ban or kick. |
reason | In 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().
uint NetworkServerKickOrBanIP | ( | const std::string & | ip, |
bool | ban, | ||
const std::string & | reason | ||
) |
Kick or ban someone based on an IP address.
ip | The IP address/range to ban/kick. |
ban | Whether to ban or just kick. |
reason | In 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().
void NetworkServerNewCompany | ( | const Company * | c, |
NetworkClientInfo * | ci | ||
) |
Perform all the server specific administration of a new company.
c | The newly created company; can't be nullptr. |
ci | The 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().
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.
action | The action that's performed. |
desttype | The type of destination. |
dest | The actual destination index. |
msg | The actual message. |
from_id | The origin of the message. |
data | Arbitrary data. |
from_admin | Whether 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().
void NetworkServerSendExternalChat | ( | const std::string & | source, |
TextColour | colour, | ||
const std::string & | user, | ||
const std::string & | msg | ||
) |
Send a chat message from external source.
source | Name of the source this message came from. |
colour | TextColour to use for the message. |
user | Name of the user who sent the messsage. |
msg | The actual message. |
Definition at line 1376 of file network_server.cpp.
References ServerNetworkGameSocketHandler::STATUS_AUTHORIZED.
void NetworkServerSendRcon | ( | ClientID | client_id, |
TextColour | colour_code, | ||
const std::string & | string | ||
) |
Send an rcon reply to the client.
client_id | The identifier of the client. |
colour_code | The colour of the text. |
string | The actual reply. |
Definition at line 2020 of file network_server.cpp.
Referenced by IConsolePrint().
void NetworkUpdateClientInfo | ( | ClientID | client_id | ) |
Send updated client info of a particular client.
client_id | The client to send it for. |
Definition at line 1535 of file network_server.cpp.
References NetworkClientInfo::client_playas, TimerGameEconomy::date, TimerGameEconomy::date_fract, Debug, NetworkClientInfo::GetByClientID(), NetworkAdminClientUpdate(), and ServerNetworkGameSocketHandler::STATUS_AUTHORIZED.
Referenced by CmdCompanyCtrl(), NetworkServerDoMove(), and NetworkServerNewCompany().
void NetworkUpdateClientName | ( | const std::string & | client_name | ) |
Send the server our name as callback from the setting.
newname | The new client name. |
Definition at line 1334 of file network_client.cpp.
References _network_own_client_id, _network_server, NetworkClientInfo::client_name, NetworkClientInfo::GetByClientID(), NetworkMakeClientNameUnique(), and ClientNetworkGameSocketHandler::SendSetName().
bool NetworkValidateClientName | ( | std::string & | client_name | ) |
Trim the given client name in place, i.e.
remove leading and trailing spaces. After the trim check whether the client name is valid. A client name is valid whenever the name is not empty and does not start with spaces. This check is done via NetworkIsValidClientName
. When the client name is valid, this function returns true. When the client name is not valid a GUI error message is shown telling the user to set the client name and this function returns false.
This function is not suitable for ensuring a valid client name at the server as the error message will then be shown to the host instead of the client.
client_name | The client name to validate. It will be trimmed of leading and trailing spaces. |
Definition at line 1309 of file network_client.cpp.
References INVALID_STRING_ID, NetworkIsValidClientName(), ShowErrorMessage(), StrTrimInPlace(), and WL_ERROR.
Referenced by NetworkValidateOurClientName().
bool NetworkValidateOurClientName | ( | ) |
Convenience method for NetworkValidateClientName on _settings_client.network.client_name.
It trims the client name and checks whether it is empty. When it is empty an error message is shown to the GUI user. See NetworkValidateClientName(char*)
for details about the functionality.
Definition at line 1325 of file network_client.cpp.
References _settings_client, NetworkSettings::client_name, ClientSettings::network, and NetworkValidateClientName().
Referenced by NetworkClientConnectGame().
bool NetworkValidateServerName | ( | std::string & | server_name | ) |
Trim the given server name in place, i.e.
remove leading and trailing spaces. After the trim check whether the server name is not empty. When the server name is empty a GUI error message is shown telling the user to set the servername and this function returns false.
server_name | The server name to validate. It will be trimmed of leading and trailing spaces. |
Definition at line 860 of file network.cpp.
References INVALID_STRING_ID, ShowErrorMessage(), StrTrimInPlace(), and WL_ERROR.
std::string_view ParseFullConnectionString | ( | const std::string & | connection_string, |
uint16_t & | port, | ||
CompanyID * | company_id | ||
) |
Converts a string to ip/port/company Format: IP:port::company.
Returns the IP part as a string view into the passed string. This view is valid as long the passed connection string is valid. If there is no port present in the connection string, the port reference will not be touched. When there is no company ID present in the connection string or company_id is nullptr, then company ID will not be touched.
connection_string | The string with the connection data. |
port | The port reference to set. |
company_id | The company ID to set, if available. |
Definition at line 523 of file network.cpp.
|
extern |
Uncomment the following define to enable command replaying.
See docs/desync.md for details.
Uncomment the following define to enable command replaying.
Definition at line 70 of file network.cpp.
Referenced by CmdCompanyCtrl(), CmdScrollViewport(), NetworkAdminCmdLogging(), NetworkCanJoinCompany(), NetworkUpdateClientName(), NetworkClientListWindow::OnClickCompanyNew(), NetworkClientListWindow::RebuildList(), ClientNetworkGameSocketHandler::Receive_SERVER_NEWGAME(), ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(), and OrderBackup::Reset().