OpenTTD Source  1.11.0-beta2
network.cpp File Reference
#include "../stdafx.h"
#include "../strings_func.h"
#include "../command_func.h"
#include "../date_func.h"
#include "network_admin.h"
#include "network_client.h"
#include "network_server.h"
#include "network_content.h"
#include "network_udp.h"
#include "network_gamelist.h"
#include "network_base.h"
#include "core/udp.h"
#include "core/host.h"
#include "network_gui.h"
#include "../console_func.h"
#include "../3rdparty/md5/md5.h"
#include "../core/random_func.hpp"
#include "../window_func.h"
#include "../company_func.h"
#include "../company_base.h"
#include "../landscape_type.h"
#include "../rev.h"
#include "../core/pool_func.hpp"
#include "../gfx_func.h"
#include "../error.h"
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

class  TCPQueryConnecter
 Non blocking connection create to query servers. More...
 
class  TCPClientConnecter
 Non blocking connection create to actually connect to servers. More...
 

Functions

void StateGameLoop ()
 State controlling game loop. More...
 
bool HasClients ()
 Return whether there is any client connected or trying to connect at all. More...
 
byte NetworkSpectatorCount ()
 
const char * NetworkChangeCompanyPassword (CompanyID company_id, const char *password)
 Change the company password of a given company. More...
 
const char * GenerateCompanyPasswordHash (const char *password, const char *password_server_id, uint32 password_game_seed)
 Hash the given password using server ID and game seed. More...
 
bool NetworkCompanyIsPassworded (CompanyID company_id)
 Check if the company we want to join requires a password. More...
 
void NetworkTextMessage (NetworkAction action, TextColour colour, bool self_send, const char *name, const char *str, int64 data)
 
uint NetworkCalculateLag (const NetworkClientSocket *cs)
 
void NetworkError (StringID error_string)
 
StringID GetNetworkErrorMsg (NetworkErrorCode err)
 Retrieve the string id of an internal error number. More...
 
void NetworkHandlePauseChange (PauseMode prev_mode, PauseMode changed_mode)
 Handle the pause mode change so we send the right messages to the chat. More...
 
static void CheckPauseHelper (bool pause, PauseMode pm)
 Helper function for the pause checkers. More...
 
static uint NetworkCountActiveClients ()
 Counts the number of active clients connected. More...
 
static void CheckMinActiveClients ()
 Check if the minimum number of active clients has been reached and pause or unpause the game as appropriate.
 
static bool NetworkHasJoiningClient ()
 Checks whether there is a joining client. More...
 
static void CheckPauseOnJoin ()
 Check whether we should pause on join.
 
void ParseConnectionString (const char **company, const char **port, char *connection_string)
 Converts a string to ip/port/company Format: IP:port::company. More...
 
static void InitializeNetworkPools (bool close_admins=true)
 Resets the pools used for network clients, and the admin pool if needed. More...
 
void NetworkClose (bool close_admins)
 Close current connections. More...
 
static void NetworkInitialize (bool close_admins=true)
 
void NetworkTCPQueryServer (NetworkAddress address)
 
void NetworkAddServer (const char *b)
 
void GetBindAddresses (NetworkAddressList *addresses, uint16 port)
 Get the addresses to bind to. More...
 
void NetworkRebuildHostList ()
 
