OpenTTD
network_command.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #include "../stdafx.h"
13 #include "network_admin.h"
14 #include "network_client.h"
15 #include "network_server.h"
16 #include "../command_func.h"
17 #include "../company_func.h"
18 #include "../settings_type.h"
19 
20 #include "../safeguards.h"
21 
23 static CommandCallback * const _callback_table[] = {
24  /* 0x00 */ nullptr,
25  /* 0x01 */ CcBuildPrimaryVehicle,
26  /* 0x02 */ CcBuildAirport,
27  /* 0x03 */ CcBuildBridge,
28  /* 0x04 */ CcPlaySound_SPLAT_WATER,
29  /* 0x05 */ CcBuildDocks,
30  /* 0x06 */ CcFoundTown,
31  /* 0x07 */ CcBuildRoadTunnel,
32  /* 0x08 */ CcBuildRailTunnel,
33  /* 0x09 */ CcBuildWagon,
34  /* 0x0A */ CcRoadDepot,
35  /* 0x0B */ CcRailDepot,
36  /* 0x0C */ CcPlaceSign,
37  /* 0x0D */ CcPlaySound_EXPLOSION,
38  /* 0x0E */ CcPlaySound_SPLAT_OTHER,
39  /* 0x0F */ CcPlaySound_SPLAT_RAIL,
40  /* 0x10 */ CcStation,
41  /* 0x11 */ CcTerraform,
42  /* 0x12 */ CcAI,
43  /* 0x13 */ CcCloneVehicle,
44  /* 0x14 */ CcGiveMoney,
45  /* 0x15 */ CcCreateGroup,
46  /* 0x16 */ CcFoundRandomTown,
47  /* 0x17 */ CcRoadStop,
48  /* 0x18 */ CcBuildIndustry,
49  /* 0x19 */ CcStartStopVehicle,
50  /* 0x1A */ CcGame,
51  /* 0x1B */ CcAddVehicleNewGroup,
52 };
53 
60 {
61  CommandPacket *add = MallocT<CommandPacket>(1);
62  *add = *p;
63  add->next = nullptr;
64  if (this->first == nullptr) {
65  this->first = add;
66  } else {
67  this->last->next = add;
68  }
69  this->last = add;
70  this->count++;
71 }
72 
78 CommandPacket *CommandQueue::Pop(bool ignore_paused)
79 {
80  CommandPacket **prev = &this->first;
81  CommandPacket *ret = this->first;
82  CommandPacket *prev_item = nullptr;
83  if (ignore_paused && _pause_mode != PM_UNPAUSED) {
84  while (ret != nullptr && !IsCommandAllowedWhilePaused(ret->cmd)) {
85  prev_item = ret;
86  prev = &ret->next;
87  ret = ret->next;
88  }
89  }
90  if (ret != nullptr) {
91  if (ret == this->last) this->last = prev_item;
92  *prev = ret->next;
93  this->count--;
94  }
95  return ret;
96 }
97 
103 CommandPacket *CommandQueue::Peek(bool ignore_paused)
104 {
105  if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
106 
107  for (CommandPacket *p = this->first; p != nullptr; p = p->next) {
108  if (IsCommandAllowedWhilePaused(p->cmd)) return p;
109  }
110  return nullptr;
111 }
112 
115 {
116  CommandPacket *cp;
117  while ((cp = this->Pop()) != nullptr) {
118  free(cp);
119  }
120  assert(this->count == 0);
121 }
122 
127 
138 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
139 {
140  assert((cmd & CMD_FLAGS_MASK) == 0);
141 
142  CommandPacket c;
143  c.company = company;
144  c.tile = tile;
145  c.p1 = p1;
146  c.p2 = p2;
147  c.cmd = cmd;
148  c.callback = callback;
149 
150  strecpy(c.text, (text != nullptr) ? text : "", lastof(c.text));
151 
152  if (_network_server) {
153  /* If we are the server, we queue the command in our 'special' queue.
154  * In theory, we could execute the command right away, but then the
155  * client on the server can do everything 1 tick faster than others.
156  * So to keep the game fair, we delay the command with 1 tick
157  * which gives about the same speed as most clients.
158  */
159  c.frame = _frame_counter_max + 1;
160  c.my_cmd = true;
161 
162  _local_wait_queue.Append(&c);
163  return;
164  }
165 
166  c.frame = 0; // The client can't tell which frame, so just make it 0
167 
168  /* Clients send their command to the server and forget all about the packet */
170 }
171 
181 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
182 {
183  for (CommandPacket *p = _local_execution_queue.Peek(); p != nullptr; p = p->next) {
184  CommandPacket c = *p;
185  c.callback = 0;
186  cs->outgoing_queue.Append(&c);
187  }
188 }
189 
194 {
195  assert(IsLocalCompany());
196 
198 
199  CommandPacket *cp;
200  while ((cp = queue.Peek()) != nullptr) {
201  /* The queue is always in order, which means
202  * that the first element will be executed first. */
203  if (_frame_counter < cp->frame) break;
204 
205  if (_frame_counter > cp->frame) {
206  /* If we reach here, it means for whatever reason, we've already executed
207  * past the command we need to execute. */
208  error("[net] Trying to execute a packet in the past!");
209  }
210 
211  /* We can execute this command */
213  cp->cmd |= CMD_NETWORK_COMMAND;
214  DoCommandP(cp, cp->my_cmd);
215 
216  queue.Pop();
217  free(cp);
218  }
219 
220  /* Local company may have changed, so we should not restore the old value */
222 }
223 
228 {
229  _local_wait_queue.Free();
230  _local_execution_queue.Free();
231 }
232 
238 static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
239 {
240  CommandCallback *callback = cp.callback;
241  cp.frame = _frame_counter_max + 1;
242 
243  NetworkClientSocket *cs;
245  if (cs->status >= NetworkClientSocket::STATUS_MAP) {
246  /* Callbacks are only send back to the client who sent them in the
247  * first place. This filters that out. */
248  cp.callback = (cs != owner) ? nullptr : callback;
249  cp.my_cmd = (cs == owner);
250  cs->outgoing_queue.Append(&cp);
251  }
252  }
253 
254  cp.callback = (cs != owner) ? nullptr : callback;
255  cp.my_cmd = (cs == owner);
256  _local_execution_queue.Append(&cp);
257 }
258 
264 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
265 {
266 #ifdef DEBUG_DUMP_COMMANDS
267  /* When replaying we do not want this limitation. */
268  int to_go = UINT16_MAX;
269 #else
271 #endif
272 
273  CommandPacket *cp;
274  while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
275  DistributeCommandPacket(*cp, owner);
276  NetworkAdminCmdLogging(owner, cp);
277  free(cp);
278  }
279 }
280 
283 {
284  /* First send the server's commands. */
285  DistributeQueue(&_local_wait_queue, nullptr);
286 
287  /* Then send the queues of the others. */
288  NetworkClientSocket *cs;
290  DistributeQueue(&cs->incoming_queue, cs);
291  }
292 }
293 
301 {
302  cp->company = (CompanyID)p->Recv_uint8();
303  cp->cmd = p->Recv_uint32();
304  if (!IsValidCommand(cp->cmd)) return "invalid command";
305  if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "offline only command";
306  if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
307 
308  cp->p1 = p->Recv_uint32();
309  cp->p2 = p->Recv_uint32();
310  cp->tile = p->Recv_uint32();
312 
313  byte callback = p->Recv_uint8();
314  if (callback >= lengthof(_callback_table)) return "invalid callback";
315 
316  cp->callback = _callback_table[callback];
317  return nullptr;
318 }
319 
326 {
327  p->Send_uint8 (cp->company);
328  p->Send_uint32(cp->cmd);
329  p->Send_uint32(cp->p1);
330  p->Send_uint32(cp->p2);
331  p->Send_uint32(cp->tile);
332  p->Send_string(cp->text);
333 
334  byte callback = 0;
335  while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
336  callback++;
337  }
338 
339  if (callback == lengthof(_callback_table)) {
340  DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", cp->callback);
341  callback = 0; // _callback_table[0] == nullptr
342  }
343  p->Send_uint8 (callback);
344 }
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
"Send" a particular CommandPacket to all clients.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
static bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:45
Internal entity of a packet.
Definition: packet.h:42
CommandPacket * Pop(bool ignore_paused=false)
Return the first item in the queue and remove it from the queue.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:248
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
Prepare a DoCommand to be send over the network.
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:150
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
static CommandQueue _local_execution_queue
Local queue of packets waiting for execution.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:98
uint32 p2
parameter p2.
Definition: command_type.h:480
Client part of the network protocol.
uint count
The number of items in the queue.
Definition: tcp_game.h:134
void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback.
static CommandCallback *const _callback_table[]
Table with all the callbacks we&#39;ll use for conversion.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:119
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.
uint16 commands_per_frame
how many commands may be sent each frame_freq frames?
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
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.
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:63
CommandFlags GetCommandFlags(uint32 cmd)
Definition: command.cpp:386
void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Opens a &#39;Rename group&#39; window for newly created group.
Definition: group_gui.cpp:1052
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including &#39;\0&#39;.
Definition: command_type.h:483
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by AIs.
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:522
bool IsValidCommand(uint32 cmd)
Definition: command.cpp:372
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
A queue of CommandPackets.
Definition: tcp_game.h:131
NetworkSettings network
settings related to the network
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:472
mask for all command flags
Definition: command_type.h:379
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
A normal unpaused game.
Definition: openttd.h:58
Server part of the network protocol.
execute the command without sending it on the network
Definition: command_type.h:378
uint32 p1
parameter p1.
Definition: command_type.h:479
void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by Game Scripts.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
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:1069
CommandPacket * last
The last packet in the queue; only valid when first != nullptr.
Definition: tcp_game.h:133
static ClientNetworkGameSocketHandler * my_client
This is us!
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:101
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:132
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:534
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:122
void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback for building a tunnel.
Definition: rail_gui.cpp:275
CompanyID company
company that is executing the command
void Free()
Free everything that is in the queue.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:49
#define FOR_ALL_CLIENT_SOCKETS(var)
Iterate over all the sockets.
uint32 frame
the frame in which this packet is executed
the command&#39;s string may contain control strings
Definition: command_type.h:398
void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Callback for building wagons.
Definition: train_gui.cpp:32
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition: command_type.h:482
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
bool IsCommandAllowedWhilePaused(uint32 cmd)
Returns whether the command is allowed while the game is paused.
Definition: command.cpp:412
void NetworkFreeLocalCommandQueue()
Free the local command queues.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:219
uint32 _frame_counter
The current frame.
Definition: network.cpp:70
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Replace the unknown/bad bits with question marks.
Definition: string_type.h:52
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
const char * ReceiveCommand(Packet *p, CommandPacket *cp)
Receives a command from the network.
bool _network_server
network-server is active
Definition: network.cpp:55
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
TileIndex tile
tile command being executed on.
Definition: command_type.h:478
Everything we need to know about a command to be able to execute it.
Allow the special control codes.
Definition: string_type.h:54
the command cannot be executed in a multiplayer game; single-player only
Definition: command_type.h:391
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
CommandPacket * Peek(bool ignore_paused=false)
Return the first item in the queue, but don&#39;t remove it.
static NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the server.
static CommandQueue _local_wait_queue
Local queue of packets waiting for handling.
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
bool my_cmd
did the command originate from "me"
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:122
CommandPacket * next
the next command packet (if in queue)
void CcRoadStop(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback for building road stops.
Definition: road_gui.cpp:160
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:69
uint32 cmd
command being executed.
Definition: command_type.h:481
static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
"Send" a particular CommandQueue to all clients.
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a &#39;\0&#39; in the stream.
Definition: packet.cpp:288
Server part of the admin network protocol.