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

OpenTTD Source 15.3

Data Structures | Typedefs | Functions | Variables
network_command.cpp File Reference

Command handling over network connections. More...

`#include "../stdafx.h"`
`#include "network_admin.h"`
`#include "network_client.h"`
`#include "network_server.h"`
`#include "../command_func.h"`
`#include "../company_func.h"`
`#include "../settings_type.h"`
`#include "../airport_cmd.h"`
`#include "../aircraft_cmd.h"`
`#include "../autoreplace_cmd.h"`
`#include "../company_cmd.h"`
`#include "../depot_cmd.h"`
`#include "../dock_cmd.h"`
`#include "../economy_cmd.h"`
`#include "../engine_cmd.h"`
`#include "../error_func.h"`
`#include "../goal_cmd.h"`
`#include "../group_cmd.h"`
`#include "../industry_cmd.h"`
`#include "../landscape_cmd.h"`
`#include "../league_cmd.h"`
`#include "../misc_cmd.h"`
`#include "../news_cmd.h"`
`#include "../object_cmd.h"`
`#include "../order_cmd.h"`
`#include "../rail_cmd.h"`
`#include "../road_cmd.h"`
`#include "../roadveh_cmd.h"`
`#include "../settings_cmd.h"`
`#include "../signs_cmd.h"`
`#include "../station_cmd.h"`
`#include "../story_cmd.h"`
`#include "../subsidy_cmd.h"`
`#include "../terraform_cmd.h"`
`#include "../timetable_cmd.h"`
`#include "../town_cmd.h"`
`#include "../train_cmd.h"`
`#include "../tree_cmd.h"`
`#include "../tunnelbridge_cmd.h"`
`#include "../vehicle_cmd.h"`
`#include "../viewport_cmd.h"`
`#include "../water_cmd.h"`
`#include "../waypoint_cmd.h"`
`#include "../script/script_cmd.h"`
`#include "../safeguards.h"`

Go to the source code of this file.

## Data Structures
struct | CallbackArgsHelper< void(*const)(Commands, const CommandCost &, Targs...)>
struct | CommandDispatch

## Typedefs
using | UnpackNetworkCommandProc = void(*)(const CommandPacket &)
using | UnpackDispatchT = std::array< UnpackNetworkCommandProc, _callback_tuple_size >

## Functions
template<size_t... i>
auto | MakeCallbackTable (std::index_sequence< i... >) noexcept
bool | IsNetworkRegisteredCallback (CommandCallback *callback)
| Helper function to ensure that callbacks used when Posting commands are actually registered for the network calls.
template<Commands Tcmd>
static CommandDataBuffer | SanitizeCmdStrings (const CommandDataBuffer &data)
| Validate and sanitize strings in command data.
template<Commands Tcmd, size_t cb>
static void | UnpackNetworkCommand (const CommandPacket &cp)
| Unpack a generic command packet into its actual typed components.
template<Commands Tcmd>
static void | NetworkReplaceCommandClientId (CommandPacket &cp, ClientID client_id)
template<Commands Tcmd, size_t Tcb>
constexpr UnpackNetworkCommandProc | MakeUnpackNetworkCommandCallback () noexcept
template<Commands Tcmd, size_t... i>
constexpr UnpackDispatchT | MakeUnpackNetworkCommand (std::index_sequence< i... >) noexcept
template<typename T , T... i, size_t... j>
constexpr auto | MakeDispatchTable (std::integer_sequence< T, i... >, std::index_sequence< j... >) noexcept
static size_t | FindCallbackIndex (CommandCallback *callback)
| Find the callback index of a callback pointer.
void | NetworkSendCommand (Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, const CommandDataBuffer &cmd_data)
| Prepare a DoCommand to be send over the network.
void | NetworkSyncCommandQueue (NetworkClientSocket *cs)
| Sync our local command queue to the command queue of the given socket.
void | NetworkExecuteLocalCommandQueue ()
| Execute all commands on the local command queue that ought to be executed this frame.
void | NetworkFreeLocalCommandQueue ()
| Free the local command queues.
static void | DistributeCommandPacket (CommandPacket &cp, const NetworkClientSocket *owner)
| "Send" a particular CommandPacket to all clients.
static void | DistributeQueue (CommandQueue &queue, const NetworkClientSocket *owner)
| "Send" a particular CommandQueue to all clients.
void | NetworkDistributeCommands ()
| Distribute the commands of ourself and the clients.
template<class T >
static void | SetClientIdHelper (T &data, ClientID client_id)
| Helper to process a single ClientID argument.
template<class Ttuple , size_t... Tindices>
static void | SetClientIds (Ttuple &values, ClientID client_id, std::index_sequence< Tindices... >)
| Set all invalid ClientID's to the proper value.
void | NetworkReplaceCommandClientId (CommandPacket &cp, ClientID client_id)
| Insert a client ID into the command data in a command packet.
template<class T >
static void | SanitizeSingleStringHelper (CommandFlags cmd_flags, T &data)
| Validate a single string argument coming from network.
template<class Ttuple , size_t... Tindices>
static void | SanitizeStringsHelper (CommandFlags cmd_flags, Ttuple &values, std::index_sequence< Tindices... >)
| Helper function to perform validation on command data strings.
template<Commands Tcmd>
CommandDataBuffer | SanitizeCmdStrings (const CommandDataBuffer &data)
| Validate and sanitize strings in command data.
template<Commands Tcmd, size_t Tcb>
void | UnpackNetworkCommand (const CommandPacket &cp)
| Unpack a generic command packet into its actual typed components.

