OpenTTD Source: src/network/network_client.cpp File Reference

OpenTTD Source 15.0-beta1

Data Structures | Functions | Variables
network_client.cpp File Reference

Client part of the network protocol. More...

`#include "../stdafx.h"`
`#include "network_gui.h"`
`#include "../saveload/saveload.h"`
`#include "../saveload/saveload_filter.h"`
`#include "../command_func.h"`
`#include "../console_func.h"`
`#include "../strings_func.h"`
`#include "../window_func.h"`
`#include "../company_func.h"`
`#include "../company_base.h"`
`#include "../company_gui.h"`
`#include "../company_cmd.h"`
`#include "../core/random_func.hpp"`
`#include "../timer/timer_game_tick.h"`
`#include "../timer/timer_game_calendar.h"`
`#include "../gfx_func.h"`
`#include "../error.h"`
`#include "../rev.h"`
`#include "network.h"`
`#include "network_base.h"`
`#include "network_client.h"`
`#include "network_gamelist.h"`
`#include "../core/backup_type.hpp"`
`#include "../thread.h"`
`#include "../social_integration.h"`
`#include "table/strings.h"`
`#include "../safeguards.h"`

Go to the source code of this file.

## Data Structures
struct | PacketReader
| Read some packets, and when do use that data as initial load filter. More...
class | ClientGamePasswordRequestHandler

## Functions
void | ClientNetworkEmergencySave ()
| Create an emergency savegame when the network connection is lost.
bool | SafeLoad (const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, std::shared_ptr< struct LoadFilter > lf)
void | NetworkClient_Connected ()
| Is called after a client is connected to the server.
void | NetworkClientSendRcon (const std::string &password, const std::string &command)
| Send a remote console command.
void | NetworkClientRequestMove (CompanyID company_id)
| Notify the server of this client wanting to be moved to another company.
void | NetworkClientsToSpectators (CompanyID cid)
| Move the clients of a company to the spectators.
bool | NetworkIsValidClientName (const std::string_view client_name)
| Check whether the given client name is deemed valid for use in network games.
bool | NetworkValidateClientName (std::string &client_name)
| Trim the given client name in place, i.e.
bool | NetworkValidateOurClientName ()
| Convenience method for NetworkValidateClientName on _settings_client.network.client_name.
void | NetworkUpdateClientName (const std::string &client_name)
| Send the server our name as callback from the setting.
void | NetworkClientSendChat (NetworkAction action, DestType type, int dest, const std::string &msg, int64_t data)
| Send a chat message.
bool | NetworkClientPreferTeamChat (const NetworkClientInfo *cio)
| Tell whether the client has team members who they can chat to.
uint | NetworkMaxCompaniesAllowed ()
| Get the maximum number of companies that are allowed by the server.
bool | NetworkMaxCompaniesReached ()
| Check if max_companies has been reached on the server (local check only).

## Variables
static uint32_t | last_ack_frame
| Last frame we performed an ack.
static uint8_t | _network_server_max_companies
| Maximum number of companies of the currently joined server.
std::string | _network_server_name
| The current name of the server you are on.
NetworkJoinInfo | _network_join
| Information about the game to join to.

## Detailed Description

Client part of the network protocol.

Definition in file network_client.cpp.

## Function Documentation

## ◆ ClientNetworkEmergencySave()

void ClientNetworkEmergencySave | ( |  | ) |

Create an emergency savegame when the network connection is lost.

Definition at line 135 of file network_client.cpp.

References DoAutoOrNetsave().

Referenced by ClientNetworkGameSocketHandler::ClientError(), NetworkGameSocketHandler::CloseConnection(), ClientNetworkGameSocketHandler::Receive_SERVER_ERROR(), ClientNetworkGameSocketHandler::Receive_SERVER_NEWGAME(), and ClientNetworkGameSocketHandler::Receive_SERVER_SHUTDOWN().

## ◆ NetworkClient_Connected()

void NetworkClient_Connected | ( |  | ) |

Is called after a client is connected to the server.

Definition at line 1229 of file network_client.cpp.

References _frame_counter, _frame_counter_server, Debug, last_ack_frame, and ClientNetworkGameSocketHandler::SendJoin().

Referenced by TCPClientConnecter::OnConnect().

## ◆ NetworkClientPreferTeamChat()

bool NetworkClientPreferTeamChat | ( | const NetworkClientInfo * | cio | ) |

Tell whether the client has team members who they can chat to.

Parameters

  cio | client to check members of.

Returns
  true if there is at least one team member.

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.

Referenced by MainWindow::OnHotkey().

## ◆ NetworkClientRequestMove()

void NetworkClientRequestMove | ( | CompanyID | company_id | ) |

