OpenTTD
network_command.cpp
Go to the documentation of this file.
1 /* $Id: network_command.cpp 27571 2016-05-22 10:07:48Z frosch $ */
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 #ifdef ENABLE_NETWORK
13 
14 #include "../stdafx.h"
15 #include "network_admin.h"
16 #include "network_client.h"
17 #include "network_server.h"
18 #include "../command_func.h"
19 #include "../company_func.h"
20 #include "../settings_type.h"
21 
22 #include "../safeguards.h"
23 
25 static CommandCallback * const _callback_table[] = {
26  /* 0x00 */ NULL,
27  /* 0x01 */ CcBuildPrimaryVehicle,
28  /* 0x02 */ CcBuildAirport,
29  /* 0x03 */ CcBuildBridge,
30  /* 0x04 */ CcPlaySound_SPLAT_WATER,
31  /* 0x05 */ CcBuildDocks,
32  /* 0x06 */ CcFoundTown,
33  /* 0x07 */ CcBuildRoadTunnel,
34  /* 0x08 */ CcBuildRailTunnel,
35  /* 0x09 */ CcBuildWagon,
36  /* 0x0A */ CcRoadDepot,
37  /* 0x0B */ CcRailDepot,
38  /* 0x0C */ CcPlaceSign,
39  /* 0x0D */ CcPlaySound_EXPLOSION,
40  /* 0x0E */ CcPlaySound_SPLAT_OTHER,
41  /* 0x0F */ CcPlaySound_SPLAT_RAIL,
42  /* 0x10 */ CcStation,
43  /* 0x11 */ CcTerraform,
44  /* 0x12 */ CcAI,
45  /* 0x13 */ CcCloneVehicle,
46  /* 0x14 */ CcGiveMoney,
47  /* 0x15 */ CcCreateGroup,
48  /* 0x16 */ CcFoundRandomTown,
49  /* 0x17 */ CcRoadStop,
50  /* 0x18 */ CcBuildIndustry,
51  /* 0x19 */ CcStartStopVehicle,
52  /* 0x1A */ CcGame,
53  /* 0x1B */ CcAddVehicleNewGroup,
54 };
55 
62 {
63  CommandPacket *add = MallocT<CommandPacket>(1);
64  *add = *p;
65  add->next = NULL;
66  if (this->first == NULL) {
67  this->first = add;
68  } else {
69  this->last->next = add;
70  }
71  this->last = add;
72  this->count++;
73 }
74 
80 CommandPacket *CommandQueue::Pop(bool ignore_paused)
81 {
82  CommandPacket **prev = &this->first;
83  CommandPacket *ret = this->first;
84  CommandPacket *prev_item = NULL;
85  if (ignore_paused && _pause_mode != PM_UNPAUSED) {
86  while (ret != NULL && !IsCommandAllowedWhilePaused(ret->cmd)) {
87  prev_item = ret;
88  prev = &ret->next;
89  ret = ret->next;
90  }
91  }
92  if (ret != NULL) {
93  if (ret == this->last) this->last = prev_item;
94  *prev = ret->next;
95  this->count--;
96  }
97  return ret;
98 }
99 
105 CommandPacket *CommandQueue::Peek(bool ignore_paused)
106 {
107  if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
108 
109  for (CommandPacket *p = this->first; p != NULL; p = p->next) {
110  if (IsCommandAllowedWhilePaused(p->cmd)) return p;
111  }
112  return NULL;
113 }
114 
117 {
118  CommandPacket *cp;
119  while ((cp = this->Pop()) != NULL) {
120  free(cp);
121  }
122  assert(this->count == 0);
123 }
124 
129 
140 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
141 {
142  assert((cmd & CMD_FLAGS_MASK) == 0);
143 
144  CommandPacket c;
145  c.company = company;
146  c.tile = tile;
147  c.p1 = p1;
148  c.p2 = p2;
149  c.cmd = cmd;
150  c.callback = callback;
151 
152  strecpy(c.text, (text != NULL) ? text : "", lastof(c.text));
153 
154  if (_network_server) {
155  /* If we are the server, we queue the command in our 'special' queue.
156  * In theory, we could execute the command right away, but then the
157  * client on the server can do everything 1 tick faster than others.
158  * So to keep the game fair, we delay the command with 1 tick
159  * which gives about the same speed as most clients.
160  */
161  c.frame = _frame_counter_max + 1;
162  c.my_cmd = true;
163 
164  _local_wait_queue.Append(&c);
165  return;
166  }
167 
168  c.frame = 0; // The client can't tell which frame, so just make it 0
169 
170  /* Clients send their command to the server and forget all about the packet */
172 }
173 
183 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
184 {
185  for (CommandPacket *p = _local_execution_queue.Peek(); p != NULL; p = p->next) {
186  CommandPacket c = *p;
187  c.callback = 0;
188  cs->outgoing_queue.Append(&c);
189  }
190 }
191 
196 {
197  assert(IsLocalCompany());
198 
200 
201  CommandPacket *cp;
202  while ((cp = queue.Peek()) != NULL) {
203  /* The queue is always in order, which means
204  * that the first element will be executed first. */
205  if (_frame_counter < cp->frame) break;
206 
207  if (_frame_counter > cp->frame) {
208  /* If we reach here, it means for whatever reason, we've already executed
209  * past the command we need to execute. */
210  error("[net] Trying to execute a packet in the past!");
211  }
212 
213  /* We can execute this command */
215  cp->cmd |= CMD_NETWORK_COMMAND;
216  DoCommandP(cp, cp->my_cmd);
217 
218  queue.Pop();
219  free(cp);
220  }
221 
222  /* Local company may have changed, so we should not restore the old value */
224 }
225 
230 {
231  _local_wait_queue.Free();
232  _local_execution_queue.Free();
233 }
234 
240 static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
241 {
242  CommandCallback *callback = cp.callback;
243  cp.frame = _frame_counter_max + 1;
244 
245  NetworkClientSocket *cs;
247  if (cs->status >= NetworkClientSocket::STATUS_MAP) {
248  /* Callbacks are only send back to the client who sent them in the
249  * first place. This filters that out. */
250  cp.callback = (cs != owner) ? NULL : callback;
251  cp.my_cmd = (cs == owner);
252  cs->outgoing_queue.Append(&cp);
253  }
254  }
255 
256  cp.callback = (cs != owner) ? NULL : callback;
257  cp.my_cmd = (cs == owner);
258  _local_execution_queue.Append(&cp);
259 }
260 
266 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
267 {
268 #ifdef DEBUG_DUMP_COMMANDS
269  /* When replaying we do not want this limitation. */
270  int to_go = UINT16_MAX;
271 #else
273 #endif
274 
275  CommandPacket *cp;
276  while (--to_go >= 0 && (cp = queue->Pop(true)) != NULL) {
277  DistributeCommandPacket(*cp, owner);
278  NetworkAdminCmdLogging(owner, cp);
279  free(cp);
280  }
281 }
282 
285 {
286  /* First send the server's commands. */
287  DistributeQueue(&_local_wait_queue, NULL);
288 
289  /* Then send the queues of the others. */
290  NetworkClientSocket *cs;
292  DistributeQueue(&cs->incoming_queue, cs);
293  }
294 }
295 
303 {
304  cp->company = (CompanyID)p->Recv_uint8();
305  cp->cmd = p->Recv_uint32();
306  if (!IsValidCommand(cp->cmd)) return "invalid command";
307  if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "offline only command";
308  if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
309 
310  cp->p1 = p->Recv_uint32();
311  cp->p2 = p->Recv_uint32();
312  cp->tile = p->Recv_uint32();
314 
315  byte callback = p->Recv_uint8();
316  if (callback >= lengthof(_callback_table)) return "invalid callback";
317 
318  cp->callback = _callback_table[callback];
319  return NULL;
320 }
321 
328 {
329  p->Send_uint8 (cp->company);
330  p->Send_uint32(cp->cmd);
331  p->Send_uint32(cp->p1);
332  p->Send_uint32(cp->p2);
333  p->Send_uint32(cp->tile);
334  p->Send_string(cp->text);
335 
336  byte callback = 0;
337  while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
338  callback++;
339  }
340 
341  if (callback == lengthof(_callback_table)) {
342  DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", cp->callback);
343  callback = 0; // _callback_table[0] == NULL
344  }
345  p->Send_uint8 (callback);
346 }
347 
348 #endif /* ENABLE_NETWORK */
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
"Send" a particular CommandPacket to all clients.
void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Callback function that is called after a sign is placed.
Definition: signs_cmd.cpp:118
static bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:45
Internal entity of a packet.
Definition: packet.h:44
A normal unpaused game.
Definition: openttd.h:58
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:250
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 CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
DoCommand callback function for all commands executed by Game Scripts.
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:152
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 CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Opens a &#39;Rename group&#39; window for newly created group.
Definition: group_gui.cpp:987
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:100
uint32 p2
parameter p2.
Definition: command_type.h:475
Client part of the network protocol.
uint count
The number of items in the queue.
Definition: tcp_game.h:136
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:121
void CcRoadStop(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Command callback for building road stops.
Definition: road_gui.cpp:199
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.
CommandFlags GetCommandFlags(uint32 cmd)
Definition: command.cpp:379
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including &#39;\0&#39;.
Definition: command_type.h:478
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:524
bool IsValidCommand(uint32 cmd)
Definition: command.cpp:365
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
DoCommand callback function for all commands executed by AIs.
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:133
NetworkSettings network
settings related to the network
void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Callback for building wagons.
Definition: train_gui.cpp:31
mask for all command flags
Definition: command_type.h:375
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
void CcCloneVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
This is the Callback method after the cloning attempt of a vehicle.
Definition: depot_gui.cpp:121
Server part of the network protocol.
execute the command without sending it on the network
Definition: command_type.h:374
uint32 p1
parameter p1.
Definition: command_type.h:474
void CcBuildRoadTunnel(const CommandCost &result, TileIndex start_tile, uint32 p1, uint32 p2)
Callback executed after a build road tunnel command has been called.
Definition: road_gui.cpp:96
void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Command callback.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
CommandPacket * last
The last packet in the queue; only valid when first != NULL.
Definition: tcp_game.h:135
static ClientNetworkGameSocketHandler * my_client
This is us!
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:134
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:527
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
PauseModeByte _pause_mode
The current pause mode.
Definition: gfx.cpp:48
#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:394
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition: command_type.h:477
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
bool IsCommandAllowedWhilePaused(uint32 cmd)
Returns whether the command is allowed while the game is paused.
Definition: command.cpp:405
void NetworkFreeLocalCommandQueue()
Free the local command queues.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2)
Callback executed after a build Bridge CMD has been called.
Definition: bridge_gui.cpp:61
uint32 _frame_counter
The current frame.
Definition: network.cpp:72
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
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:50
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
void CcBuildPrimaryVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
This is the Callback method after the construction attempt of a primary vehicle.
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:57
TileIndex tile
tile command being executed on.
Definition: command_type.h:473
void CcStartStopVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
This is the Callback method after attempting to start/stop a vehicle.
Everything we need to know about a command to be able to execute it.
Allow the special control codes.
Definition: string_type.h:52
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:467
the command cannot be executed in a multiplayer game; single-player only
Definition: command_type.h:387
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Open rename window after adding a vehicle to a new group via drag and drop.
Definition: group_gui.cpp:1003
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 CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Command callback for building a tunnel.
Definition: rail_gui.cpp:275
Owner
Enum for all companies/owners.
Definition: company_type.h:20
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
bool my_cmd
did the command originate from "me"
CommandPacket * next
the next command packet (if in queue)
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:71
uint32 cmd
command being executed.
Definition: command_type.h:476
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:290
Server part of the admin network protocol.