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

OpenTTD Source 14.0-beta1

network_command.cpp
Go to the documentation of this file.
1 /*
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/>.
6 */
7
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"
54
55 #include "../safeguards.h"
56
58 static constexpr auto _callback_tuple = std::make_tuple(
59 (CommandCallback *)nullptr, // Make sure this is actually a pointer-to-function.
60 &CcBuildPrimaryVehicle,
61 &CcBuildAirport,
62 &CcBuildBridge,
63 &CcPlaySound_CONSTRUCTION_WATER,
64 &CcBuildDocks,
65 &CcFoundTown,
66 &CcBuildRoadTunnel,
67 &CcBuildRailTunnel,
68 &CcBuildWagon,
69 &CcRoadDepot,
70 &CcRailDepot,
71 &CcPlaceSign,
72 &CcPlaySound_EXPLOSION,
73 &CcPlaySound_CONSTRUCTION_OTHER,
74 &CcPlaySound_CONSTRUCTION_RAIL,
75 &CcStation,
76 &CcTerraform,
77 &CcAI,
78 &CcCloneVehicle,
79 &CcCreateGroup,
80 &CcFoundRandomTown,
81 &CcRoadStop,
82 &CcBuildIndustry,
83 &CcStartStopVehicle,
84 &CcGame,
85 &CcAddVehicleNewGroup
86 );
87
88 #ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
89 /*
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.
93 *
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"
100 #endif
101
102 /* Helpers to generate the callback table from the callback list. */
103
104 inline constexpr size_t _callback_tuple_size = std::tuple_size_v<decltype(_callback_tuple)>;
105
106 template <size_t... i>
107 inline auto MakeCallbackTable(std::index_sequence<i...>) noexcept
108 {
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.
110 }
111
113 static auto _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{});
114
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>...>;
119 };
120
121
122 /* Helpers to generate the command dispatch table from the command traits. */
123
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;
133 };
134
135 template <Commands Tcmd, size_t Tcb>
136 constexpr UnpackNetworkCommandProc MakeUnpackNetworkCommandCallback() noexcept
137 {
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>;
145 } else {
146 return nullptr;
147 }
148 }
149
150 template <Commands Tcmd, size_t... i>
151 constexpr UnpackDispatchT MakeUnpackNetworkCommand(std::index_sequence<i...>) noexcept
152 {
153 return UnpackDispatchT{{ MakeUnpackNetworkCommandCallback<Tcmd, i>()...}};
154 }
155
156 template <typename T, T... i, size_t... j>
157 inline constexpr auto MakeDispatchTable(std::integer_sequence<T, i...>, std::index_sequence<j...>) noexcept
158 {
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>{}) }... }};
160 }
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>{});
163
164 #ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
165 # pragma GCC diagnostic pop
166 #endif
167
168
174 void CommandQueue::Append(CommandPacket *p)
175 {
176 CommandPacket *add = new CommandPacket();
177 *add = *p;
178 add->next = nullptr;
179 if (this->first == nullptr) {
180 this->first = add;
181 } else {
182 this->last->next = add;
183 }
184 this->last = add;
185 this->count++;
186 }
187
193 CommandPacket *CommandQueue::Pop(bool ignore_paused)
194 {
195 CommandPacket **prev = &this->first;
196 CommandPacket *ret = this->first;
197 CommandPacket *prev_item = nullptr;
198 if (ignore_paused && _pause_mode != PM_UNPAUSED) {
199 while (ret != nullptr && !IsCommandAllowedWhilePaused(ret->cmd)) {
200 prev_item = ret;
201 prev = &ret->next;
202 ret = ret->next;
203 }
204 }
205 if (ret != nullptr) {
206 if (ret == this->last) this->last = prev_item;
207 *prev = ret->next;
208 this->count--;
209 }
210 return ret;
211 }
212
218 CommandPacket *CommandQueue::Peek(bool ignore_paused)
219 {
220 if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
221
222 for (CommandPacket *p = this->first; p != nullptr; p = p->next) {
223 if (IsCommandAllowedWhilePaused(p->cmd)) return p;
224 }
225 return nullptr;
226 }
227
229 void CommandQueue::Free()
230 {
231 CommandPacket *cp;
232 while ((cp = this->Pop()) != nullptr) {
233 delete cp;
234 }
235 assert(this->count == 0);
236 }
237
239 static CommandQueue _local_wait_queue;
241 static CommandQueue _local_execution_queue;
242
243
249 static size_t FindCallbackIndex(CommandCallback *callback)
250 {
251 if (auto it = std::find(std::cbegin(_callback_table), std::cend(_callback_table), callback); it != std::cend(_callback_table)) {
252 return static_cast<size_t>(std::distance(std::cbegin(_callback_table), it));
253 }
254
255 return std::numeric_limits<size_t>::max();
256 }
257
266 void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, const CommandDataBuffer &cmd_data)
267 {
268 CommandPacket c;
269 c.company = company;
270 c.cmd = cmd;
271 c.err_msg = err_message;
272 c.callback = callback;
273 c.data = cmd_data;
274
275 if (_network_server) {
276 /* If we are the server, we queue the command in our 'special' queue.
277 * In theory, we could execute the command right away, but then the
278 * client on the server can do everything 1 tick faster than others.
279 * So to keep the game fair, we delay the command with 1 tick
280 * which gives about the same speed as most clients.
281 */
282 c.frame = _frame_counter_max + 1;
283 c.my_cmd = true;
284
285 _local_wait_queue.Append(&c);
286 return;
287 }
288
289 c.frame = 0; // The client can't tell which frame, so just make it 0
290
291 /* Clients send their command to the server and forget all about the packet */
292 MyClient::SendCommand(&c);
293 }
294
304 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
305 {
306 for (CommandPacket *p = _local_execution_queue.Peek(); p != nullptr; p = p->next) {
307 CommandPacket c = *p;
308 c.callback = nullptr;
309 cs->outgoing_queue.Append(&c);
310 }
311 }
312
316 void NetworkExecuteLocalCommandQueue()
317 {
318 assert(IsLocalCompany());
319
320 CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
321
322 CommandPacket *cp;
323 while ((cp = queue.Peek()) != nullptr) {
324 /* The queue is always in order, which means
325 * that the first element will be executed first. */
326 if (_frame_counter < cp->frame) break;
327
328 if (_frame_counter > cp->frame) {
329 /* If we reach here, it means for whatever reason, we've already executed
330 * past the command we need to execute. */
331 FatalError("[net] Trying to execute a packet in the past!");
332 }
333
334 /* We can execute this command */
335 _current_company = cp->company;
336 size_t cb_index = FindCallbackIndex(cp->callback);
337 assert(cb_index < _callback_tuple_size);
338 assert(_cmd_dispatch[cp->cmd].Unpack[cb_index] != nullptr);
339 _cmd_dispatch[cp->cmd].Unpack[cb_index](cp);
340
341 queue.Pop();
342 delete cp;
343 }
344
345 /* Local company may have changed, so we should not restore the old value */
346 _current_company = _local_company;
347 }
348
352 void NetworkFreeLocalCommandQueue()
353 {
354 _local_wait_queue.Free();
355 _local_execution_queue.Free();
356 }
357
363 static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
364 {
365 CommandCallback *callback = cp.callback;
366 cp.frame = _frame_counter_max + 1;
367
368 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
369 if (cs->status >= NetworkClientSocket::STATUS_MAP) {
370 /* Callbacks are only send back to the client who sent them in the
371 * first place. This filters that out. */
372 cp.callback = (cs != owner) ? nullptr : callback;
373 cp.my_cmd = (cs == owner);
374 cs->outgoing_queue.Append(&cp);
375 }
376 }
377
378 cp.callback = (nullptr != owner) ? nullptr : callback;
379 cp.my_cmd = (nullptr == owner);
380 _local_execution_queue.Append(&cp);
381 }
382
388 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
389 {
390 #ifdef DEBUG_DUMP_COMMANDS
391 /* When replaying we do not want this limitation. */
392 int to_go = UINT16_MAX;
393 #else
394 int to_go = _settings_client.network.commands_per_frame;
395 if (owner == nullptr) {
396 /* This is the server, use the commands_per_frame_server setting if higher */
397 to_go = std::max<int>(to_go, _settings_client.network.commands_per_frame_server);
398 }
399 #endif
400
401 CommandPacket *cp;
402 while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
403 DistributeCommandPacket(*cp, owner);
404 NetworkAdminCmdLogging(owner, cp);
405 delete cp;
406 }
407 }
408
410 void NetworkDistributeCommands()
411 {
412 /* First send the server's commands. */
413 DistributeQueue(&_local_wait_queue, nullptr);
414
415 /* Then send the queues of the others. */
416 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
417 DistributeQueue(&cs->incoming_queue, cs);
418 }
419 }
420
427 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
428 {
429 cp->company = (CompanyID)p->Recv_uint8();
430 cp->cmd = static_cast<Commands>(p->Recv_uint16());
431 if (!IsValidCommand(cp->cmd)) return "invalid command";
432 if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
433 cp->err_msg = p->Recv_uint16();
434 cp->data = _cmd_dispatch[cp->cmd].Sanitize(p->Recv_buffer());
435
436 byte callback = p->Recv_uint8();
437 if (callback >= _callback_table.size() || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) return "invalid callback";
438
439 cp->callback = _callback_table[callback];
440 return nullptr;
441 }
442
448 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
449 {
450 p->Send_uint8(cp->company);
451 p->Send_uint16(cp->cmd);
452 p->Send_uint16(cp->err_msg);
453 p->Send_buffer(cp->data);
454
455 size_t callback = FindCallbackIndex(cp->callback);
456 if (callback > UINT8_MAX || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) {
457 Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd);
458 callback = 0; // _callback_table[0] == nullptr
459 }
460 p->Send_uint8 ((uint8_t)callback);
461 }
462
464 template <class T>
465 static inline void SetClientIdHelper(T &data, [[maybe_unused]] ClientID client_id)
466 {
467 if constexpr (std::is_same_v<ClientID, T>) {
468 data = client_id;
469 }
470 }
471
473 template<class Ttuple, size_t... Tindices>
474 static inline void SetClientIds(Ttuple &values, ClientID client_id, std::index_sequence<Tindices...>)
475 {
476 ((SetClientIdHelper(std::get<Tindices>(values), client_id)), ...);
477 }
478
479 template <Commands Tcmd>
480 static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
481 {
482 /* Unpack command parameters. */
483 auto params = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp.data);
484
485 /* Insert client id. */
486 SetClientIds(params, client_id, std::make_index_sequence<std::tuple_size_v<decltype(params)>>{});
487
488 /* Repack command parameters. */
489 cp.data = EndianBufferWriter<CommandDataBuffer>::FromValue(params);
490 }
491
497 void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
498 {
499 _cmd_dispatch[cp.cmd].ReplaceClientId(cp, client_id);
500 }
501
502
504 template <class T>
505 static inline void SanitizeSingleStringHelper([[maybe_unused]] CommandFlags cmd_flags, T &data)
506 {
507 if constexpr (std::is_same_v<std::string, T>) {
508 data = StrMakeValid(data.substr(0, NETWORK_COMPANY_NAME_LENGTH), (!_network_server && cmd_flags & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
509 }
510 }
511
513 template<class Ttuple, size_t... Tindices>
514 static inline void SanitizeStringsHelper(CommandFlags cmd_flags, Ttuple &values, std::index_sequence<Tindices...>)
515 {
516 ((SanitizeSingleStringHelper(cmd_flags, std::get<Tindices>(values))), ...);
517 }
518
525 template <Commands Tcmd>
526 CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data)
527 {
528 auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(data);
529 SanitizeStringsHelper(CommandTraits<Tcmd>::flags, args, std::make_index_sequence<std::tuple_size_v<typename CommandTraits<Tcmd>::Args>>{});
530 return EndianBufferWriter<CommandDataBuffer>::FromValue(args);
531 }
532
539 template <Commands Tcmd, size_t Tcb>
540 void UnpackNetworkCommand(const CommandPacket *cp)
541 {
542 auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp->data);
543 Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, args);
544 }
CommandQueue::last
CommandPacket * last
The last packet in the queue; only valid when first != nullptr.
Definition: tcp_game.h:137
CcBuildIndustry
void CcBuildIndustry(Commands cmd, const CommandCost &result, TileIndex tile, IndustryType indtype, uint32_t, bool, uint32_t)
Command callback.
Definition: industry_gui.cpp:251
SetClientIdHelper
static void SetClientIdHelper(T &data, [[maybe_unused]] ClientID client_id)
Helper to process a single ClientID argument.
Definition: network_command.cpp:465
CcBuildRailTunnel
void CcBuildRailTunnel(Commands, const CommandCost &result, TileIndex tile)
Command callback for building a tunnel.
Definition: rail_gui.cpp:296
_local_wait_queue
static CommandQueue _local_wait_queue
Local queue of packets waiting for handling.
Definition: network_command.cpp:239
_cmd_dispatch
static constexpr auto _cmd_dispatch
Command dispatch table.
Definition: network_command.cpp:162
SanitizeStringsHelper
static void SanitizeStringsHelper(CommandFlags cmd_flags, Ttuple &values, std::index_sequence< Tindices... >)
Helper function to perform validation on command data strings.
Definition: network_command.cpp:514
CommandDataBuffer
std::vector< byte > CommandDataBuffer
Storage buffer for serialized command data.
Definition: command_type.h:465
CommandPacket::frame
uint32_t frame
the frame in which this packet is executed
Definition: network_internal.h:114
CcAI
void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
DoCommand callback function for all commands executed by AIs.
Definition: ai_instance.cpp:103
CMD_OFFLINE
@ CMD_OFFLINE
the command cannot be executed in a multiplayer game; single-player only
Definition: command_type.h:394
GetCommandFlags
CommandFlags GetCommandFlags(Commands cmd)
This function mask the parameter with CMD_ID_MASK and returns the flags which belongs to the given co...
Definition: command.cpp:118
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
_network_server
bool _network_server
network-server is active
Definition: network.cpp:60
_local_execution_queue
static CommandQueue _local_execution_queue
Local queue of packets waiting for execution.
Definition: network_command.cpp:241
SanitizeCmdStrings
static CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data)
Validate and sanitize strings in command data.
Definition: network_command.cpp:526
StrMakeValid
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition: string.cpp:114
SanitizeSingleStringHelper
static void SanitizeSingleStringHelper([[maybe_unused]] CommandFlags cmd_flags, T &data)
Validate a single string argument coming from network.
Definition: network_command.cpp:505
CommandPacket::err_msg
StringID err_msg
string ID of error message to use.
Definition: network_internal.h:118
UnpackNetworkCommand
static void UnpackNetworkCommand(const CommandPacket *cp)
Unpack a generic command packet into its actual typed components.
Definition: network_command.cpp:540
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
NetworkSyncCommandQueue
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
Definition: network_command.cpp:304
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:479
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:41
CommandPacket::next
CommandPacket * next
the next command packet (if in queue)
Definition: network_internal.h:112
DistributeCommandPacket
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
"Send" a particular CommandPacket to all clients.
Definition: network_command.cpp:363
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
_callback_table
static auto _callback_table
Type-erased table of callbacks.
Definition: network_command.cpp:113
CommandTraits
Defines the traits of a command.
Definition: command_type.h:448
NetworkReplaceCommandClientId
static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
Insert a client ID into the command data in a command packet.
Definition: network_command.cpp:480
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
CcBuildWagon
void CcBuildWagon(Commands cmd, const CommandCost &result, VehicleID new_veh_id, uint, uint16_t, CargoArray, TileIndex tile, EngineID, bool, CargoID, ClientID)
Callback for building wagons.
Definition: train_gui.cpp:29
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:63
CommandQueue::Append
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
Definition: network_command.cpp:174
CommandPacket::company
CompanyID company
company that is executing the command
Definition: network_internal.h:113
SVS_ALLOW_CONTROL_CODE
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:48
CommandFlags
CommandFlags
Command flags for the command table _command_proc_table.
Definition: command_type.h:391
Packet::Send_uint8
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:129
CommandCost
Common return value for all commands.
Definition: command_type.h:23
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:109
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:2893
NetworkSettings::commands_per_frame_server
uint16_t commands_per_frame_server
how many commands may be sent each frame_freq frames? (server-originating commands)
Definition: settings_type.h:298
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:1189
CcBuildPrimaryVehicle
void CcBuildPrimaryVehicle(Commands, 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:3461
Packet::Send_buffer
void Send_buffer(const std::vector< byte > &data)
Copy a sized byte buffer into the packet.
Definition: packet.cpp:192
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:50
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:49
CommandQueue
A queue of CommandPackets.
Definition: tcp_game.h:135
FindCallbackIndex
static size_t FindCallbackIndex(CommandCallback *callback)
Find the callback index of a callback pointer.
Definition: network_command.cpp:249
CallbackArgsHelper
Definition: network_command.cpp:115
network_client.h
CommandPacket::my_cmd
bool my_cmd
did the command originate from "me"
Definition: network_internal.h:115
Packet::Recv_buffer
std::vector< byte > Recv_buffer()
Extract a sized byte buffer from the packet.
Definition: packet.cpp:384
network_server.h
Packet
Internal entity of a packet.
Definition: packet.h:42
NetworkGameSocketHandler::ReceiveCommand
const char * ReceiveCommand(Packet *p, CommandPacket *cp)
Receives a command from the network.
Definition: network_command.cpp:427
NetworkSettings::commands_per_frame
uint16_t commands_per_frame
how many commands may be sent each frame_freq frames?
Definition: settings_type.h:297
CcBuildBridge
void CcBuildBridge(Commands, const CommandCost &result, TileIndex end_tile, TileIndex tile_start, TransportType transport_type, BridgeType, byte)
Callback executed after a build Bridge CMD has been called.
Definition: bridge_gui.cpp:57
CommandQueue::first
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:136
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:50
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:49
CMD_END
@ CMD_END
Must ALWAYS be on the end of this list!! (period)
Definition: command_type.h:361
IsValidCommand
bool IsValidCommand(Commands cmd)
This function range-checks a cmd.
Definition: command.cpp:106
ClientNetworkGameSocketHandler::SendCommand
static NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the server.
Definition: network_client.cpp:440
CommandQueue::Free
void Free()
Free everything that is in the queue.
Definition: network_command.cpp:229
_frame_counter
uint32_t _frame_counter
The current frame.
Definition: network.cpp:73
CommandPacket::data
CommandDataBuffer data
command parameters.
Definition: network_internal.h:120
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:181
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.
Definition: network_command.cpp:266
CommandQueue::count
uint count
The number of items in the queue.
Definition: tcp_game.h:138
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:123
IsCommandAllowedWhilePaused
bool IsCommandAllowedWhilePaused(Commands cmd)
Returns whether the command is allowed while the game is paused.
Definition: command.cpp:144
CcBuildRoadTunnel
void CcBuildRoadTunnel(Commands, const CommandCost &result, TileIndex start_tile)
Callback executed after a build road tunnel command has been called.
Definition: road_gui.cpp:127
NetworkGameSocketHandler::incoming_queue
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:512
CommandDispatch
Definition: network_command.cpp:129
CommandHelper
Definition: command_func.h:93
CommandQueue::Peek
CommandPacket * Peek(bool ignore_paused=false)
Return the first item in the queue, but don't remove it.
Definition: network_command.cpp:218
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:637
EndianBufferWriter
Endian-aware buffer adapter that always writes values in little endian order.
Definition: endian_buffer.hpp:26
NetworkGameSocketHandler::SendCommand
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
Definition: network_command.cpp:448
NetworkExecuteLocalCommandQueue
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:316
Commands
Commands
List of commands.
Definition: command_type.h:187
_callback_tuple
static constexpr auto _callback_tuple
Typed list of all possible callbacks.
Definition: network_command.cpp:58
NetworkAdminCmdLogging
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
Definition: network_admin.cpp:962
Packet::Recv_uint16
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:331
network_admin.h
CMD_STR_CTRL
@ CMD_STR_CTRL
the command's string may contain control strings
Definition: command_type.h:401
CommandPacket::callback
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition: network_internal.h:119
NetworkFreeLocalCommandQueue
void NetworkFreeLocalCommandQueue()
Free the local command queues.
Definition: network_command.cpp:352
IsLocalCompany
bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:47
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:46
SetClientIds
static void SetClientIds(Ttuple &values, ClientID client_id, std::index_sequence< Tindices... >)
Set all invalid ClientID's to the proper value.
Definition: network_command.cpp:474
CcPlaceSign
void CcPlaceSign(Commands, const CommandCost &result, SignID new_sign)
Callback function that is called after a sign is placed.
Definition: signs_cmd.cpp:108
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:1176
_frame_counter_max
uint32_t _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:72
Packet::Send_uint16
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:139
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:317
CcGame
void CcGame(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
DoCommand callback function for all commands executed by Game Scripts.
Definition: game_instance.cpp:90
NetworkDistributeCommands
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
Definition: network_command.cpp:410
DistributeQueue
static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
"Send" a particular CommandQueue to all clients.
Definition: network_command.cpp:388
NETWORK_COMPANY_NAME_LENGTH
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including '\0'.
Definition: config.h:54
CommandQueue::Pop
CommandPacket * Pop(bool ignore_paused=false)
Return the first item in the queue and remove it from the queue.
Definition: network_command.cpp:193
CommandPacket::cmd
Commands cmd
command being executed.
Definition: network_internal.h:117