# 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_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 "table/strings.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

| DECLARE_POSTFIX_INCREMENT (ClientID)
| assert_compile (NetworkClientInfoPool::MAX_SIZE==NetworkClientSocketPool::MAX_SIZE)
| assert_compile ((int) NETWORK_NUM_LANDSCAPES==(int) NUM_LANDSCAPE)
void | StateGameLoop ()
| State controlling game loop.
NetworkClientInfo * | NetworkFindClientInfoFromIndex (ClientIndex index)
| Return the CI given it's raw index.
NetworkClientInfo * | NetworkFindClientInfoFromClientID (ClientID client_id)
| Return the CI given it's client-identifier.
NetworkClientInfo * | NetworkFindClientInfoFromIP (const char *ip)
| Return the CI for a given IP.
NetworkClientSocket * | NetworkFindClientStateFromClientID (ClientID client_id)
| Return the client state given it's client-identifier.
void | NetworkGetClientName (char *client_name, size_t size, const NetworkClientSocket *cs)
byte | NetworkSpectatorCount ()
bool | NetworkCompanyIsPassworded (CompanyID company_id)
| Check if the company we want to join requires a password.
void | NetworkTextMessage (NetworkAction action, ConsoleColour colour, bool self_send, const char *name, const char *str, int64 data)
uint | NetworkCalculateLag (const NetworkClientSocket *cs)
static void | NetworkError (StringID error_string)
static void | ServerStartError (const char *error)
static void | NetworkClientError (NetworkRecvStatus res, NetworkClientSocket *cs)
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 NetworkClientSocket * | NetworkAllocClient (SOCKET s)
NetworkRecvStatus | NetworkCloseClient (NetworkClientSocket *cs, NetworkRecvStatus status)
static void | NetworkAcceptClients (SOCKET ls)
static bool | NetworkListen ()
static void | InitializeNetworkPools ()
| Resets both pools used for network clients.
static void | NetworkClose ()
static void | NetworkInitialize ()
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)
| We want to disconnect from the host/clients.
static bool | NetworkReceive ()
static void | NetworkSend ()
static bool | NetworkDoClientLoop ()
void | NetworkUDPGameLoop ()
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")
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
NetworkCompanyState * | _network_company_states = NULL
ClientID | _network_own_client_id
ClientID | _redirect_console_to_client
bool | _network_need_advertise
uint32 | _network_last_advertise_frame
uint8 | _network_reconnect
StringList | _network_bind_list
StringList | _network_host_list
StringList | _network_ban_list
uint32 | _frame_counter_server
uint32 | _frame_counter_max
uint32 | _frame_counter
uint32 | _last_sync_frame
NetworkAddressList | _broadcast_list
uint32 | _sync_seed_1
uint32 | _sync_frame
bool | _network_first_time
bool | _network_udp_server
uint16 | _network_udp_broadcast
uint8 | _network_advertise_retries
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
static SocketList | _listensockets
static byte | _network_clients_connected = 0
static ClientID | _network_client_id = CLIENT_ID_FIRST

## Detailed Description

Base functions for networking support.

Definition in file network.cpp.

## Function Documentation

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

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

Referenced by CheckMinActiveClients(), and CheckPauseOnJoin().

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

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

StringID GetNetworkErrorMsg | ( | NetworkErrorCode | err | ) |

Retrieve the string id of an internal error number.

Parameters:

  | err | NetworkErrorCode

Returns:
  the StringID

Definition at line 328 of file network.cpp.

References lengthof.

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

References NETWORK_REVISION_LENGTH.

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

References HasBit().

Referenced by CompanyWindow::DrawWidget(), and CompanyWindow::OnClick().

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

References NetworkClientInfo::client_playas, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), NetworkClientSocket::status, and STATUS_ACTIVE.

Referenced by CheckMinActiveClients().

void NetworkDisconnect | ( | bool | blocking | ) |

We want to disconnect from the host/clients.

Parameters:

  | blocking | whether to wait till everything has been closed

Definition at line 922 of file network.cpp.

References _settings_client, DeleteWindowById(), ClientSettings::network, NetworkUDPRemoveAdvertise(), NetworkTCPSocketHandler::Send_Packets(), and NetworkSettings::server_advertise.

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

NetworkClientInfo* NetworkFindClientInfoFromClientID | ( | ClientID | client_id | ) |

Return the CI given it's client-identifier.

Parameters:

  | client_id | the ClientID to search for

Returns:
  return a pointer to the corresponding NetworkClientInfo struct or NULL when not found

Definition at line 127 of file network.cpp.

References NetworkClientInfo::client_id.

Referenced by CmdCompanyCtrl(), NetworkChatWindow::DrawWidget(), NetworkServerChangeClientName(), NetworkServerDoMove(), MainWindow::OnKeyPress(), and NetworkChatWindow::UpdateWidgetSize().

NetworkClientInfo* NetworkFindClientInfoFromIndex | ( | ClientIndex | index | ) |

Return the CI given it's raw index.

Parameters:

  | index | the index to search for

Returns:
  return a pointer to the corresponding NetworkClientInfo struct

Definition at line 117 of file network.cpp.

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

NetworkClientInfo* NetworkFindClientInfoFromIP | ( | const char * | ip | ) |

Return the CI for a given IP.

Parameters:

  | ip | IP of the client we are looking for. This must be in string-format

Returns:
  return a pointer to the corresponding NetworkClientInfo struct or NULL when not found

Definition at line 143 of file network.cpp.

References NetworkClientInfo::client_address, and NetworkAddress::GetAddressLength().

NetworkClientSocket* NetworkFindClientStateFromClientID | ( | ClientID | client_id | ) |

Return the client state given it's client-identifier.

Parameters:

  | client_id | the ClientID to search for

Returns:
  return a pointer to the corresponding NetworkClientSocket struct or NULL when not found

Definition at line 162 of file network.cpp.

References NetworkClientSocket::client_id.

Referenced by NetworkServerDoMove().

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

References _pause_mode, lastof, PM_PAUSED_ACTIVE_CLIENTS, 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 451 of file network.cpp.

References NetworkClientSocket::status, STATUS_ACTIVE, and STATUS_AUTHORIZED.

Referenced by CheckPauseOnJoin().

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

Referenced by NetworkHTTPSocketHandler::Connect().

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 1191 of file openttd.cpp.

References _date, _pause_mode, _settings_game, AnimateAnimatedTiles(), AUTOSAVE_DIR, CallWindowTickEvent(), CheckCaches(), ClearStorageChanges(), GameSettings::game_creation, AI::GameLoop(), GameCreationSettings::generation_seed, IncreaseDate(), IsGeneratingWorld(), lengthof, OWNER_NONE, PM_UNPAUSED, and SaveOrLoad().