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 
8 /** @file network_command.cpp Command handling over network connections. */
9 
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 
20 /** Table with all the callbacks we'll use for conversion*/
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 
52 /**
53  * Append a CommandPacket at the end of the queue.
54  * @param p The packet to append to the queue.
55  * @note A new instance of the CommandPacket will be made.
56  */
Append(CommandPacket * p)57 void CommandQueue::Append(CommandPacket *p)
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 
71 /**
72  * Return the first item in the queue and remove it from the queue.
73  * @param ignore_paused Whether to ignore commands that may not be executed while paused.
74  * @return the first item in the queue.
75  */
Pop(bool ignore_paused)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 
96 /**
97  * Return the first item in the queue, but don't remove it.
98  * @param ignore_paused Whether to ignore commands that may not be executed while paused.
99  * @return the first item in the queue.
100  */
Peek(bool ignore_paused)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 
111 /** Free everything that is in the queue. */
Free()112 void CommandQueue::Free()
113 {
114 	CommandPacket *cp;
115 	while ((cp = this->Pop()) != nullptr) {
116 		delete cp;
117 	}
118 	assert(this->count == 0);
119 }
120 
121 /** Local queue of packets waiting for handling. */
122 static CommandQueue _local_wait_queue;
123 /** Local queue of packets waiting for execution. */
124 static CommandQueue _local_execution_queue;
125 
126 /**
127  * Prepare a DoCommand to be send over the network
128  * @param tile The tile to perform a command on (see #CommandProc)
129  * @param p1 Additional data for the command (see #CommandProc)
130  * @param p2 Additional data for the command (see #CommandProc)
131  * @param cmd The command to execute (a CMD_* value)
132  * @param callback A callback function to call after the command is finished
133  * @param text The text to pass
134  * @param company The company that wants to send the command
135  */
NetworkSendCommand(TileIndex tile,uint32 p1,uint32 p2,uint32 cmd,CommandCallback * callback,const std::string & text,CompanyID company)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 
159 		_local_wait_queue.Append(&c);
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 */
166 	MyClient::SendCommand(&c);
167 }
168 
169 /**
170  * Sync our local command queue to the command queue of the given
171  * socket. This is needed for the case where we receive a command
172  * before saving the game for a joining client, but without the
173  * execution of those commands. Not syncing those commands means
174  * that the client will never get them and as such will be in a
175  * desynced state from the time it started with joining.
176  * @param cs The client to sync the queue to.
177  */
NetworkSyncCommandQueue(NetworkClientSocket * cs)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 
187 /**
188  * Execute all commands on the local command queue that ought to be executed this frame.
189  */
NetworkExecuteLocalCommandQueue()190 void NetworkExecuteLocalCommandQueue()
191 {
192 	assert(IsLocalCompany());
193 
194 	CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
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 */
209 		_current_company = cp->company;
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 */
218 	_current_company = _local_company;
219 }
220 
221 /**
222  * Free the local command queues.
223  */
NetworkFreeLocalCommandQueue()224 void NetworkFreeLocalCommandQueue()
225 {
226 	_local_wait_queue.Free();
227 	_local_execution_queue.Free();
228 }
229 
230 /**
231  * "Send" a particular CommandPacket to all clients.
232  * @param cp    The command that has to be distributed.
233  * @param owner The client that owns the command,
234  */
DistributeCommandPacket(CommandPacket & cp,const NetworkClientSocket * owner)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);
252 	_local_execution_queue.Append(&cp);
253 }
254 
255 /**
256  * "Send" a particular CommandQueue to all clients.
257  * @param queue The queue of commands that has to be distributed.
258  * @param owner The client that owns the commands,
259  */
DistributeQueue(CommandQueue * queue,const NetworkClientSocket * owner)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
266 	int to_go = _settings_client.network.commands_per_frame;
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 
277 /** Distribute the commands of ourself and the clients. */
NetworkDistributeCommands()278 void NetworkDistributeCommands()
279 {
280 	/* First send the server's commands. */
281 	DistributeQueue(&_local_wait_queue, nullptr);
282 
283 	/* Then send the queues of the others. */
284 	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
285 		DistributeQueue(&cs->incoming_queue, cs);
286 	}
287 }
288 
289 /**
290  * Receives a command from the network.
291  * @param p the packet to read from.
292  * @param cp the struct to write the data to.
293  * @return an error message. When nullptr there has been no error.
294  */
ReceiveCommand(Packet * p,CommandPacket * cp)295 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
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();
306 	cp->text    = p->Recv_string(NETWORK_COMPANY_NAME_LENGTH, (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
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 
315 /**
316  * Sends a command over the network.
317  * @param p the packet to send it in.
318  * @param cp the packet to actually send.
319  */
SendCommand(Packet * p,const CommandPacket * cp)320 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
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 }
340