OpenTTD Source
20241121-master-g67a0fccfad
|
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 |
template<Commands Tcmd> | |
static CommandDataBuffer | SanitizeCmdStrings (const CommandDataBuffer &data) |
Validate and sanitize strings in command data. More... | |
template<Commands Tcmd, size_t cb> | |
static void | UnpackNetworkCommand (const CommandPacket &cp) |
Unpack a generic command packet into its actual typed components. More... | |
template<Commands Tcmd> | |
static void | NetworkReplaceCommandClientId (CommandPacket &cp, ClientID client_id) |
Insert a client ID into the command data in a command packet. More... | |
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. More... | |
void | NetworkSendCommand (Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, const CommandDataBuffer &cmd_data) |
Prepare a DoCommand to be send over the network. More... | |
void | NetworkSyncCommandQueue (NetworkClientSocket *cs) |
Sync our local command queue to the command queue of the given socket. More... | |
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. More... | |
static void | DistributeQueue (CommandQueue &queue, const NetworkClientSocket *owner) |
"Send" a particular CommandQueue to all clients. More... | |
void | NetworkDistributeCommands () |
Distribute the commands of ourself and the clients. | |
template<class T > | |
static void | SetClientIdHelper (T &data, [[maybe_unused]] 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. | |
template<class T > | |
static void | SanitizeSingleStringHelper ([[maybe_unused]] 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. More... | |
template<Commands Tcmd, size_t Tcb> | |
void | UnpackNetworkCommand (const CommandPacket &cp) |
Unpack a generic command packet into its actual typed components. More... | |
Variables | |
static constexpr auto | _callback_tuple |
Typed list of all possible callbacks. More... | |
constexpr size_t | _callback_tuple_size = std::tuple_size_v<decltype(_callback_tuple)> |
static 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. | |
Command handling over network connections.
Definition in file network_command.cpp.
|
static |
"Send" a particular CommandPacket to all clients.
cp | The command that has to be distributed. |
owner | The client that owns the command, |
Definition at line 290 of file network_command.cpp.
References _frame_counter_max, _local_execution_queue, CommandPacket::callback, CommandPacket::frame, and CommandPacket::my_cmd.
Referenced by DistributeQueue().
|
static |
"Send" a particular CommandQueue to all clients.
queue | The queue of commands that has to be distributed. |
owner | The client that owns the commands, |
Definition at line 315 of file network_command.cpp.
References _pause_mode, _settings_client, NetworkSettings::commands_per_frame, NetworkSettings::commands_per_frame_server, DistributeCommandPacket(), IsCommandAllowedWhilePaused(), ClientSettings::network, NetworkAdminCmdLogging(), and PM_UNPAUSED.
Referenced by NetworkDistributeCommands().
|
static |
Find the callback index of a callback pointer.
callback | Address of callback to search for. |
Definition at line 179 of file network_command.cpp.
References _callback_table.
Referenced by NetworkGameSocketHandler::SendCommand().
|
static |
Insert a client ID into the command data in a command packet.
cp | Command packet to modify. |
client_id | Client id to insert. |
Definition at line 416 of file network_command.cpp.
References CommandPacket::data, and SetClientIds().
void NetworkSendCommand | ( | Commands | cmd, |
StringID | err_message, | ||
CommandCallback * | callback, | ||
CompanyID | company, | ||
const CommandDataBuffer & | cmd_data | ||
) |
Prepare a DoCommand to be send over the network.
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 196 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(*)(DoCommandFlag, Targs...), true >::SendNet().
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.
cs | The client to sync the queue to. |
Definition at line 234 of file network_command.cpp.
References _local_execution_queue, and CommandPacket::callback.
|
static |
Validate and sanitize strings in command data.
Tcmd | Command this data belongs to. |
data | Command data. |
Definition at line 462 of file network_command.cpp.
References SanitizeStringsHelper().
CommandDataBuffer SanitizeCmdStrings | ( | const CommandDataBuffer & | data | ) |
Validate and sanitize strings in command data.
Tcmd | Command this data belongs to. |
data | Command data. |
Definition at line 462 of file network_command.cpp.
References SanitizeStringsHelper().
|
static |
Unpack a generic command packet into its actual typed components.
Tcmd | Command type to be unpacked. |
Tcb | Index into the callback list. |
cp | Command packet to unpack. |
Definition at line 476 of file network_command.cpp.
References _callback_tuple, CommandPacket::data, CommandPacket::err_msg, and CommandPacket::my_cmd.
void UnpackNetworkCommand | ( | const CommandPacket & | cp | ) |
Unpack a generic command packet into its actual typed components.
Tcmd | Command type to be unpacked. |
Tcb | Index into the callback list. |
cp | Command packet to unpack. |
Definition at line 476 of file network_command.cpp.
References _callback_tuple, CommandPacket::data, CommandPacket::err_msg, and CommandPacket::my_cmd.
|
staticconstexpr |
Typed list of all possible callbacks.
Definition at line 58 of file network_command.cpp.
Referenced by UnpackNetworkCommand().