2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file network_command.cpp Command handling over network connections. */
10 #include "../stdafx.h"
11 #include "network_admin.h"
12 #include "network_client.h"
13 #include "network_server.h"
14 #include "../command_func.h"
15 #include "../company_func.h"
16 #include "../settings_type.h"
17 #include "../airport_cmd.h"
18 #include "../aircraft_cmd.h"
19 #include "../autoreplace_cmd.h"
20 #include "../company_cmd.h"
21 #include "../depot_cmd.h"
22 #include "../dock_cmd.h"
23 #include "../economy_cmd.h"
24 #include "../engine_cmd.h"
25 #include "../error_func.h"
26 #include "../goal_cmd.h"
27 #include "../group_cmd.h"
28 #include "../industry_cmd.h"
29 #include "../landscape_cmd.h"
30 #include "../league_cmd.h"
31 #include "../misc_cmd.h"
32 #include "../news_cmd.h"
33 #include "../object_cmd.h"
34 #include "../order_cmd.h"
35 #include "../rail_cmd.h"
36 #include "../road_cmd.h"
37 #include "../roadveh_cmd.h"
38 #include "../settings_cmd.h"
39 #include "../signs_cmd.h"
40 #include "../station_cmd.h"
41 #include "../story_cmd.h"
42 #include "../subsidy_cmd.h"
43 #include "../terraform_cmd.h"
44 #include "../timetable_cmd.h"
45 #include "../town_cmd.h"
46 #include "../train_cmd.h"
47 #include "../tree_cmd.h"
48 #include "../tunnelbridge_cmd.h"
49 #include "../vehicle_cmd.h"
50 #include "../viewport_cmd.h"
51 #include "../water_cmd.h"
52 #include "../waypoint_cmd.h"
53 #include "../script/script_cmd.h"
55 #include "../safeguards.h"
57 /** Typed list of all possible callbacks. */
58 static constexpr auto _callback_tuple
= std::make_tuple(
59 (CommandCallback
*)nullptr, // Make sure this is actually a pointer-to-function.
60 &CcBuildPrimaryVehicle
,
63 &CcPlaySound_CONSTRUCTION_WATER
,
72 &CcPlaySound_EXPLOSION
,
73 &CcPlaySound_CONSTRUCTION_OTHER
,
74 &CcPlaySound_CONSTRUCTION_RAIL
,
88 #ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
90 * We cast specialized function pointers to a generic one, but don't use the
91 * converted value to call the function, which is safe, except that GCC
92 * helpfully thinks it is not.
94 * "Any pointer to function can be converted to a pointer to a different function type.
95 * Calling the function through a pointer to a different function type is undefined,
96 * but converting such pointer back to pointer to the original function type yields
97 * the pointer to the original function." */
98 # pragma GCC diagnostic push
99 # pragma GCC diagnostic ignored "-Wcast-function-type"
102 /* Helpers to generate the callback table from the callback list. */
104 inline constexpr size_t _callback_tuple_size
= std::tuple_size_v
<decltype(_callback_tuple
)>;
106 template <size_t... i
>
107 inline auto MakeCallbackTable(std::index_sequence
<i
...>) noexcept
109 return std::array
<CommandCallback
*, sizeof...(i
)>{{ reinterpret_cast<CommandCallback
*>(reinterpret_cast<void(*)()>(std::get
<i
>(_callback_tuple
)))... }}; // MingW64 fails linking when casting a pointer to its own type. To work around, cast it to some other type first.
112 /** Type-erased table of callbacks. */
113 static auto _callback_table
= MakeCallbackTable(std::make_index_sequence
<_callback_tuple_size
>{});
115 template <typename T
> struct CallbackArgsHelper
;
116 template <typename
... Targs
>
117 struct CallbackArgsHelper
<void(*const)(Commands
, const CommandCost
&, Targs
...)> {
118 using Args
= std::tuple
<std::decay_t
<Targs
>...>;
122 /* Helpers to generate the command dispatch table from the command traits. */
124 template <Commands Tcmd
> static CommandDataBuffer
SanitizeCmdStrings(const CommandDataBuffer
&data
);
125 template <Commands Tcmd
, size_t cb
> static void UnpackNetworkCommand(const CommandPacket
&cp
);
126 template <Commands Tcmd
> static void NetworkReplaceCommandClientId(CommandPacket
&cp
, ClientID client_id
);
127 using UnpackNetworkCommandProc
= void (*)(const CommandPacket
&);
128 using UnpackDispatchT
= std::array
<UnpackNetworkCommandProc
, _callback_tuple_size
>;
129 struct CommandDispatch
{
130 CommandDataBuffer(*Sanitize
)(const CommandDataBuffer
&);
131 void (*ReplaceClientId
)(CommandPacket
&, ClientID
);
132 UnpackDispatchT Unpack
;
135 template <Commands Tcmd
, size_t Tcb
>
136 constexpr UnpackNetworkCommandProc
MakeUnpackNetworkCommandCallback() noexcept
138 /* Check if the callback matches with the command arguments. If not, don't generate an Unpack proc. */
139 using Tcallback
= std::tuple_element_t
<Tcb
, decltype(_callback_tuple
)>;
140 if constexpr (std::is_same_v
<Tcallback
, CommandCallback
* const> || // Callback type is CommandCallback.
141 std::is_same_v
<Tcallback
, CommandCallbackData
* const> || // Callback type is CommandCallbackData.
142 std::is_same_v
<typename CommandTraits
<Tcmd
>::CbArgs
, typename CallbackArgsHelper
<Tcallback
>::Args
> || // Callback proc takes all command return values and parameters.
143 (!std::is_void_v
<typename CommandTraits
<Tcmd
>::RetTypes
> && std::is_same_v
<typename CallbackArgsHelper
<typename CommandTraits
<Tcmd
>::RetCallbackProc
const>::Args
, typename CallbackArgsHelper
<Tcallback
>::Args
>)) { // Callback return is more than CommandCost and the proc takes all return values.
144 return &UnpackNetworkCommand
<Tcmd
, Tcb
>;
150 template <Commands Tcmd
, size_t... i
>
151 constexpr UnpackDispatchT
MakeUnpackNetworkCommand(std::index_sequence
<i
...>) noexcept
153 return UnpackDispatchT
{{ MakeUnpackNetworkCommandCallback
<Tcmd
, i
>()...}};
156 template <typename T
, T
... i
, size_t... j
>
157 inline constexpr auto MakeDispatchTable(std::integer_sequence
<T
, i
...>, std::index_sequence
<j
...>) noexcept
159 return std::array
<CommandDispatch
, sizeof...(i
)>{{ { &SanitizeCmdStrings
<static_cast<Commands
>(i
)>, &NetworkReplaceCommandClientId
<static_cast<Commands
>(i
)>, MakeUnpackNetworkCommand
<static_cast<Commands
>(i
)>(std::make_index_sequence
<_callback_tuple_size
>{}) }... }};
161 /** Command dispatch table. */
162 static constexpr auto _cmd_dispatch
= MakeDispatchTable(std::make_integer_sequence
<std::underlying_type_t
<Commands
>, CMD_END
>{}, std::make_index_sequence
<_callback_tuple_size
>{});
164 #ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
165 # pragma GCC diagnostic pop
168 /** Local queue of packets waiting for handling. */
169 static CommandQueue _local_wait_queue
;
170 /** Local queue of packets waiting for execution. */
171 static CommandQueue _local_execution_queue
;
175 * Find the callback index of a callback pointer.
176 * @param callback Address of callback to search for.
177 * @return Callback index or std::numeric_limits<size_t>::max() if the function wasn't found in the callback list.
179 static size_t FindCallbackIndex(CommandCallback
*callback
)
181 if (auto it
= std::ranges::find(_callback_table
, callback
); it
!= std::end(_callback_table
)) {
182 return static_cast<size_t>(std::distance(std::begin(_callback_table
), it
));
185 return std::numeric_limits
<size_t>::max();
189 * Prepare a DoCommand to be send over the network
190 * @param cmd The command to execute (a CMD_* value)
191 * @param err_message Message prefix to show on error
192 * @param callback A callback function to call after the command is finished
193 * @param company The company that wants to send the command
194 * @param cmd_data The command proc arguments.
196 void NetworkSendCommand(Commands cmd
, StringID err_message
, CommandCallback
*callback
, CompanyID company
, const CommandDataBuffer
&cmd_data
)
201 c
.err_msg
= err_message
;
202 c
.callback
= callback
;
205 if (_network_server
) {
206 /* If we are the server, we queue the command in our 'special' queue.
207 * In theory, we could execute the command right away, but then the
208 * client on the server can do everything 1 tick faster than others.
209 * So to keep the game fair, we delay the command with 1 tick
210 * which gives about the same speed as most clients.
212 c
.frame
= _frame_counter_max
+ 1;
215 _local_wait_queue
.push_back(c
);
219 c
.frame
= 0; // The client can't tell which frame, so just make it 0
221 /* Clients send their command to the server and forget all about the packet */
222 MyClient::SendCommand(c
);
226 * Sync our local command queue to the command queue of the given
227 * socket. This is needed for the case where we receive a command
228 * before saving the game for a joining client, but without the
229 * execution of those commands. Not syncing those commands means
230 * that the client will never get them and as such will be in a
231 * desynced state from the time it started with joining.
232 * @param cs The client to sync the queue to.
234 void NetworkSyncCommandQueue(NetworkClientSocket
*cs
)
236 for (auto &p
: _local_execution_queue
) {
237 CommandPacket
&c
= cs
->outgoing_queue
.emplace_back(p
);
238 c
.callback
= nullptr;
243 * Execute all commands on the local command queue that ought to be executed this frame.
245 void NetworkExecuteLocalCommandQueue()
247 assert(IsLocalCompany());
249 CommandQueue
&queue
= (_network_server
? _local_execution_queue
: ClientNetworkGameSocketHandler::my_client
->incoming_queue
);
251 auto cp
= queue
.begin();
252 for (; cp
!= queue
.end(); cp
++) {
253 /* The queue is always in order, which means
254 * that the first element will be executed first. */
255 if (_frame_counter
< cp
->frame
) break;
257 if (_frame_counter
> cp
->frame
) {
258 /* If we reach here, it means for whatever reason, we've already executed
259 * past the command we need to execute. */
260 FatalError("[net] Trying to execute a packet in the past!");
263 /* We can execute this command */
264 _current_company
= cp
->company
;
265 size_t cb_index
= FindCallbackIndex(cp
->callback
);
266 assert(cb_index
< _callback_tuple_size
);
267 assert(_cmd_dispatch
[cp
->cmd
].Unpack
[cb_index
] != nullptr);
268 _cmd_dispatch
[cp
->cmd
].Unpack
[cb_index
](*cp
);
270 queue
.erase(queue
.begin(), cp
);
272 /* Local company may have changed, so we should not restore the old value */
273 _current_company
= _local_company
;
277 * Free the local command queues.
279 void NetworkFreeLocalCommandQueue()
281 _local_wait_queue
.clear();
282 _local_execution_queue
.clear();
286 * "Send" a particular CommandPacket to all clients.
287 * @param cp The command that has to be distributed.
288 * @param owner The client that owns the command,
290 static void DistributeCommandPacket(CommandPacket
&cp
, const NetworkClientSocket
*owner
)
292 CommandCallback
*callback
= cp
.callback
;
293 cp
.frame
= _frame_counter_max
+ 1;
295 for (NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
296 if (cs
->status
>= NetworkClientSocket::STATUS_MAP
) {
297 /* Callbacks are only send back to the client who sent them in the
298 * first place. This filters that out. */
299 cp
.callback
= (cs
!= owner
) ? nullptr : callback
;
300 cp
.my_cmd
= (cs
== owner
);
301 cs
->outgoing_queue
.push_back(cp
);
305 cp
.callback
= (nullptr != owner
) ? nullptr : callback
;
306 cp
.my_cmd
= (nullptr == owner
);
307 _local_execution_queue
.push_back(cp
);
311 * "Send" a particular CommandQueue to all clients.
312 * @param queue The queue of commands that has to be distributed.
313 * @param owner The client that owns the commands,
315 static void DistributeQueue(CommandQueue
&queue
, const NetworkClientSocket
*owner
)
317 #ifdef DEBUG_DUMP_COMMANDS
318 /* When replaying we do not want this limitation. */
319 int to_go
= UINT16_MAX
;
321 int to_go
= _settings_client
.network
.commands_per_frame
;
322 if (owner
== nullptr) {
323 /* This is the server, use the commands_per_frame_server setting if higher */
324 to_go
= std::max
<int>(to_go
, _settings_client
.network
.commands_per_frame_server
);
328 /* Not technically the most performant way, but consider clients rarely click more than once per tick. */
329 for (auto cp
= queue
.begin(); cp
!= queue
.end(); /* removing some items */) {
330 /* Do not distribute commands when paused and the command is not allowed while paused. */
331 if (_pause_mode
!= PM_UNPAUSED
&& !IsCommandAllowedWhilePaused(cp
->cmd
)) {
336 /* Limit the number of commands per client per tick. */
337 if (--to_go
< 0) break;
339 DistributeCommandPacket(*cp
, owner
);
340 NetworkAdminCmdLogging(owner
, *cp
);
341 cp
= queue
.erase(cp
);
345 /** Distribute the commands of ourself and the clients. */
346 void NetworkDistributeCommands()
348 /* First send the server's commands. */
349 DistributeQueue(_local_wait_queue
, nullptr);
351 /* Then send the queues of the others. */
352 for (NetworkClientSocket
*cs
: NetworkClientSocket::Iterate()) {
353 DistributeQueue(cs
->incoming_queue
, cs
);
358 * Receives a command from the network.
359 * @param p the packet to read from.
360 * @param cp the struct to write the data to.
361 * @return an error message. When nullptr there has been no error.
363 const char *NetworkGameSocketHandler::ReceiveCommand(Packet
&p
, CommandPacket
&cp
)
365 cp
.company
= (CompanyID
)p
.Recv_uint8();
366 cp
.cmd
= static_cast<Commands
>(p
.Recv_uint16());
367 if (!IsValidCommand(cp
.cmd
)) return "invalid command";
368 if (GetCommandFlags(cp
.cmd
) & CMD_OFFLINE
) return "single-player only command";
369 cp
.err_msg
= p
.Recv_uint16();
370 cp
.data
= _cmd_dispatch
[cp
.cmd
].Sanitize(p
.Recv_buffer());
372 uint8_t callback
= p
.Recv_uint8();
373 if (callback
>= _callback_table
.size() || _cmd_dispatch
[cp
.cmd
].Unpack
[callback
] == nullptr) return "invalid callback";
375 cp
.callback
= _callback_table
[callback
];
380 * Sends a command over the network.
381 * @param p the packet to send it in.
382 * @param cp the packet to actually send.
384 void NetworkGameSocketHandler::SendCommand(Packet
&p
, const CommandPacket
&cp
)
386 p
.Send_uint8(cp
.company
);
387 p
.Send_uint16(cp
.cmd
);
388 p
.Send_uint16(cp
.err_msg
);
389 p
.Send_buffer(cp
.data
);
391 size_t callback
= FindCallbackIndex(cp
.callback
);
392 if (callback
> UINT8_MAX
|| _cmd_dispatch
[cp
.cmd
].Unpack
[callback
] == nullptr) {
393 Debug(net
, 0, "Unknown callback for command; no callback sent (command: {})", cp
.cmd
);
394 callback
= 0; // _callback_table[0] == nullptr
396 p
.Send_uint8 ((uint8_t)callback
);
399 /** Helper to process a single ClientID argument. */
401 static inline void SetClientIdHelper(T
&data
, [[maybe_unused
]] ClientID client_id
)
403 if constexpr (std::is_same_v
<ClientID
, T
>) {
408 /** Set all invalid ClientID's to the proper value. */
409 template<class Ttuple
, size_t... Tindices
>
410 static inline void SetClientIds(Ttuple
&values
, ClientID client_id
, std::index_sequence
<Tindices
...>)
412 ((SetClientIdHelper(std::get
<Tindices
>(values
), client_id
)), ...);
415 template <Commands Tcmd
>
416 static void NetworkReplaceCommandClientId(CommandPacket
&cp
, ClientID client_id
)
418 /* Unpack command parameters. */
419 auto params
= EndianBufferReader::ToValue
<typename CommandTraits
<Tcmd
>::Args
>(cp
.data
);
421 /* Insert client id. */
422 SetClientIds(params
, client_id
, std::make_index_sequence
<std::tuple_size_v
<decltype(params
)>>{});
424 /* Repack command parameters. */
425 cp
.data
= EndianBufferWriter
<CommandDataBuffer
>::FromValue(params
);
429 * Insert a client ID into the command data in a command packet.
430 * @param cp Command packet to modify.
431 * @param client_id Client id to insert.
433 void NetworkReplaceCommandClientId(CommandPacket
&cp
, ClientID client_id
)
435 _cmd_dispatch
[cp
.cmd
].ReplaceClientId(cp
, client_id
);
439 /** Validate a single string argument coming from network. */
441 static inline void SanitizeSingleStringHelper([[maybe_unused
]] CommandFlags cmd_flags
, T
&data
)
443 if constexpr (std::is_same_v
<std::string
, T
>) {
444 data
= StrMakeValid(data
, (!_network_server
&& HasFlag(cmd_flags
, CMD_STR_CTRL
)) ? SVS_ALLOW_CONTROL_CODE
| SVS_REPLACE_WITH_QUESTION_MARK
: SVS_REPLACE_WITH_QUESTION_MARK
);
448 /** Helper function to perform validation on command data strings. */
449 template<class Ttuple
, size_t... Tindices
>
450 static inline void SanitizeStringsHelper(CommandFlags cmd_flags
, Ttuple
&values
, std::index_sequence
<Tindices
...>)
452 ((SanitizeSingleStringHelper(cmd_flags
, std::get
<Tindices
>(values
))), ...);
456 * Validate and sanitize strings in command data.
457 * @tparam Tcmd Command this data belongs to.
458 * @param data Command data.
459 * @return Sanitized command data.
461 template <Commands Tcmd
>
462 CommandDataBuffer
SanitizeCmdStrings(const CommandDataBuffer
&data
)
464 auto args
= EndianBufferReader::ToValue
<typename CommandTraits
<Tcmd
>::Args
>(data
);
465 SanitizeStringsHelper(CommandTraits
<Tcmd
>::flags
, args
, std::make_index_sequence
<std::tuple_size_v
<typename CommandTraits
<Tcmd
>::Args
>>{});
466 return EndianBufferWriter
<CommandDataBuffer
>::FromValue(args
);
470 * Unpack a generic command packet into its actual typed components.
471 * @tparam Tcmd Command type to be unpacked.
472 * @tparam Tcb Index into the callback list.
473 * @param cp Command packet to unpack.
475 template <Commands Tcmd
, size_t Tcb
>
476 void UnpackNetworkCommand(const CommandPacket
&cp
)
478 auto args
= EndianBufferReader::ToValue
<typename CommandTraits
<Tcmd
>::Args
>(cp
.data
);
479 Command
<Tcmd
>::PostFromNet(cp
.err_msg
, std::get
<Tcb
>(_callback_tuple
), cp
.my_cmd
, args
);