OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network_command.cpp
Go to the documentation of this file.
1 /* $Id: network_command.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
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 */ CcBuildCanal,
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 */ CcPlaySound10,
40 /* 0x0E */ CcPlaySound1D,
41 /* 0x0F */ CcPlaySound1E,
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
61 void CommandQueue::Append(CommandPacket *p)
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
116 void CommandQueue::Free()
117 {
118 CommandPacket *cp;
119 while ((cp = this->Pop()) != NULL) {
120 free(cp);
121 }
122 assert(this->count == 0);
123 }
124
126 static CommandQueue _local_wait_queue;
128 static CommandQueue _local_execution_queue;
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 */
171 MyClient::SendCommand(&c);
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
195 void NetworkExecuteLocalCommandQueue()
196 {
197 assert(IsLocalCompany());
198
199 CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
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 */
214 _current_company = cp->company;
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 */
223 _current_company = _local_company;
224 }
225
229 void NetworkFreeLocalCommandQueue()
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;
246 FOR_ALL_CLIENT_SOCKETS(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
272 int to_go = _settings_client.network.commands_per_frame;
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
284 void NetworkDistributeCommands()
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;
291 FOR_ALL_CLIENT_SOCKETS(cs) {
292 DistributeQueue(&cs->incoming_queue, cs);
293 }
294 }
295
302 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
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();
313 p->Recv_string(cp->text, lengthof(cp->text), (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
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
327 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
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 */