## Variables
static constexpr auto | _callback_tuple
| Typed list of all possible callbacks.
constexpr size_t | _callback_tuple_size = std::tuple_size_v<decltype(_callback_tuple)>
static const auto | _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{})
| Type-erased table of callbacks.
static constexpr auto | _cmd_dispatch = MakeDispatchTable(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{}, std::make_index_sequence<_callback_tuple_size>{})
| Command dispatch table.
static CommandQueue | _local_wait_queue
| Local queue of packets waiting for handling.
static CommandQueue | _local_execution_queue
| Local queue of packets waiting for execution.

## Detailed Description

Command handling over network connections.

Definition in file network_command.cpp.

## Typedef Documentation

## ◆ UnpackDispatchT

using UnpackDispatchT = std::array<UnpackNetworkCommandProc, _callback_tuple_size>

Definition at line 139 of file network_command.cpp.

## ◆ UnpackNetworkCommandProc

using UnpackNetworkCommandProc = void (*)(const CommandPacket &)

Definition at line 138 of file network_command.cpp.

## Function Documentation

## ◆ DistributeCommandPacket()

static void DistributeCommandPacket | ( | CommandPacket & | cp,
|  | const NetworkClientSocket * | owner
| ) |  |
| static

"Send" a particular CommandPacket to all clients.

Parameters

  cp | The command that has to be distributed.
  owner | The client that owns the command,

Definition at line 301 of file network_command.cpp.

References _frame_counter_max, _local_execution_queue, CommandPacket::callback, CommandPacket::frame, and CommandPacket::my_cmd.

Referenced by DistributeQueue().

## ◆ DistributeQueue()

static void DistributeQueue | ( | CommandQueue & | queue,
|  | const NetworkClientSocket * | owner
| ) |  |
| static

"Send" a particular CommandQueue to all clients.

Parameters

  queue | The queue of commands that has to be distributed.
  owner | The client that owns the commands,

Definition at line 326 of file network_command.cpp.

References _pause_mode, _settings_client, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Any(), NetworkSettings::commands_per_frame, NetworkSettings::commands_per_frame_server, DistributeCommandPacket(), IsCommandAllowedWhilePaused(), ClientSettings::network, and NetworkAdminCmdLogging().

Referenced by NetworkDistributeCommands().

## ◆ FindCallbackIndex()

static size_t FindCallbackIndex | ( | CommandCallback * | callback | ) |
| static

Find the callback index of a callback pointer.

Parameters

  callback | Address of callback to search for.

Returns
  Callback index or std::numeric_limits<size_t>::max() if the function wasn't found in the callback list.

Definition at line 190 of file network_command.cpp.

References _callback_table.

Referenced by NetworkGameSocketHandler::SendCommand().

## ◆ IsNetworkRegisteredCallback()

bool IsNetworkRegisteredCallback | ( | CommandCallback * | callback | ) |

Helper function to ensure that callbacks used when Posting commands are actually registered for the network calls.

Parameters

  callback | The callback to check.

Returns
  `true` when the callback is in the callback table, otherwise `false`.

Definition at line 121 of file network_command.cpp.

References _callback_table.

Referenced by CommandHelper< Tcmd, Tret(*)(DoCommandFlags, Targs...), true >::Post().

## ◆ MakeCallbackTable()