void NetworkClientConnectGame (NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
 
static void NetworkInitGameInfo ()
 
bool NetworkServerStart ()
 
void NetworkReboot ()
 
void NetworkDisconnect (bool blocking, bool close_admins)
 We want to disconnect from the host/clients. More...
 
static bool NetworkReceive ()
 Receives something from the network. More...
 
static void NetworkSend ()
 
void NetworkBackgroundLoop ()
 We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer. More...
 
void NetworkGameLoop ()
 
static void NetworkGenerateServerId ()
 
void NetworkStartDebugLog (NetworkAddress address)
 
void NetworkStartUp ()
 This tries to launch the network for a given OS.
 
void NetworkShutDown ()
 This shuts the network down.
 
const char * GetNetworkRevisionString ()
 Get the network version string used by this build. More...
 
static const char * ExtractNetworkRevisionHash (const char *revstr)
 
bool IsNetworkCompatibleVersion (const char *other)
 Checks whether the given version string is compatible with our version. More...
 

Variables

NetworkClientInfoPool _networkclientinfo_pool ("NetworkClientInfo")
 Make sure both pools have the same size. More...
 
bool _networking
 are we in networking mode?
 
bool _network_server
 network-server is active
 
bool _network_available
 is network mode available?
 
bool _network_dedicated
 are we a dedicated server?
 
bool _is_network_server
 Does this client wants to be a network-server?
 
NetworkServerGameInfo _network_game_info
 Information about our game. More...
 
NetworkCompanyState_network_company_states = nullptr
 Statistics about some companies.
 
ClientID _network_own_client_id
 Our client identifier.
 
ClientID _redirect_console_to_client
 If not invalid, redirect the console output to a client.
 
bool _network_need_advertise
 Whether we need to advertise.
 
uint8 _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.
 
uint32 _frame_counter_server
 The frame_counter of the server, if in network-mode.
 
uint32 _frame_counter_max
 To where we may go with our clients.
 
uint32 _frame_counter
 The current frame.
 
uint32 _last_sync_frame
 Used in the server to store the last time a sync packet was sent to clients.
 
NetworkAddressList _broadcast_list
 List of broadcast addresses.
 
uint32 _sync_seed_1
 Seed to compare during sync checks.
 
uint32 _sync_frame
 The frame to perform the sync check.
 
bool _network_first_time
 Whether we have finished joining or not.
 
bool _network_udp_server
 Is the UDP server started?
 
uint16 _network_udp_broadcast
 Timeout for the UDP broadcasts.
 
uint8 _network_advertise_retries
 The number of advertisement retries we did.
 
CompanyMask _network_company_passworded
 Bitmask of the password status of all companies.
 
NetworkUDPSocketHandler_udp_client_socket
 udp client socket
 
NetworkUDPSocketHandler_udp_server_socket
 udp server socket
 
NetworkUDPSocketHandler_udp_master_socket
 udp master socket
 
byte _network_clients_connected = 0
 The amount of clients connected.
 
static const uint GITHASH_SUFFIX_LEN = 12
 How many hex digits of the git hash to include in network revision string. More...
 

Detailed Description

Base functions for networking support.

Definition in file network.cpp.

Function Documentation

◆ CheckPauseHelper()

static void CheckPauseHelper ( bool  pause,
PauseMode  pm 
)
static

Helper function for the pause checkers.

If pause is true and the current pause mode isn't set the game will be paused, if it it false and the pause mode is set the game will be unpaused. In the other cases nothing happens to the pause state.

Parameters
pausewhether we'd like to pause
pmthe mode which we would like to pause with

Definition at line 393 of file network.cpp.

References _pause_mode, CMD_PAUSE, DoCommandP(), and PM_UNPAUSED.

Referenced by CheckMinActiveClients(), and CheckPauseOnJoin().

◆ GenerateCompanyPasswordHash()

const char* GenerateCompanyPasswordHash ( const char *  password,
const char *  password_server_id,
uint32  password_game_seed 
)

Hash the given password using server ID and game seed.

Parameters
passwordPassword to hash.
password_server_idServer ID.
password_game_seedGame seed.
Returns
The hashed password.

Definition at line 182 of file network.cpp.

References lastof, NETWORK_SERVER_ID_LENGTH, seprintf(), and StrEmpty().

Referenced by NetworkServerSetCompanyPassword(), ClientNetworkGameSocketHandler::SendCompanyPassword(), ClientNetworkGameSocketHandler::SendMove(), and ClientNetworkGameSocketHandler::SendSetPassword().

◆ GetBindAddresses()

void GetBindAddresses ( NetworkAddressList addresses,
uint16  port 
)

Get the addresses to bind to.

Parameters
addressesthe list to write to.
portthe port to bind to.

Definition at line 623 of file network.cpp.

References _network_bind_list.

Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::Listen(), and NetworkUDPInitialize().

◆ GetNetworkErrorMsg()

StringID GetNetworkErrorMsg ( NetworkErrorCode  err)

Retrieve the string id of an internal error number.

Parameters
errNetworkErrorCode
Returns
the StringID

Definition at line 299 of file network.cpp.

References lengthof.

Referenced by ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(), ServerNetworkAdminSocketHandler::SendError(), and ServerNetworkGameSocketHandler::SendError().

◆ GetNetworkRevisionString()

const char* GetNetworkRevisionString ( )

Get the network version string used by this build.

The returned string is guaranteed to be at most NETWORK_REVISON_LENGTH bytes.

Definition at line 1098 of file network.cpp.

References stredup().

Referenced by IsNetworkCompatibleVersion(), NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(), and ServerNetworkAdminSocketHandler::SendWelcome().

◆ HasClients()

bool HasClients ( )

Return whether there is any client connected or trying to connect at all.

Returns
whether we have any client activity

Definition at line 100 of file network.cpp.

◆ InitializeNetworkPools()

static void InitializeNetworkPools ( bool  close_admins = true)
static

Resets the pools used for network clients, and the admin pool if needed.

Parameters
close_adminsWhether the admin pool has to be cleared as well.

Definition at line 511 of file network.cpp.

References PoolBase::Clean(), PT_NADMIN, PT_NCLIENT, and PT_NONE.

Referenced by NetworkClose().

◆ IsNetworkCompatibleVersion()

bool IsNetworkCompatibleVersion ( const char *  other)

Checks whether the given version string is compatible with our version.

First tries to match the full string, if that fails, attempts to compare just git hashes.

Parameters
otherthe version string to compare to

Definition at line 1147 of file network.cpp.

References GetNetworkRevisionString(), and NETWORK_REVISION_LENGTH.

Referenced by ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE().

◆ NetworkBackgroundLoop()

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 840 of file network.cpp.

References _network_content_client, TCPConnecter::CheckCallbacks(), NetworkHTTPSocketHandler::HTTPReceive(), NetworkBackgroundUDPLoop(), and ClientNetworkContentSocketHandler::SendReceive().

◆ NetworkChangeCompanyPassword()

const char* NetworkChangeCompanyPassword ( CompanyID  company_id,
const char *  password 
)

Change the company password of a given company.

Parameters
company_idID of the company the password should be changed for.
passwordThe unhashed password we like to set ('*' or '' resets the password)
Returns
The password.

Definition at line 162 of file network.cpp.

References _network_server, NetworkClientSetCompanyPassword(), and NetworkServerSetCompanyPassword().

◆ NetworkClose()

◆ NetworkCompanyIsPassworded()

bool NetworkCompanyIsPassworded ( CompanyID  company_id)

Check if the company we want to join requires a password.

Parameters
company_idid of the company we want to check the 'passworded' flag for.
Returns
true if the company requires a password.

Definition at line 213 of file network.cpp.

References _network_company_passworded, and HasBit().

Referenced by NetworkServerUpdateCompanyPassworded().

◆ NetworkCountActiveClients()

static uint NetworkCountActiveClients ( )
static

Counts the number of active clients connected.

It has to be in STATUS_ACTIVE and not a spectator

Returns
number of active clients

Definition at line 405 of file network.cpp.

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

Referenced by CheckMinActiveClients().

◆ NetworkDisconnect()

void NetworkDisconnect ( bool  blocking,
bool  close_admins 
)

◆ NetworkHandlePauseChange()

void NetworkHandlePauseChange ( PauseMode  prev_mode,
PauseMode  changed_mode 
)

Handle the pause mode change so we send the right messages to the chat.

Parameters
prev_modeThe previous pause mode.
changed_modeThe pause mode that got changed.

Definition at line 337 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().

◆ NetworkHasJoiningClient()

static bool NetworkHasJoiningClient ( )
static

Checks whether there is a joining client.

Returns
true iff one client is joining (but not authorizing)

Definition at line 435 of file network.cpp.

Referenced by CheckPauseOnJoin().

◆ NetworkReceive()

static bool NetworkReceive ( )
static

◆ ParseConnectionString()

void ParseConnectionString ( const char **  company,
const char **  port,
char *  connection_string 
)

Converts a string to ip/port/company Format: IP:port::company.

connection_string will be re-terminated to separate out the hostname, and company and port will be set to the company and port strings given by the user, inside the memory area originally occupied by connection_string.

Definition at line 464 of file network.cpp.

Referenced by NetworkHTTPSocketHandler::Connect().

◆ StateGameLoop()

Variable Documentation

◆ _network_game_info

NetworkServerGameInfo _network_game_info

Information about our game.

Uncomment the following define to enable command replaying.

Definition at line 57 of file network.cpp.

Referenced by ServerNetworkGameSocketHandler::AllowConnection(), ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(), ServerNetworkGameSocketHandler::SendWelcome(), and ServerNetworkAdminSocketHandler::SendWelcome().

◆ _networkclientinfo_pool

NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo")

Make sure both pools have the same size.

The pool with client information.

◆ GITHASH_SUFFIX_LEN

const uint GITHASH_SUFFIX_LEN = 12
static

How many hex digits of the git hash to include in network revision string.

Determined as 10 hex digits + 2 characters for -g/-u/-m prefix.

Definition at line 1092 of file network.cpp.