Data Structures | Functions | Variables

# network.cpp File Reference

Base functions for networking support. More...

`#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"`

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
| assert_compile (NetworkClientInfoPool::MAX_SIZE==NetworkClientSocketPool::MAX_SIZE)
| Make sure both pools have the same size.
| assert_compile ((int) NETWORK_NUM_LANDSCAPES==(int) NUM_LANDSCAPE)
void | StateGameLoop ()
| State controlling game loop.
byte | NetworkSpectatorCount ()
const char * | NetworkChangeCompanyPassword (CompanyID company_id, const char *password)
| Change the company password of a given company.
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.
bool | NetworkCompanyIsPassworded (CompanyID company_id)
| Check if the company we want to join requires a password.
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.
void | NetworkHandlePauseChange (PauseMode prev_mode, PauseMode changed_mode)
| Handle the pause mode change so we send the right messages to the chat.
static void | CheckPauseHelper (bool pause, PauseMode pm)
| Helper function for the pause checkers.
static uint | NetworkCountActiveClients ()
| Counts the number of active clients connected.
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.
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.
static void | InitializeNetworkPools (bool close_admins=true)
| Resets the pools used for network clients, and the admin pool if needed.
void | NetworkClose (bool close_admins)
| Close current connections.
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.
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.
static bool | NetworkReceive ()
| Receives something from the network.
static void | NetworkSend ()
void | NetworkBackgroundLoop ()
| We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer.
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.
bool | IsNetworkCompatibleVersion (const char *other)
| Checks whether the given version string is compatible with our version.
## Variables
NetworkClientInfoPool | _networkclientinfo_pool ("NetworkClientInfo")
| The pool with client information.
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.
NetworkCompanyState * | _network_company_states = NULL
| 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.
uint32 | _network_last_advertise_frame
| Last time we did 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.

## Detailed Description

Base functions for networking support.

Definition in file network.cpp.

## Function Documentation

assert_compile | ( | NetworkClientInfoPool::MAX_SIZE | = `=NetworkClientSocketPool::MAX_SIZE` | ) |

Make sure both pools have the same size.

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:

  | pause | whether we'd like to pause
  | pm | the mode which we would like to pause with

Definition at line 389 of file network.cpp.

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

Referenced by CheckMinActiveClients(), and CheckPauseOnJoin().

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:

  | password | Password to hash.
  | password_server_id | Server ID.
  | password_game_seed | Game seed.

Returns:
  The hashed password.

Definition at line 181 of file network.cpp.

References lengthof, NETWORK_SERVER_ID_LENGTH, and StrEmpty().

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

void GetBindAddresses | ( | NetworkAddressList * | addresses,
|  | uint16 | port |
| ) |  |  |

Get the addresses to bind to.

Parameters:

  | addresses | the list to write to.
  | port | the port to bind to.

Definition at line 623 of file network.cpp.

References SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), and SmallVector< T, S >::Length().

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

StringID GetNetworkErrorMsg | ( | NetworkErrorCode | err | ) |

Retrieve the string id of an internal error number.

Parameters:

  | err | NetworkErrorCode

Returns:
  the StringID

Definition at line 298 of file network.cpp.

References lengthof.

Referenced by ClientNetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(), ServerNetworkGameSocketHandler::SendError(), and ServerNetworkAdminSocketHandler::SendError().

static void InitializeNetworkPools | ( | bool | close_admins = `true` | ) | `[static]`

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

Parameters:

  | close_admins | Whether the admin pool has to be cleared as well.

Definition at line 509 of file network.cpp.

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

Referenced by NetworkClose().

bool IsNetworkCompatibleVersion | ( | const char * | other | ) |

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

Parameters:

  | other | the version string to compare to

Definition at line 1082 of file network.cpp.

References NETWORK_REVISION_LENGTH.

Referenced by ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE().

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().

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

Change the company password of a given company.

Parameters:

  | company_id | ID of the company the password should be changed for.
  | password | The unhashed password we like to set ('*' or '' resets the password)

Returns:
  The password.

Definition at line 161 of file network.cpp.

References NetworkClientSetCompanyPassword(), and NetworkServerSetCompanyPassword().

Referenced by CmdCompanyCtrl().

void NetworkClose | ( | bool | close_admins | ) |

Close current connections.

Parameters:

  | close_admins | Whether the admin connections have to be closed as well.

Definition at line 518 of file network.cpp.

