(svn r27953) -Cleanup: Adjust other languages for r27952
[openttd.git] / src / network / network_command.cpp
blobbc08bc5f14440499c24082ca1253f8471c91f257
1 /* $Id$ */
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 */
10 /** @file network_command.cpp Command handling over network connections. */
12 #ifdef ENABLE_NETWORK
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"
22 #include "../safeguards.h"
24 /** Table with all the callbacks we'll use for conversion*/
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,
56 /**
57 * Append a CommandPacket at the end of the queue.
58 * @param p The packet to append to the queue.
59 * @note A new instance of the CommandPacket will be made.
61 void CommandQueue::Append(CommandPacket *p)
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;
71 this->last = add;
72 this->count++;
75 /**
76 * Return the first item in the queue and remove it from the queue.
77 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
78 * @return the first item in the queue.
80 CommandPacket *CommandQueue::Pop(bool ignore_paused)
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;
92 if (ret != NULL) {
93 if (ret == this->last) this->last = prev_item;
94 *prev = ret->next;
95 this->count--;
97 return ret;
101 * Return the first item in the queue, but don't remove it.
102 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
103 * @return the first item in the queue.
105 CommandPacket *CommandQueue::Peek(bool ignore_paused)
107 if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
109 for (CommandPacket *p = this->first; p != NULL; p = p->next) {
110 if (IsCommandAllowedWhilePaused(p->cmd)) return p;
112 return NULL;
115 /** Free everything that is in the queue. */
116 void CommandQueue::Free()
118 CommandPacket *cp;
119 while ((cp = this->Pop()) != NULL) {
120 free(cp);
122 assert(this->count == 0);
125 /** Local queue of packets waiting for handling. */
126 static CommandQueue _local_wait_queue;
127 /** Local queue of packets waiting for execution. */
128 static CommandQueue _local_execution_queue;
131 * Prepare a DoCommand to be send over the network
132 * @param tile The tile to perform a command on (see #CommandProc)
133 * @param p1 Additional data for the command (see #CommandProc)
134 * @param p2 Additional data for the command (see #CommandProc)
135 * @param cmd The command to execute (a CMD_* value)
136 * @param callback A callback function to call after the command is finished
137 * @param text The text to pass
138 * @param company The company that wants to send the command
140 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
142 assert((cmd & CMD_FLAGS_MASK) == 0);
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;
152 strecpy(c.text, (text != NULL) ? text : "", lastof(c.text));
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.
161 c.frame = _frame_counter_max + 1;
162 c.my_cmd = true;
164 _local_wait_queue.Append(&c);
165 return;
168 c.frame = 0; // The client can't tell which frame, so just make it 0
170 /* Clients send their command to the server and forget all about the packet */
171 MyClient::SendCommand(&c);
175 * Sync our local command queue to the command queue of the given
176 * socket. This is needed for the case where we receive a command
177 * before saving the game for a joining client, but without the
178 * execution of those commands. Not syncing those commands means
179 * that the client will never get them and as such will be in a
180 * desynced state from the time it started with joining.
181 * @param cs The client to sync the queue to.
183 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
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);
193 * Execute all commands on the local command queue that ought to be executed this frame.
195 void NetworkExecuteLocalCommandQueue()
197 assert(IsLocalCompany());
199 CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
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;
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!");
213 /* We can execute this command */
214 _current_company = cp->company;
215 cp->cmd |= CMD_NETWORK_COMMAND;
216 DoCommandP(cp, cp->my_cmd);
218 queue.Pop();
219 free(cp);
222 /* Local company may have changed, so we should not restore the old value */
223 _current_company = _local_company;
227 * Free the local command queues.
229 void NetworkFreeLocalCommandQueue()
231 _local_wait_queue.Free();
232 _local_execution_queue.Free();
236 * "Send" a particular CommandPacket to all clients.
237 * @param cp The command that has to be distributed.
238 * @param owner The client that owns the command,
240 static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
242 CommandCallback *callback = cp.callback;
243 cp.frame = _frame_counter_max + 1;
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);
256 cp.callback = (cs != owner) ? NULL : callback;
257 cp.my_cmd = (cs == owner);
258 _local_execution_queue.Append(&cp);
262 * "Send" a particular CommandQueue to all clients.
263 * @param queue The queue of commands that has to be distributed.
264 * @param owner The client that owns the commands,
266 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
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
275 CommandPacket *cp;
276 while (--to_go >= 0 && (cp = queue->Pop(true)) != NULL) {
277 DistributeCommandPacket(*cp, owner);
278 NetworkAdminCmdLogging(owner, cp);
279 free(cp);
283 /** Distribute the commands of ourself and the clients. */
284 void NetworkDistributeCommands()
286 /* First send the server's commands. */
287 DistributeQueue(&_local_wait_queue, NULL);
289 /* Then send the queues of the others. */
290 NetworkClientSocket *cs;
291 FOR_ALL_CLIENT_SOCKETS(cs) {
292 DistributeQueue(&cs->incoming_queue, cs);
297 * Receives a command from the network.
298 * @param p the packet to read from.
299 * @param cp the struct to write the data to.
300 * @return an error message. When NULL there has been no error.
302 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
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";
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);
315 byte callback = p->Recv_uint8();
316 if (callback >= lengthof(_callback_table)) return "invalid callback";
318 cp->callback = _callback_table[callback];
319 return NULL;
323 * Sends a command over the network.
324 * @param p the packet to send it in.
325 * @param cp the packet to actually send.
327 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
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);
336 byte callback = 0;
337 while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
338 callback++;
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
345 p->Send_uint8 (callback);
348 #endif /* ENABLE_NETWORK */