OpenTTD: src/network/network_server.cpp File Reference

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

Data Structures | Functions | Variables
network_server.cpp File Reference

Server part of the network protocol. More...

`#include "../stdafx.h"`
`#include "../strings_func.h"`
`#include "../date_func.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 "../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
| assert_compile (MAX_CLIENT_SLOTS > MAX_CLIENTS)
| Make very sure the preconditions given in network_type.h are actually followed.
| assert_compile (NetworkClientSocketPool::MAX_SIZE==MAX_CLIENT_SLOTS)
| Yes...
static void | NetworkHandleCommandQueue (NetworkClientSocket *cs)
| Handle the command-queue of a socket.
void | NetworkServerSendChat (NetworkAction action, DestType desttype, int dest, const char *msg, ClientID from_id, int64 data, bool from_admin)
| Send an actual chat message.
void | NetworkPopulateCompanyStats (NetworkCompanyStats *stats)
| Populate the company stats.
void | NetworkUpdateClientInfo (ClientID client_id)
| Send updated client info of a particular client.
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.
bool | NetworkFindName (char *new_name, const char *last)
| Check whether a name is unique, and otherwise try to make it unique.
bool | NetworkServerChangeClientName (ClientID client_id, const char *new_name)
| Change the client name of the given client.
void | NetworkServerSetCompanyPassword (CompanyID company_id, const char *password, bool already_hashed)
| Set/Reset a company password on the server end.
void | NetworkServer_Tick (bool send_frame)
| This is called every tick if this is a _network_server.
void | NetworkServerYearlyLoop ()
| Yearly "callback".
void | NetworkServerMonthlyLoop ()
| Monthly "callback".
void | NetworkServerDailyLoop ()
| Daily "callback".
void | NetworkServerShowStatusToConsole ()
| Show the status message of all clients on the console.
void | NetworkServerSendConfigUpdate ()
| Send Config Update.
void | NetworkServerUpdateCompanyPassworded (CompanyID company_id, bool passworded)
| Tell that a particular company is (not) passworded.
void | NetworkServerDoMove (ClientID client_id, CompanyID company_id)
| Handle the tid-bits of moving a client from one company to another.
void | NetworkServerSendRcon (ClientID client_id, TextColour colour_code, const char *string)
| Send an rcon reply to the client.
void | NetworkServerKickClient (ClientID client_id)
| Kick a single client.
uint | NetworkServerKickOrBanIP (ClientID client_id, bool ban)
| Ban, or kick, everyone joined from the given client's IP.
uint | NetworkServerKickOrBanIP (const char *ip, bool ban)
| Kick or ban someone based on an IP address.
bool | NetworkCompanyHasClients (CompanyID company)
| Check whether a particular company has clients.
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.

## Variables
static ClientID | _network_client_id = CLIENT_ID_FIRST
| The identifier counter for new clients (is never decreased)
NetworkClientSocketPool | _networkclientsocket_pool ("NetworkClientSocket")
| The pool with clients.

## Detailed Description

Server part of the network protocol.

Definition in file network_server.cpp.

## Function Documentation

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

Yes...

static void NetworkAutoCleanCompanies | ( |  | ) |
| static

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.

happens a year later)

Definition at line 1635 of file network_server.cpp.

References _network_company_states, _network_dedicated, _settings_client, NetworkSettings::autoclean_companies, NetworkSettings::autoclean_novehicles, NetworkSettings::autoclean_protected, NetworkSettings::autoclean_unprotected, CC_DEFAULT, CLIENT_ID_SERVER, NetworkClientInfo::client_playas, CMD_COMPANY_CTRL, CRR_AUTOCLEAN, DoCommandP(), FOR_ALL_CLIENT_INFOS, FOR_ALL_VEHICLES, NetworkClientInfo::GetByClientID(), IConsolePrintF(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, CompanyProperties::is_ai, Vehicle::IsPrimaryVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MAX_COMPANIES, NetworkCompanyState::months_empty, ClientSettings::network, NetworkServerUpdateCompanyPassworded(), Vehicle::owner, NetworkCompanyState::password, and StrEmpty().

Referenced by NetworkServerMonthlyLoop().

bool NetworkCompanyHasClients | ( | CompanyID | company | ) |

Check whether a particular company has clients.

Parameters

  company | The company to check.

Returns
  True if at least one client is joined to the company.

Definition at line 2129 of file network_server.cpp.

References NetworkClientInfo::client_playas, and FOR_ALL_CLIENT_INFOS.

bool NetworkFindName | ( | char * | new_name,
|  | const char * | last
| ) |  |

Check whether a name is unique, and otherwise try to make it unique.

Parameters

  new_name | The name to check/modify.
  last | The last writeable element of the buffer.

Returns
  True if an unique name was achieved.

Definition at line 1708 of file network_server.cpp.

References CLIENT_ID_SERVER, NetworkClientInfo::client_name, FOR_ALL_CLIENT_INFOS, NetworkClientInfo::GetByClientID(), lastof, MAX_CLIENTS, NETWORK_CLIENT_NAME_LENGTH, seprintf(), and strecpy().

Referenced by NetworkUpdateClientName(), ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(), and ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME().

static void NetworkHandleCommandQueue | ( | NetworkClientSocket * | cs | ) |
| static

Handle the command-queue of a socket.

Parameters

  cs | The socket to handle the queue for.

Definition at line 1792 of file network_server.cpp.

References free().

Referenced by NetworkServer_Tick(), and ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK().

void NetworkPopulateCompanyStats | ( | NetworkCompanyStats * | stats | ) |

Populate the company stats.

Parameters

  stats | the stats to update

Definition at line 1564 of file network_server.cpp.

References FACIL_AIRPORT, FACIL_BUS_STOP, FACIL_DOCK, FACIL_TRAIN, FACIL_TRUCK_STOP, BaseStation::facilities, FOR_ALL_VEHICLES, SpecializedVehicle< RoadVehicle, Type >::From(), RoadVehicle::IsBus(), Vehicle::IsPrimaryVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MAX_COMPANIES, NetworkCompanyStats::num_station, NetworkCompanyStats::num_vehicle, BaseStation::owner, Vehicle::owner, BaseVehicle::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

Referenced by ServerNetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO(), ServerNetworkGameSocketHandler::SendCompanyInfo(), and ServerNetworkAdminSocketHandler::SendCompanyStats().

void NetworkServer_Tick | ( | bool | send_frame | ) |

This is called every tick if this is a _network_server.

Parameters

  send_frame | Whether to send the frame to the clients.

Definition at line 1805 of file network_server.cpp.

References _frame_counter, _last_sync_frame, _realtime_tick, _settings_client, NetworkSettings::bytes_per_frame, NetworkSettings::bytes_per_frame_burst, CC_ERROR, CC_WARNING, DAY_TICKS, FOR_ALL_CLIENT_SOCKETS, IConsolePrintF(), NetworkSettings::max_download_time, NetworkSettings::max_init_time, NetworkSettings::max_join_time, NetworkSettings::max_lag_time, NetworkSettings::max_password_time, MILLISECONDS_PER_TICK, min(), ClientSettings::network, NetworkHandleCommandQueue(), NetworkUDPAdvertise(), and NetworkSettings::sync_freq.

bool NetworkServerChangeClientName | ( | ClientID | client_id,
|  | const char * | new_name
| ) |  |

Change the client name of the given client.

Parameters

  client_id | the client to change the name of
  new_name | the new name for the client

Returns
  true iff the name was changed

Definition at line 1751 of file network_server.cpp.

References CC_DEFAULT, NetworkClientInfo::client_name, FOR_ALL_CLIENT_INFOS, NetworkClientInfo::GetByClientID(), lastof, NetworkUpdateClientInfo(), and strecpy().

void NetworkServerDailyLoop | ( |  | ) |

Daily "callback".

Called whenever the date changes.

Definition at line 1946 of file network_server.cpp.

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

Referenced by OnNewDay().

void NetworkServerDoMove | ( | ClientID | client_id,
|  | CompanyID | company_id
| ) |  |

Handle the tid-bits of moving a client from one company to another.

Parameters

  client_id | id of the client we want to move.
  company_id | id of the company we want to move the client to.

Returns
  void

Definition at line 2031 of file network_server.cpp.

References _network_dedicated, CLIENT_ID_SERVER, NetworkClientInfo::client_playas, COMPANY_SPECTATOR, DESTTYPE_BROADCAST, NetworkClientInfo::GetByClientID(), NetworkServerSendChat(), NetworkUpdateClientInfo(), and SetLocalCompany().

Referenced by MenuClickCompany(), CompanyWindow::OnClick(), and ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE().

void NetworkServerKickClient | ( | ClientID | client_id | ) |

Kick a single client.

Parameters

  client_id | The client to kick.

Definition at line 2074 of file network_server.cpp.

References CLIENT_ID_SERVER.

Referenced by NetworkServerKickOrBanIP().

uint NetworkServerKickOrBanIP | ( | ClientID | client_id,
|  | bool | ban
| ) |  |

Ban, or kick, everyone joined from the given client's IP.

Parameters

  client_id | The client to check for.
  ban | Whether to ban or kick.

Definition at line 2085 of file network_server.cpp.

References NetworkServerKickOrBanIP().

Referenced by NetworkServerKickOrBanIP().

uint NetworkServerKickOrBanIP | ( | const char * | ip,
|  | bool | ban
| ) |  |

Kick or ban someone based on an IP address.

Parameters

  ip | The IP address/range to ban/kick.
  ban | Whether to ban or just kick.

Definition at line 2095 of file network_server.cpp.

References _network_ban_list, SmallVector< T, S >::Append(), SmallVector< T, S >::Begin(), CLIENT_ID_SERVER, SmallVector< T, S >::End(), FOR_ALL_CLIENT_SOCKETS, NetworkServerKickClient(), and stredup().

void NetworkServerMonthlyLoop | ( |  | ) |

Monthly "callback".

Called whenever the month changes.

Definition at line 1938 of file network_server.cpp.

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

Referenced by OnNewMonth().

void NetworkServerNewCompany | ( | const Company * | c,
|  | NetworkClientInfo * | ci
| ) |  |

Perform all the server specific administration of a new company.

Parameters

  c | The newly created company; can't be NULL.
  ci | The client information of the client that made the company; can be NULL.

Definition at line 2182 of file network_server.cpp.

References _network_company_states, _network_server, NetworkClientInfo::client_id, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, CMD_RENAME_PRESIDENT, DESTTYPE_BROADCAST, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, NetworkCompanyState::months_empty, NetworkAdminCompanyInfo(), NetworkSendCommand(), NetworkServerSendChat(), NetworkServerUpdateCompanyPassworded(), NetworkUpdateClientInfo(), and NetworkCompanyState::password.

Referenced by CmdCompanyCtrl().

void NetworkServerSendChat | ( | NetworkAction | action,
|  | DestType | desttype,
|  | int | dest,
|  | const char * | msg,
|  | ClientID | from_id,
|  | int64 | data,
|  | bool | from_admin
| ) |  |

Send an actual chat message.

Parameters

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

References _local_company, _settings_client, NetworkClientInfo::client_id, CLIENT_ID_SERVER, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, DEBUG, DESTTYPE_BROADCAST, DESTTYPE_CLIENT, DESTTYPE_TEAM, FOR_ALL_CLIENT_SOCKETS, NetworkClientInfo::GetByClientID(), GetDrawStringCompanyColour(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), lastof, ClientSettings::network, NETWORK_NAME_LENGTH, NetworkAdminChat(), NetworkSettings::server_admin_chat, and SetDParam().

Referenced by NetworkServerDoMove(), NetworkServerNewCompany(), ServerNetworkAdminSocketHandler::Receive_ADMIN_CHAT(), ServerNetworkGameSocketHandler::Receive_CLIENT_CHAT(), ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(), and SendChat().

void NetworkServerSendRcon | ( | ClientID | client_id,
|  | TextColour | colour_code,
|  | const char * | string
| ) |  |

Send an rcon reply to the client.

Parameters

  client_id | The identifier of the client.
  colour_code | The colour of the text.
  string | The actual reply.

Definition at line 2065 of file network_server.cpp.

Referenced by IConsolePrint().

void NetworkServerSetCompanyPassword | ( | CompanyID | company_id,
|  | const char * | password,
|  | bool | already_hashed
| ) |  |

Set/Reset a company password on the server end.

Parameters

  company_id | ID of the company the password should be changed for.
  password | The new password.
  already_hashed | Is the given password already hashed?

Definition at line 1776 of file network_server.cpp.

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

Referenced by NetworkChangeCompanyPassword(), and ServerNetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD().

void NetworkServerShowStatusToConsole | ( |  | ) |

Show the status message of all clients on the console.

Definition at line 1962 of file network_server.cpp.

References CC_INFO, NetworkClientInfo::client_name, NetworkClientInfo::client_playas, FOR_ALL_CLIENT_SOCKETS, IConsolePrintF(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), and lengthof.

void NetworkServerUpdateCompanyPassworded | ( | CompanyID | company_id,
|  | bool | passworded
| ) |  |

Tell that a particular company is (not) passworded.

Parameters

  company_id | The company that got/removed the password.
  passworded | Whether the password was received or removed.

Definition at line 2010 of file network_server.cpp.

References _network_company_passworded, FOR_ALL_CLIENT_SOCKETS, 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().

void NetworkServerYearlyLoop | ( |  | ) |

Yearly "callback".

Called whenever the year changes.

Definition at line 1931 of file network_server.cpp.

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

Referenced by OnNewYear().

void NetworkUpdateClientInfo | ( | ClientID | client_id | ) |

Send updated client info of a particular client.

Parameters

  client_id | The client to send it for.

Definition at line 1603 of file network_server.cpp.

References _date, _date_fract, NetworkClientInfo::client_playas, DEBUG, FOR_ALL_CLIENT_SOCKETS, NetworkClientInfo::GetByClientID(), and NetworkAdminClientUpdate().

Referenced by CmdCompanyCtrl(), NetworkServerChangeClientName(), NetworkServerDoMove(), NetworkServerNewCompany(), NetworkUpdateClientName(), and ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME().

## Variable Documentation

NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket")

The pool with clients.