References _network_company_states, ClientNetworkGameSocketHandler::CloseConnection(), NetworkAdminSocketHandler::CloseConnection(), TCPListenHandler< ServerNetworkAdminSocketHandler, ADMIN_PACKET_SERVER_FULL, ADMIN_PACKET_SERVER_BANNED >::CloseListeners(), TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::CloseListeners(), FOR_ALL_ADMIN_SOCKETS, FOR_ALL_CLIENT_SOCKETS, free(), InitializeNetworkPools(), TCPConnecter::KillAll(), ClientNetworkGameSocketHandler::my_client, NETWORK_RECV_STATUS_CONN_LOST, NetworkFreeLocalCommandQueue(), and ClientNetworkGameSocketHandler::SendQuit().

Referenced by NetworkDisconnect().

bool NetworkCompanyIsPassworded | ( | CompanyID | company_id | ) |

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

Parameters:

  | company_id | id 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 HasBit().

Referenced by CompanyWindow::DrawWidget(), NetworkServerUpdateCompanyPassworded(), CompanyWindow::OnClick(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), and ServerNetworkAdminSocketHandler::SendCompanyUpdate().

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

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

Referenced by CheckMinActiveClients().

void NetworkDisconnect | ( | bool | blocking,
|  | bool | close_admins |
| ) |  |  |

We want to disconnect from the host/clients.

Parameters:

  | blocking | whether to wait till everything has been closed.
  | close_admins | Whether the admin sockets need to be closed as well.

Definition at line 782 of file network.cpp.

References _settings_client, DeleteWindowById(), FOR_ALL_ACTIVE_ADMIN_SOCKETS, FOR_ALL_CLIENT_SOCKETS, ClientSettings::network, NetworkClose(), NetworkUDPInitialize(), NetworkUDPRemoveAdvertise(), NetworkTCPSocketHandler::SendPackets(), ServerNetworkAdminSocketHandler::SendShutdown(), NetworkSettings::server_advertise, WC_NETWORK_STATUS_WINDOW, and WN_NETWORK_STATUS_WINDOW_JOIN.

Referenced by NetworkShutDown(), NetworkJoinStatusWindow::OnClick(), TCPQueryConnecter::OnFailure(), NetworkJoinStatusWindow::OnQueryTextFinished(), and SafeLoad().

void NetworkHandlePauseChange | ( | PauseMode | prev_mode,
|  | PauseMode | changed_mode |
| ) |  |  |

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

Parameters:

  | prev_mode | The previous pause mode.
  | changed_mode | The pause mode that got changed.

Definition at line 336 of file network.cpp.

References _pause_mode, CC_DEFAULT, DRAW_STRING_BUFFER, lastof, PM_PAUSED_ACTIVE_CLIENTS, PM_PAUSED_GAME_SCRIPT, PM_PAUSED_JOIN, PM_PAUSED_NORMAL, PM_UNPAUSED, and SetDParam().

Referenced by CmdPause().

static bool NetworkHasJoiningClient | ( |  | ) | `[static]`

Checks whether there is a joining client.

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

Definition at line 432 of file network.cpp.

References FOR_ALL_CLIENT_SOCKETS.

Referenced by CheckPauseOnJoin().

static bool NetworkReceive | ( |  | ) | `[static]`

Receives something from the network.

Returns:
  true if everything went fine, false when the connection got closed.

Definition at line 814 of file network.cpp.

References ClientNetworkGameSocketHandler::Receive(), TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::Receive(), and TCPListenHandler< ServerNetworkAdminSocketHandler, ADMIN_PACKET_SERVER_FULL, ADMIN_PACKET_SERVER_BANNED >::Receive().

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

Referenced by NetworkHTTPSocketHandler::Connect(), and AfterNewGRFScan::OnNewGRFsScanned().

void StateGameLoop | ( |  | ) |

State controlling game loop.

The state must not be changed from anywhere but here. That check is enforced in DoCommand.

Definition at line 1300 of file openttd.cpp.

References _current_company, _date, _date_fract, _pause_mode, _settings_game, AnimateAnimatedTiles(), AUTOSAVE_DIR, CallWindowTickEvent(), CheckCaches(), ClearStorageChanges(), GameSettings::game_creation, AI::GameLoop(), Game::GameLoop(), GameCreationSettings::generation_seed, HasModalProgress(), IncreaseDate(), IsLocalCompany(), lengthof, OWNER_NONE, PM_UNPAUSED, Backup< T >::Restore(), RunTileLoop(), SaveOrLoad(), SL_SAVE, and UpdateLandscapingLimits().

Referenced by ClientNetworkGameSocketHandler::GameLoop().

## Variable Documentation

NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo")

The pool with client information.