template<size_t... i>

auto MakeCallbackTable | ( | std::index_sequence< i... > |  | ) |
| inlinenoexcept

Definition at line 108 of file network_command.cpp.

## ◆ MakeDispatchTable()

template<typename T , T... i, size_t... j>

constexpr auto MakeDispatchTable | ( | std::integer_sequence< T, i... > | ,
|  | std::index_sequence< j... > |
| ) |  |
| inlineconstexprnoexcept

Definition at line 168 of file network_command.cpp.

## ◆ MakeUnpackNetworkCommand()

template<Commands Tcmd, size_t... i>

constexpr UnpackDispatchT MakeUnpackNetworkCommand | ( | std::index_sequence< i... > |  | ) |
| constexprnoexcept

Definition at line 162 of file network_command.cpp.

## ◆ MakeUnpackNetworkCommandCallback()

template<Commands Tcmd, size_t Tcb>

constexpr UnpackNetworkCommandProc MakeUnpackNetworkCommandCallback | ( |  | ) |
| constexprnoexcept

Definition at line 147 of file network_command.cpp.

## ◆ NetworkDistributeCommands()

void NetworkDistributeCommands | ( |  | ) |

Distribute the commands of ourself and the clients.

Definition at line 357 of file network_command.cpp.

References _local_wait_queue, and DistributeQueue().

## ◆ NetworkExecuteLocalCommandQueue()

void NetworkExecuteLocalCommandQueue | ( |  | ) |

Execute all commands on the local command queue that ought to be executed this frame.

Definition at line 256 of file network_command.cpp.

## ◆ NetworkFreeLocalCommandQueue()

void NetworkFreeLocalCommandQueue | ( |  | ) |

Free the local command queues.

Definition at line 290 of file network_command.cpp.

References _local_execution_queue, and _local_wait_queue.

## ◆ NetworkReplaceCommandClientId() [1/2]

template<Commands Tcmd>

static void NetworkReplaceCommandClientId | ( | CommandPacket & | cp,
|  | ClientID | client_id
| ) |  |
| static

Definition at line 427 of file network_command.cpp.

## ◆ NetworkReplaceCommandClientId() [2/2]

void NetworkReplaceCommandClientId | ( | CommandPacket & | cp,
|  | ClientID | client_id
| ) |  |

Insert a client ID into the command data in a command packet.

Parameters

  cp | Command packet to modify.
  client_id | Client id to insert.

Definition at line 444 of file network_command.cpp.

References _cmd_dispatch, and CommandPacket::cmd.

Referenced by ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND().

## ◆ NetworkSendCommand()

void NetworkSendCommand | ( | Commands | cmd,
|  | StringID | err_message,
|  | CommandCallback * | callback,
|  | CompanyID | company,
|  | const CommandDataBuffer & | cmd_data
| ) |  |

Prepare a DoCommand to be send over the network.

Parameters

  cmd | The command to execute (a CMD_* value)
  err_message | Message prefix to show on error
  callback | A callback function to call after the command is finished
  company | The company that wants to send the command
  cmd_data | The command proc arguments.

Definition at line 207 of file network_command.cpp.

References _frame_counter_max, _local_wait_queue, _network_server, CommandPacket::callback, CommandPacket::cmd, CommandPacket::company, CommandPacket::data, CommandPacket::err_msg, CommandPacket::frame, CommandPacket::my_cmd, and ClientNetworkGameSocketHandler::SendCommand().

Referenced by CommandHelper< Tcmd, Tret(*)(DoCommandFlags, Targs...), true >::SendNet().

## ◆ NetworkSyncCommandQueue()

void NetworkSyncCommandQueue | ( | NetworkClientSocket * | cs | ) |

Sync our local command queue to the command queue of the given socket.

This is needed for the case where we receive a command before saving the game for a joining client, but without the execution of those commands. Not syncing those commands means that the client will never get them and as such will be in a desynced state from the time it started with joining.

Parameters

  cs | The client to sync the queue to.

Definition at line 245 of file network_command.cpp.

References _local_execution_queue, and CommandPacket::callback.

Referenced by ServerNetworkGameSocketHandler::SendMap().

## ◆ SanitizeCmdStrings() [1/2]

template<Commands Tcmd>

static CommandDataBuffer SanitizeCmdStrings | ( | const CommandDataBuffer & | data | ) |
| static

Validate and sanitize strings in command data.

Template Parameters

  Tcmd | Command this data belongs to.

