OpenTTD Source  12.0-beta2
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 
18 #include "../safeguards.h"
19 
21 static CommandCallback * const _callback_table[] = {
22  /* 0x00 */ nullptr,
23  /* 0x01 */ CcBuildPrimaryVehicle,
24  /* 0x02 */ CcBuildAirport,
25  /* 0x03 */ CcBuildBridge,
26  /* 0x04 */ CcPlaySound_CONSTRUCTION_WATER,
27  /* 0x05 */ CcBuildDocks,
28  /* 0x06 */ CcFoundTown,
29  /* 0x07 */ CcBuildRoadTunnel,
30  /* 0x08 */ CcBuildRailTunnel,
31  /* 0x09 */ CcBuildWagon,
32  /* 0x0A */ CcRoadDepot,
33  /* 0x0B */ CcRailDepot,
34  /* 0x0C */ CcPlaceSign,
35  /* 0x0D */ CcPlaySound_EXPLOSION,
36  /* 0x0E */ CcPlaySound_CONSTRUCTION_OTHER,
37  /* 0x0F */ CcPlaySound_CONSTRUCTION_RAIL,
38  /* 0x10 */ CcStation,
39  /* 0x11 */ CcTerraform,
40  /* 0x12 */ CcAI,
41  /* 0x13 */ CcCloneVehicle,
42  /* 0x14 */ nullptr,
43  /* 0x15 */ CcCreateGroup,
44  /* 0x16 */ CcFoundRandomTown,
45  /* 0x17 */ CcRoadStop,
46  /* 0x18 */ CcBuildIndustry,
47  /* 0x19 */ CcStartStopVehicle,
48  /* 0x1A */ CcGame,
49  /* 0x1B */ CcAddVehicleNewGroup,
50 };
51 
58 {
59  CommandPacket *add = new CommandPacket();
60  *add = *p;
61  add->next = nullptr;
62  if (this->first == nullptr) {
63  this->first = add;
64  } else {
65  this->last->next = add;
66  }
67  this->last = add;
68  this->count++;
69 }
70 
76 CommandPacket *CommandQueue::Pop(bool ignore_paused)
77 {
78  CommandPacket **prev = &this->first;
79  CommandPacket *ret = this->first;
80  CommandPacket *prev_item = nullptr;
81  if (ignore_paused && _pause_mode != PM_UNPAUSED) {
82  while (ret != nullptr && !IsCommandAllowedWhilePaused(ret->cmd)) {
83  prev_item = ret;
84  prev = &ret->next;
85  ret = ret->next;
86  }
87  }
88  if (ret != nullptr) {
89  if (ret == this->last) this->last = prev_item;
90  *prev = ret->next;
91  this->count--;
92  }
93  return ret;
94 }
95 
101 CommandPacket *CommandQueue::Peek(bool ignore_paused)
102 {
103  if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
104 
105  for (CommandPacket *p = this->first; p != nullptr; p = p->next) {
106  if (IsCommandAllowedWhilePaused(p->cmd)) return p;
107  }
108  return nullptr;
109 }
110 
113 {
114  CommandPacket *cp;
115  while ((cp = this->Pop()) != nullptr) {
116  delete cp;
117  }
118  assert(this->count == 0);
119 }
120 
125 
136 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
137 {
138  assert((cmd & CMD_FLAGS_MASK) == 0);
139 
140  CommandPacket c;
141  c.company = company;
142  c.tile = tile;
143  c.p1 = p1;
144  c.p2 = p2;
145  c.cmd = cmd;
146  c.callback = callback;
147  c.text = text;
148 
149  if (_network_server) {
150  /* If we are the server, we queue the command in our 'special' queue.
151  * In theory, we could execute the command right away, but then the
152  * client on the server can do everything 1 tick faster than others.
153  * So to keep the game fair, we delay the command with 1 tick
154  * which gives about the same speed as most clients.
155  */
156  c.frame = _frame_counter_max + 1;
157  c.my_cmd = true;
158 
160  return;
161  }
162 
163  c.frame = 0; // The client can't tell which frame, so just make it 0
164 
165  /* Clients send their command to the server and forget all about the packet */
167 }
168 
178 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
179 {
180  for (CommandPacket *p = _local_execution_queue.Peek(); p != nullptr; p = p->next) {
181  CommandPacket c = *p;
182  c.callback = nullptr;
183  cs->outgoing_queue.Append(&c);
184  }
185 }
186 
191 {
192  assert(IsLocalCompany());
193 
195 
196  CommandPacket *cp;
197  while ((cp = queue.Peek()) != nullptr) {
198  /* The queue is always in order, which means
199  * that the first element will be executed first. */
200  if (_frame_counter < cp->frame) break;
201 
202  if (_frame_counter > cp->frame) {
203  /* If we reach here, it means for whatever reason, we've already executed
204  * past the command we need to execute. */
205  error("[net] Trying to execute a packet in the past!");
206  }
207 
208  /* We can execute this command */
210  cp->cmd |= CMD_NETWORK_COMMAND;
211  DoCommandP(cp, cp->my_cmd);
212 
213  queue.Pop();
214  delete cp;
215  }
216 
217  /* Local company may have changed, so we should not restore the old value */
219 }
220 
225 {
228 }
229 
235 static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
236 {
237  CommandCallback *callback = cp.callback;
238  cp.frame = _frame_counter_max + 1;
239 
240  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
241  if (cs->status >= NetworkClientSocket::STATUS_MAP) {
242  /* Callbacks are only send back to the client who sent them in the
243  * first place. This filters that out. */
244  cp.callback = (cs != owner) ? nullptr : callback;
245  cp.my_cmd = (cs == owner);
246  cs->outgoing_queue.Append(&cp);
247  }
248  }
249 
250  cp.callback = (nullptr != owner) ? nullptr : callback;
251  cp.my_cmd = (nullptr == owner);
253 }
254 
260 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
261 {
262 #ifdef DEBUG_DUMP_COMMANDS
263  /* When replaying we do not want this limitation. */
264  int to_go = UINT16_MAX;
265 #else
267 #endif
268 
269  CommandPacket *cp;
270  while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
271  DistributeCommandPacket(*cp, owner);
272  NetworkAdminCmdLogging(owner, cp);
273  delete cp;
274  }
275 }
276 
279 {
280  /* First send the server's commands. */
282 
283  /* Then send the queues of the others. */
284  for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
285  DistributeQueue(&cs->incoming_queue, cs);
286  }
287 }
288 
296 {
297  cp->company = (CompanyID)p->Recv_uint8();
298  cp->cmd = p->Recv_uint32();
299  if (!IsValidCommand(cp->cmd)) return "invalid command";
300  if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
301  if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
302 
303  cp->p1 = p->Recv_uint32();
304  cp->p2 = p->Recv_uint32();
305  cp->tile = p->Recv_uint32();
307 
308  byte callback = p->Recv_uint8();
309  if (callback >= lengthof(_callback_table)) return "invalid callback";
310 
311  cp->callback = _callback_table[callback];
312  return nullptr;
313 }
314 
321 {
322  p->Send_uint8 (cp->company);
323  p->Send_uint32(cp->cmd);
324  p->Send_uint32(cp->p1);
325  p->Send_uint32(cp->p2);
326  p->Send_uint32(cp->tile);
327  p->Send_string(cp->text);
328 
329  byte callback = 0;
330  while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
331  callback++;
332  }
333 
334  if (callback == lengthof(_callback_table)) {
335  Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd);
336  callback = 0; // _callback_table[0] == nullptr
337  }
338  p->Send_uint8 (callback);
339 }
CommandQueue::last
CommandPacket * last
The last packet in the queue; only valid when first != nullptr.
Definition: tcp_game.h:136
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
CommandContainer::cmd
uint32 cmd
command being executed.
Definition: command_type.h:483
_frame_counter
uint32 _frame_counter
The current frame.
Definition: network.cpp:70
_local_wait_queue
static CommandQueue _local_wait_queue
Local queue of packets waiting for handling.
Definition: network_command.cpp:122
CcBuildPrimaryVehicle
void CcBuildPrimaryVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
This is the Callback method after the construction attempt of a primary vehicle.
Definition: vehicle_gui.cpp:3093
CMD_OFFLINE
@ CMD_OFFLINE
the command cannot be executed in a multiplayer game; single-player only
Definition: command_type.h:393
_callback_table
static CommandCallback *const _callback_table[]
Table with all the callbacks we'll use for conversion.
Definition: network_command.cpp:21
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
CommandPacket::frame
uint32 frame
the frame in which this packet is executed
Definition: network_internal.h:112
_local_execution_queue
static CommandQueue _local_execution_queue
Local queue of packets waiting for execution.
Definition: network_command.cpp:124
CcRoadStop
void CcRoadStop(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback for building road stops.
Definition: road_gui.cpp:158
CcStartStopVehicle
void CcStartStopVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
This is the Callback method after attempting to start/stop a vehicle.
Definition: vehicle_gui.cpp:2592
CommandCallback
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:474
CommandContainer::p2
uint32 p2
parameter p2.
Definition: command_type.h:482
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
NetworkSyncCommandQueue
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
Definition: network_command.cpp:178
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:42
CommandPacket::next
CommandPacket * next
the next command packet (if in queue)
Definition: network_internal.h:110
DistributeCommandPacket
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
"Send" a particular CommandPacket to all clients.
Definition: network_command.cpp:235
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
IsCommandAllowedWhilePaused
bool IsCommandAllowedWhilePaused(uint32 cmd)
Returns whether the command is allowed while the game is paused.
Definition: command.cpp:419
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:129
IsLocalCompany
static bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:43
_frame_counter_max
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:69
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:150
CcBuildBridge
void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2, uint32 cmd)
Callback executed after a build Bridge CMD has been called.
Definition: bridge_gui.cpp:61
Packet::Recv_string
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition: packet.cpp:380
CcAddVehicleNewGroup
void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Open rename window after adding a vehicle to a new group via drag and drop.
Definition: group_gui.cpp:1157
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:61
CommandQueue::Append
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
Definition: network_command.cpp:57
CommandContainer::text
std::string text
possible text sent for name changes etc.
Definition: command_type.h:485
IsValidCommand
bool IsValidCommand(uint32 cmd)
Definition: command.cpp:379
CommandPacket::company
CompanyID company
company that is executing the command
Definition: network_internal.h:111
CcCreateGroup
void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Opens a 'Rename group' window for newly created group.
Definition: group_gui.cpp:1140
SVS_ALLOW_CONTROL_CODE
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:52
CcBuildIndustry
void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback.
Definition: industry_gui.cpp:227
CommandContainer::p1
uint32 p1
parameter p1.
Definition: command_type.h:481
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:335
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:107
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
CcPlaceSign
void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Callback function that is called after a sign is placed.
Definition: signs_cmd.cpp:117
NetworkSendCommand
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
Prepare a DoCommand to be send over the network.
Definition: network_command.cpp:136
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
CommandQueue
A queue of CommandPackets.
Definition: tcp_game.h:134
CcGame
void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by Game Scripts.
Definition: game_instance.cpp:90
network_client.h
CommandPacket::my_cmd
bool my_cmd
did the command originate from "me"
Definition: network_internal.h:113
CMD_FLAGS_MASK
@ CMD_FLAGS_MASK
mask for all command flags
Definition: command_type.h:381
network_server.h
Packet
Internal entity of a packet.
Definition: packet.h:44
NetworkGameSocketHandler::ReceiveCommand
const char * ReceiveCommand(Packet *p, CommandPacket *cp)
Receives a command from the network.
Definition: network_command.cpp:295
GetCommandFlags
CommandFlags GetCommandFlags(uint32 cmd)
Definition: command.cpp:393
CcBuildWagon
void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Callback for building wagons.
Definition: train_gui.cpp:30
CommandContainer::callback
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition: command_type.h:484
CommandQueue::first
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:135
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
CMD_NETWORK_COMMAND
@ CMD_NETWORK_COMMAND
execute the command without sending it on the network
Definition: command_type.h:380
ClientNetworkGameSocketHandler::SendCommand
static NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the server.
Definition: network_client.cpp:426
CommandQueue::Free
void Free()
Free everything that is in the queue.
Definition: network_command.cpp:112
CommandQueue::count
uint count
The number of items in the queue.
Definition: tcp_game.h:137
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:306
CcBuildRailTunnel
void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback for building a tunnel.
Definition: rail_gui.cpp:278
NetworkGameSocketHandler::incoming_queue
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:504
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
NetworkSettings::commands_per_frame
uint16 commands_per_frame
how many commands may be sent each frame_freq frames?
Definition: settings_type.h:263
Packet::Send_string
void Send_string(const std::string_view data)
Sends a string over the network.
Definition: packet.cpp:181
CommandQueue::Peek
CommandPacket * Peek(bool ignore_paused=false)
Return the first item in the queue, but don't remove it.
Definition: network_command.cpp:101
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:594
CcCloneVehicle
void CcCloneVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
This is the Callback method after the cloning attempt of a vehicle.
Definition: depot_gui.cpp:120
NetworkGameSocketHandler::SendCommand
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
Definition: network_command.cpp:320
CcAI
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by AIs.
Definition: ai_instance.cpp:101
NetworkExecuteLocalCommandQueue
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:190
NetworkAdminCmdLogging
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
Definition: network_admin.cpp:954
network_admin.h
CommandContainer::tile
TileIndex tile
tile command being executed on.
Definition: command_type.h:480
CMD_STR_CTRL
@ CMD_STR_CTRL
the command's string may contain control strings
Definition: command_type.h:400
NetworkFreeLocalCommandQueue
void NetworkFreeLocalCommandQueue()
Free the local command queues.
Definition: network_command.cpp:224
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
CcBuildRoadTunnel
void CcBuildRoadTunnel(const CommandCost &result, TileIndex start_tile, uint32 p1, uint32 p2, uint32 cmd)
Callback executed after a build road tunnel command has been called.
Definition: road_gui.cpp:99
NetworkDistributeCommands
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
Definition: network_command.cpp:278
DistributeQueue
static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
"Send" a particular CommandQueue to all clients.
Definition: network_command.cpp:260
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:56
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:76