Notify the server of this client wanting to be moved to another company.

Parameters

  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 MenuClickCompany(), CompanyWindow::OnClick(), and NetworkClientListWindow::OnClickCompanyJoin().

## ◆ NetworkClientSendChat()

void NetworkClientSendChat | ( | NetworkAction | action,
|  | DestType | type,
|  | int | dest,
|  | const std::string & | msg,
|  | int64_t | data
| ) |  |

Send a chat message.

Parameters

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

## ◆ NetworkClientSendRcon()

void NetworkClientSendRcon | ( | const std::string & | password,
|  | const std::string & | command
| ) |  |

Send a remote console command.

Parameters

  password | The password.
  command | The command to execute.

Definition at line 1247 of file network_client.cpp.

References ClientNetworkGameSocketHandler::SendRCon().

## ◆ NetworkClientsToSpectators()

void NetworkClientsToSpectators | ( | CompanyID | cid | ) |

Move the clients of a company to the spectators.

Parameters

  cid | The company to move the clients of.

Definition at line 1265 of file network_client.cpp.

References _current_company, _local_company, CC_DEFAULT, COMPANY_SPECTATOR, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_networkclientinfo_pool >::Iterate(), Backup< T >::Restore(), and SetLocalCompany().

Referenced by ChangeOwnershipOfCompanyItems().

## ◆ NetworkIsValidClientName()

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.

Parameters

  client_name | The client name to check for validity.

Returns
  True iff the name is valid.

Definition at line 1287 of file network_client.cpp.

Referenced by NetworkValidateClientName(), ServerNetworkGameSocketHandler::Receive_CLIENT_IDENTIFY(), ServerNetworkGameSocketHandler::Receive_CLIENT_SET_NAME(), and ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO().

## ◆ NetworkMaxCompaniesAllowed()

uint NetworkMaxCompaniesAllowed | ( |  | ) |

Get the maximum number of companies that are allowed by the server.

Returns
  The number of companies allowed.

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(), and NetworkClientListWindow::SetStringParameters().

## ◆ NetworkMaxCompaniesReached()

bool NetworkMaxCompaniesReached | ( |  | ) |

Check if max_companies has been reached on the server (local check only).

Returns
  true if the max value has been reached or exceeded, false otherwise.

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::DrawWidget(), and NetworkClientListWindow::RebuildList().

## ◆ NetworkUpdateClientName()

void NetworkUpdateClientName | ( | const std::string & | client_name | ) |

Send the server our name as callback from the setting.

Parameters

  newname | The new client name.

Definition at line 1334 of file network_client.cpp.

References _network_own_client_id, _network_server, CC_DEFAULT, CLIENT_ID_SERVER, NetworkClientInfo::client_name, NetworkClientInfo::GetByClientID(), NetworkMakeClientNameUnique(), NetworkUpdateClientInfo(), and ClientNetworkGameSocketHandler::SendSetName().

## ◆ NetworkValidateClientName()

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.

Parameters

  client_name | The client name to validate. It will be trimmed of leading and trailing spaces.

Returns
  True iff the client name is valid.

Definition at line 1309 of file network_client.cpp.

References INVALID_STRING_ID, NetworkIsValidClientName(), ShowErrorMessage(), StrTrimInPlace(), and WL_ERROR.

Referenced by NetworkValidateOurClientName().

## ◆ 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.

Returns
  True iff the client name is valid.

Definition at line 1325 of file network_client.cpp.

References _settings_client, NetworkSettings::client_name, ClientSettings::network, and NetworkValidateClientName().

Referenced by NetworkClientConnectGame().

## Variable Documentation

## ◆ _network_join

NetworkJoinInfo _network_join

Information about the game to join to.

Definition at line 322 of file network_client.cpp.

Referenced by ClientGamePasswordRequestHandler::AskUserForPassword(), NetworkClientConnectGame(), NetworkClientJoinGame(), and ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE().

## ◆ _network_server_max_companies

uint8_t _network_server_max_companies
| static

Maximum number of companies of the currently joined server.

Definition at line 317 of file network_client.cpp.

Referenced by NetworkMaxCompaniesAllowed(), and ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE().

## ◆ _network_server_name

std::string _network_server_name

The current name of the server you are on.

Definition at line 319 of file network_client.cpp.

Referenced by ClientNetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE(), NetworkClientListWindow::SetStringParameters(), and NetworkClientListWindow::UpdateWidgetSize().

## ◆ last_ack_frame

uint32_t last_ack_frame
| static

Last frame we performed an ack.

Definition at line 314 of file network_client.cpp.

Referenced by NetworkClient_Connected(), and ClientNetworkGameSocketHandler::Receive_SERVER_FRAME().