Parameters

  data | Command data.

Returns
  Sanitized command data.

Definition at line 477 of file network_command.cpp.

References SanitizeStringsHelper().

## ◆ SanitizeCmdStrings() [2/2]

template<Commands Tcmd>

CommandDataBuffer SanitizeCmdStrings | ( | const CommandDataBuffer & | data | ) |

Validate and sanitize strings in command data.

Template Parameters

  Tcmd | Command this data belongs to.

Parameters

  data | Command data.

Returns
  Sanitized command data.

Definition at line 477 of file network_command.cpp.

References SanitizeStringsHelper().

## ◆ SanitizeSingleStringHelper()

template<class T >

static void SanitizeSingleStringHelper | ( | CommandFlags | cmd_flags,
|  | T & | data
| ) |  |
| inlinestatic

Validate a single string argument coming from network.

Definition at line 452 of file network_command.cpp.

References _network_server, AllowControlCode, ReplaceWithQuestionMark, StrCtrl, and StrMakeValidInPlace().

Referenced by SanitizeStringsHelper().

## ◆ SanitizeStringsHelper()

template<class Ttuple , size_t... Tindices>

static void SanitizeStringsHelper | ( | CommandFlags | cmd_flags,
|  | Ttuple & | values,
|  | std::index_sequence< Tindices... > |
| ) |  |
| inlinestatic

Helper function to perform validation on command data strings.

Definition at line 465 of file network_command.cpp.

References SanitizeSingleStringHelper().

Referenced by SanitizeCmdStrings().

## ◆ SetClientIdHelper()

template<class T >

static void SetClientIdHelper | ( | T & | data,
|  | ClientID | client_id
| ) |  |
| inlinestatic

Helper to process a single ClientID argument.

Definition at line 412 of file network_command.cpp.

Referenced by SetClientIds(), and CommandHelper< Tcmd, Tret(*)(DoCommandFlags, Targs...), true >::SetClientIds().

## ◆ SetClientIds()

template<class Ttuple , size_t... Tindices>

static void SetClientIds | ( | Ttuple & | values,
|  | ClientID | client_id,
|  | std::index_sequence< Tindices... > |
| ) |  |
| inlinestatic

Set all invalid ClientID's to the proper value.

Definition at line 421 of file network_command.cpp.

References SetClientIdHelper().

## ◆ UnpackNetworkCommand() [1/2]

template<Commands Tcmd, size_t cb>

static void UnpackNetworkCommand | ( | const CommandPacket & | cp | ) |
| static

Unpack a generic command packet into its actual typed components.

Template Parameters

  Tcmd | Command type to be unpacked.
  Tcb | Index into the callback list.

Parameters

  cp | Command packet to unpack.

Definition at line 491 of file network_command.cpp.

References _callback_tuple, CommandPacket::data, CommandPacket::err_msg, and CommandPacket::my_cmd.

## ◆ UnpackNetworkCommand() [2/2]

template<Commands Tcmd, size_t Tcb>

void UnpackNetworkCommand | ( | const CommandPacket & | cp | ) |

Unpack a generic command packet into its actual typed components.

Template Parameters

  Tcmd | Command type to be unpacked.
  Tcb | Index into the callback list.

Parameters

  cp | Command packet to unpack.

Definition at line 491 of file network_command.cpp.

References _callback_tuple, CommandPacket::data, CommandPacket::err_msg, and CommandPacket::my_cmd.

## Variable Documentation

## ◆ _callback_table

const auto _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{})
| static

Type-erased table of callbacks.

Definition at line 114 of file network_command.cpp.

Referenced by FindCallbackIndex(), IsNetworkRegisteredCallback(), and NetworkGameSocketHandler::ReceiveCommand().

## ◆ _callback_tuple

constexpr auto _callback_tuple
| staticconstexpr

