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/>.
10 /** @file network_command.cpp Command handling over network connections. */
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
[] = {
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
,
43 /* 0x11 */ CcTerraform
,
45 /* 0x13 */ CcCloneVehicle
,
46 /* 0x14 */ CcGiveMoney
,
47 /* 0x15 */ CcCreateGroup
,
48 /* 0x16 */ CcFoundRandomTown
,
49 /* 0x17 */ CcRoadStop
,
50 /* 0x18 */ CcBuildIndustry
,
51 /* 0x19 */ CcStartStopVehicle
,
53 /* 0x1B */ CcAddVehicleNewGroup
,
55 /* 0x1D */ CcSetVirtualTrain
,
56 /* 0x1E */ CcVirtualTrainWagonsMoved
,
57 /* 0x1F */ CcDeleteVirtualTrain
,
58 /* 0x20 */ CcAddVirtualEngine
,
62 * Append a CommandPacket at the end of the queue.
63 * @param p The packet to append to the queue.
64 * @note A new instance of the CommandPacket will be made.
66 void CommandQueue::Append(CommandPacket
*p
)
68 CommandPacket
*add
= MallocT
<CommandPacket
>(1);
71 if (this->first
== nullptr) {
74 this->last
->next
= add
;
81 * Return the first item in the queue and remove it from the queue.
82 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
83 * @return the first item in the queue.
85 CommandPacket
*CommandQueue::Pop(bool ignore_paused
)
87 CommandPacket
**prev
= &this->first
;
88 CommandPacket
*ret
= this->first
;
89 CommandPacket
*prev_item
= nullptr;
90 if (ignore_paused
&& _pause_mode
!= PM_UNPAUSED
) {
91 while (ret
!= nullptr && !IsCommandAllowedWhilePaused(ret
->cmd
)) {
98 if (ret
== this->last
) this->last
= prev_item
;
106 * Return the first item in the queue, but don't remove it.
107 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
108 * @return the first item in the queue.
110 CommandPacket
*CommandQueue::Peek(bool ignore_paused
)
112 if (!ignore_paused
|| _pause_mode
== PM_UNPAUSED
) return this->first
;
114 for (CommandPacket
*p
= this->first
; p
!= nullptr; p
= p
->next
) {
115 if (IsCommandAllowedWhilePaused(p
->cmd
)) return p
;
120 /** Free everything that is in the queue. */
121 void CommandQueue::Free()
124 while ((cp
= this->Pop()) != nullptr) {
127 assert(this->count
== 0);
130 /** Local queue of packets waiting for handling. */
131 static CommandQueue _local_wait_queue
;
132 /** Local queue of packets waiting for execution. */
133 static CommandQueue _local_execution_queue
;
136 * Prepare a DoCommand to be send over the network
137 * @param tile The tile to perform a command on (see #CommandProc)
138 * @param p1 Additional data for the command (see #CommandProc)
139 * @param p2 Additional data for the command (see #CommandProc)
140 * @param cmd The command to execute (a CMD_* value)
141 * @param callback A callback function to call after the command is finished
142 * @param text The text to pass
143 * @param company The company that wants to send the command
144 * @param binary_length The quantity of binary data in text
146 void NetworkSendCommand(TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
, CommandCallback
*callback
, const char *text
, CompanyID company
, uint32 binary_length
)
148 assert((cmd
& CMD_FLAGS_MASK
) == 0);
156 c
.callback
= callback
;
158 c
.binary_length
= binary_length
;
159 if (binary_length
== 0) {
160 strecpy(c
.text
, (text
!= nullptr) ? text
: "", lastof(c
.text
));
162 memcpy(c
.text
, text
, binary_length
);
165 if (_network_server
) {
166 /* If we are the server, we queue the command in our 'special' queue.
167 * In theory, we could execute the command right away, but then the
168 * client on the server can do everything 1 tick faster than others.
169 * So to keep the game fair, we delay the command with 1 tick
170 * which gives about the same speed as most clients.
172 c
.frame
= _frame_counter_max
+ 1;
175 _local_wait_queue
.Append(&c
);
179 c
.frame
= 0; // The client can't tell which frame, so just make it 0
181 /* Clients send their command to the server and forget all about the packet */
182 MyClient::SendCommand(&c
);
186 * Sync our local command queue to the command queue of the given
187 * socket. This is needed for the case where we receive a command
188 * before saving the game for a joining client, but without the
189 * execution of those commands. Not syncing those commands means
190 * that the client will never get them and as such will be in a
191 * desynced state from the time it started with joining.
192 * @param cs The client to sync the queue to.
194 void NetworkSyncCommandQueue(NetworkClientSocket
*cs
)
196 for (CommandPacket
*p
= _local_execution_queue
.Peek(); p
!= nullptr; p
= p
->next
) {
197 CommandPacket c
= *p
;
199 cs
->outgoing_queue
.Append(&c
);
204 * Execute all commands on the local command queue that ought to be executed this frame.
206 void NetworkExecuteLocalCommandQueue()
208 assert(IsLocalCompany());
210 CommandQueue
&queue
= (_network_server
? _local_execution_queue
: ClientNetworkGameSocketHandler::my_client
->incoming_queue
);
213 while ((cp
= queue
.Peek()) != nullptr) {
214 /* The queue is always in order, which means
215 * that the first element will be executed first. */
216 if (_frame_counter
< cp
->frame
) break;
218 if (_frame_counter
> cp
->frame
) {
219 /* If we reach here, it means for whatever reason, we've already executed
220 * past the command we need to execute. */
221 error("[net] Trying to execute a packet in the past!");
224 /* We can execute this command */
225 _current_company
= cp
->company
;
226 cp
->cmd
|= CMD_NETWORK_COMMAND
;
227 DoCommandP(cp
, cp
->my_cmd
);
233 /* Local company may have changed, so we should not restore the old value */
234 _current_company
= _local_company
;
238 * Free the local command queues.
240 void NetworkFreeLocalCommandQueue()
242 _local_wait_queue
.Free();
243 _local_execution_queue
.Free();
247 * "Send" a particular CommandPacket to all clients.
248 * @param cp The command that has to be distributed.
249 * @param owner The client that owns the command,
251 static void DistributeCommandPacket(CommandPacket
&cp
, const NetworkClientSocket
*owner
)
253 CommandCallback
*callback
= cp
.callback
;
254 cp
.frame
= _frame_counter_max
+ 1;
256 NetworkClientSocket
*cs
;
257 FOR_ALL_CLIENT_SOCKETS(cs
) {
258 if (cs
->status
>= NetworkClientSocket::STATUS_MAP
) {
259 /* Callbacks are only send back to the client who sent them in the
260 * first place. This filters that out. */
261 cp
.callback
= (cs
!= owner
) ? nullptr : callback
;
262 cp
.my_cmd
= (cs
== owner
);
263 cs
->outgoing_queue
.Append(&cp
);
267 cp
.callback
= (cs
!= owner
) ? nullptr : callback
;
268 cp
.my_cmd
= (cs
== owner
);
269 _local_execution_queue
.Append(&cp
);
273 * "Send" a particular CommandQueue to all clients.
274 * @param queue The queue of commands that has to be distributed.
275 * @param owner The client that owns the commands,
277 static void DistributeQueue(CommandQueue
*queue
, const NetworkClientSocket
*owner
)
279 #ifdef DEBUG_DUMP_COMMANDS
280 /* When replaying we do not want this limitation. */
281 int to_go
= UINT16_MAX
;
283 int to_go
= _settings_client
.network
.commands_per_frame
;
287 while (--to_go
>= 0 && (cp
= queue
->Pop(true)) != nullptr) {
288 DistributeCommandPacket(*cp
, owner
);
289 NetworkAdminCmdLogging(owner
, cp
);
294 /** Distribute the commands of ourself and the clients. */
295 void NetworkDistributeCommands()
297 /* First send the server's commands. */
298 DistributeQueue(&_local_wait_queue
, nullptr);
300 /* Then send the queues of the others. */
301 NetworkClientSocket
*cs
;
302 FOR_ALL_CLIENT_SOCKETS(cs
) {
303 DistributeQueue(&cs
->incoming_queue
, cs
);
308 * Receives a command from the network.
309 * @param p the packet to read from.
310 * @param cp the struct to write the data to.
311 * @return an error message. When nullptr there has been no error.
313 const char *NetworkGameSocketHandler::ReceiveCommand(Packet
*p
, CommandPacket
*cp
)
315 cp
->company
= (CompanyID
)p
->Recv_uint8();
316 cp
->cmd
= p
->Recv_uint32();
317 if (!IsValidCommand(cp
->cmd
)) return "invalid command";
318 if (GetCommandFlags(cp
->cmd
) & CMD_OFFLINE
) return "offline only command";
319 if ((cp
->cmd
& CMD_FLAGS_MASK
) != 0) return "invalid command flag";
321 cp
->p1
= p
->Recv_uint32();
322 cp
->p2
= p
->Recv_uint32();
323 cp
->tile
= p
->Recv_uint32();
324 cp
->binary_length
= p
->Recv_uint32();
325 if (cp
->binary_length
== 0) {
326 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
);
328 p
->Recv_binary(cp
->text
, cp
->binary_length
);
331 byte callback
= p
->Recv_uint8();
332 if (callback
>= lengthof(_callback_table
)) return "invalid callback";
334 cp
->callback
= _callback_table
[callback
];
339 * Sends a command over the network.
340 * @param p the packet to send it in.
341 * @param cp the packet to actually send.
343 void NetworkGameSocketHandler::SendCommand(Packet
*p
, const CommandPacket
*cp
)
345 p
->Send_uint8 (cp
->company
);
346 p
->Send_uint32(cp
->cmd
);
347 p
->Send_uint32(cp
->p1
);
348 p
->Send_uint32(cp
->p2
);
349 p
->Send_uint32(cp
->tile
);
350 p
->Send_uint32(cp
->binary_length
);
351 if (cp
->binary_length
== 0) {
352 p
->Send_string(cp
->text
);
354 p
->Send_binary(cp
->text
, cp
->binary_length
);
358 while (callback
< lengthof(_callback_table
) && _callback_table
[callback
] != cp
->callback
) {
362 if (callback
== lengthof(_callback_table
)) {
363 DEBUG(net
, 0, "Unknown callback. (Pointer: %p) No callback sent", cp
->callback
);
364 callback
= 0; // _callback_table[0] == nullptr
366 p
->Send_uint8 (callback
);
369 #endif /* ENABLE_NETWORK */