Initial value:
= std::make_tuple(
(CommandCallback *)nullptr,
&CcBuildPrimaryVehicle,
&CcBuildAirport,
&CcBuildBridge,
&CcPlaySound_CONSTRUCTION_WATER,
&CcBuildDocks,
&CcFoundTown,
&CcBuildRoadTunnel,
&CcBuildRailTunnel,
&CcBuildWagon,
&CcRoadDepot,
&CcRailDepot,
&CcPlaceSign,
&CcPlaySound_EXPLOSION,
&CcPlaySound_CONSTRUCTION_OTHER,
&CcPlaySound_CONSTRUCTION_RAIL,
&CcStation,
&CcTerraform,
&CcAI,
&CcCloneVehicle,
&CcCreateGroup,
&CcFoundRandomTown,
&CcRoadStop,
&CcStartStopVehicle,
&CcGame,
&CcAddVehicleNewGroup,
&CcMoveStationName,
&CcMoveWaypointName
)
CcBuildBridge
void CcBuildBridge(Commands, const CommandCost &result, TileIndex end_tile, TileIndex tile_start, TransportType transport_type, BridgeType, uint8_t)
Callback executed after a build Bridge CMD has been called.
Definition bridge_gui.cpp:57
CommandCallback
void CommandCallback(Commands cmd, const CommandCost &result, TileIndex tile)
Define a callback function for the client, after the command is finished.
Definition command_type.h:495
CcCloneVehicle
void CcCloneVehicle(Commands cmd, const CommandCost &result, VehicleID veh_id)
This is the Callback method after the cloning attempt of a vehicle.
Definition depot_gui.cpp:124
CcCreateGroup
void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group)
Opens a 'Rename group' window for newly created group.
Definition group_gui.cpp:1282
CcAddVehicleNewGroup
void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool, const VehicleListIdentifier &)
Open rename window after adding a vehicle to a new group via drag and drop.
Definition group_gui.cpp:1295
CcRoadStop
void CcRoadStop(Commands cmd, const CommandCost &result, TileIndex tile, uint8_t width, uint8_t length, RoadStopType, bool is_drive_through, DiagDirection dir, RoadType, RoadStopClassID spec_class, uint16_t spec_index, StationID, bool)
Command callback for building road stops.
Definition road_gui.cpp:195
CcPlaceSign
void CcPlaceSign(Commands, const CommandCost &result, SignID new_sign)
Callback function that is called after a sign is placed.
Definition signs_cmd.cpp:133
CcMoveStationName
void CcMoveStationName(Commands, const CommandCost &result, StationID station_id)
Callback function that is called after a name is moved.
Definition station_cmd.cpp:4539
CcBuildWagon
void CcBuildWagon(Commands cmd, const CommandCost &result, VehicleID new_veh_id, uint, uint16_t, CargoArray, TileIndex tile, EngineID, bool, CargoType, ClientID)
Callback for building wagons.
Definition train_gui.cpp:29
CcStartStopVehicle
void CcStartStopVehicle(Commands cmd, const CommandCost &result, VehicleID veh_id, bool)
This is the Callback method after attempting to start/stop a vehicle.
Definition vehicle_gui.cpp:2921
CcBuildPrimaryVehicle
void CcBuildPrimaryVehicle(Commands cmd, const CommandCost &result, VehicleID new_veh_id, uint, uint16_t, CargoArray)
This is the Callback method after the construction attempt of a primary vehicle.
Definition vehicle_gui.cpp:3495
CcMoveWaypointName
void CcMoveWaypointName(Commands, const CommandCost &result, StationID waypoint_id)
Callback function that is called after a name is moved.
Definition waypoint_cmd.cpp:658

Typed list of all possible callbacks.

Definition at line 58 of file network_command.cpp.

Referenced by UnpackNetworkCommand().

## ◆ _callback_tuple_size

constexpr size_t _callback_tuple_size = std::tuple_size_v<decltype(_callback_tuple)>
| inlineconstexpr

Definition at line 105 of file network_command.cpp.

## ◆ _cmd_dispatch

constexpr auto _cmd_dispatch = MakeDispatchTable(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{}, std::make_index_sequence<_callback_tuple_size>{})
| staticconstexpr

Command dispatch table.

Definition at line 173 of file network_command.cpp.

Referenced by NetworkReplaceCommandClientId(), NetworkGameSocketHandler::ReceiveCommand(), and NetworkGameSocketHandler::SendCommand().

## ◆ _local_execution_queue

CommandQueue _local_execution_queue
| static

Local queue of packets waiting for execution.

Definition at line 182 of file network_command.cpp.

Referenced by DistributeCommandPacket(), NetworkFreeLocalCommandQueue(), and NetworkSyncCommandQueue().

## ◆ _local_wait_queue

CommandQueue _local_wait_queue
| static

Local queue of packets waiting for handling.

Definition at line 180 of file network_command.cpp.

Referenced by NetworkDistributeCommands(), NetworkFreeLocalCommandQueue(), and NetworkSendCommand().