OpenTTD Source: src/network/network_command.cpp Source File

OpenTTD Source 1.11.0-RC1

network_command.cpp
Go to the documentation of this file.
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
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
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
57 void CommandQueue::Append(CommandPacket *p)
58 {
59 CommandPacket *add = MallocT<CommandPacket>(1);
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
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
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
112 void CommandQueue::Free()
113 {
114 CommandPacket *cp;
115 while ((cp = this->Pop()) != nullptr) {
116 free(cp);
117 }
118 assert(this->count == 0);
119 }
120
122 static CommandQueue _local_wait_queue;
124 static CommandQueue _local_execution_queue;
125
136 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *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
148 strecpy(c.text, (text != nullptr) ? text : "", lastof(c.text));
149
150 if (_network_server) {
151 /* If we are the server, we queue the command in our 'special' queue.
152 * In theory, we could execute the command right away, but then the
153 * client on the server can do everything 1 tick faster than others.
154 * So to keep the game fair, we delay the command with 1 tick
155 * which gives about the same speed as most clients.
156 */
157 c.frame = _frame_counter_max + 1;
158 c.my_cmd = true;
159
160 _local_wait_queue.Append(&c);
161 return;
162 }
163
164 c.frame = 0; // The client can't tell which frame, so just make it 0
165
166 /* Clients send their command to the server and forget all about the packet */
167 MyClient::SendCommand(&c);
168 }
169
179 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
180 {
181 for (CommandPacket *p = _local_execution_queue.Peek(); p != nullptr; p = p->next) {
182 CommandPacket c = *p;
183 c.callback = 0;
184 cs->outgoing_queue.Append(&c);
185 }
186 }
187
191 void NetworkExecuteLocalCommandQueue()
192 {
193 assert(IsLocalCompany());
194
195 CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
196
197 CommandPacket *cp;
198 while ((cp = queue.Peek()) != nullptr) {
199 /* The queue is always in order, which means
200 * that the first element will be executed first. */
201 if (_frame_counter < cp->frame) break;
202
203 if (_frame_counter > cp->frame) {
204 /* If we reach here, it means for whatever reason, we've already executed
205 * past the command we need to execute. */
206 error("[net] Trying to execute a packet in the past!");
207 }
208
209 /* We can execute this command */
210 _current_company = cp->company;
211 cp->cmd |= CMD_NETWORK_COMMAND;
212 DoCommandP(cp, cp->my_cmd);
213
214 queue.Pop();
215 free(cp);
216 }
217
218 /* Local company may have changed, so we should not restore the old value */
219 _current_company = _local_company;
220 }
221
225 void NetworkFreeLocalCommandQueue()
226 {
227 _local_wait_queue.Free();
228 _local_execution_queue.Free();
229 }
230
236 static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
237 {
238 CommandCallback *callback = cp.callback;
239 cp.frame = _frame_counter_max + 1;
240
241 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
242 if (cs->status >= NetworkClientSocket::STATUS_MAP) {
243 /* Callbacks are only send back to the client who sent them in the
244 * first place. This filters that out. */
245 cp.callback = (cs != owner) ? nullptr : callback;
246 cp.my_cmd = (cs == owner);
247 cs->outgoing_queue.Append(&cp);
248 }
249 }
250
251 cp.callback = (nullptr != owner) ? nullptr : callback;
252 cp.my_cmd = (nullptr == owner);
253 _local_execution_queue.Append(&cp);
254 }
255
261 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
262 {
263 #ifdef DEBUG_DUMP_COMMANDS
264 /* When replaying we do not want this limitation. */
265 int to_go = UINT16_MAX;
266 #else
267 int to_go = _settings_client.network.commands_per_frame;
268 #endif
269
270 CommandPacket *cp;
271 while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
272 DistributeCommandPacket(*cp, owner);
273 NetworkAdminCmdLogging(owner, cp);
274 free(cp);
275 }
276 }
277
279 void NetworkDistributeCommands()
280 {
281 /* First send the server's commands. */
282 DistributeQueue(&_local_wait_queue, nullptr);
283
284 /* Then send the queues of the others. */
285 for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
286 DistributeQueue(&cs->incoming_queue, cs);
287 }
288 }
289
296 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
297 {
298 cp->company = (CompanyID)p->Recv_uint8();
299 cp->cmd = p->Recv_uint32();
300 if (!IsValidCommand(cp->cmd)) return "invalid command";
301 if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
302 if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
303
304 cp->p1 = p->Recv_uint32();
305 cp->p2 = p->Recv_uint32();
306 cp->tile = p->Recv_uint32();
307 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);
308
309 byte callback = p->Recv_uint8();
310 if (callback >= lengthof(_callback_table)) return "invalid callback";
311
312 cp->callback = _callback_table[callback];
313 return nullptr;
314 }
315
321 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
322 {
323 p->Send_uint8 (cp->company);
324 p->Send_uint32(cp->cmd);
325 p->Send_uint32(cp->p1);
326 p->Send_uint32(cp->p2);
327 p->Send_uint32(cp->tile);
328 p->Send_string(cp->text);
329
330 byte callback = 0;
331 while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
332 callback++;
333 }
334
335 if (callback == lengthof(_callback_table)) {
336 DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", cp->callback);
337 callback = 0; // _callback_table[0] == nullptr
338 }
339 p->Send_uint8 (callback);
340 }
CommandQueue::last
CommandPacket * last
The last packet in the queue; only valid when first != nullptr.
Definition: tcp_game.h:132
NetworkSendCommand
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.
Definition: network_command.cpp:136
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
CommandContainer::cmd
uint32 cmd
command being executed.
Definition: command_type.h:483
_frame_counter
uint32 _frame_counter
The current frame.
Definition: network.cpp:68
_local_wait_queue
static CommandQueue _local_wait_queue
Local queue of packets waiting for handling.
Definition: network_command.cpp:122
Packet::Recv_string
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.
Definition: packet.cpp:286
CcBuildPrimaryVehicle
void CcBuildPrimaryVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
This is the Callback method after the construction attempt of a primary vehicle.
Definition: vehicle_gui.cpp:3082
CMD_OFFLINE
@ CMD_OFFLINE
the command cannot be executed in a multiplayer game; single-player only
Definition: command_type.h:393
_callback_table
static CommandCallback *const _callback_table[]
Table with all the callbacks we'll use for conversion.
Definition: network_command.cpp:21
_network_server
bool _network_server
network-server is active
Definition: network.cpp:53
CommandPacket::frame
uint32 frame
the frame in which this packet is executed
Definition: network_internal.h:154
_local_execution_queue
static CommandQueue _local_execution_queue
Local queue of packets waiting for execution.
Definition: network_command.cpp:124
CcRoadStop
void CcRoadStop(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback for building road stops.
Definition: road_gui.cpp:158
CcStartStopVehicle
void CcStartStopVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
This is the Callback method after attempting to start/stop a vehicle.
Definition: vehicle_gui.cpp:2582
CommandCallback
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:474
CommandContainer::p2
uint32 p2
parameter p2.
Definition: command_type.h:482
CommandContainer::text
char text[32 *MAX_CHAR_LENGTH]
possible text sent for name changes etc, in bytes including '\0'.
Definition: command_type.h:485
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
NetworkSyncCommandQueue
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
Sync our local command queue to the command queue of the given socket.
Definition: network_command.cpp:179
ClientNetworkGameSocketHandler::my_client
static ClientNetworkGameSocketHandler * my_client
This is us!
Definition: network_client.h:41
CommandPacket::next
CommandPacket * next
the next command packet (if in queue)
Definition: network_internal.h:152
DistributeCommandPacket
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
"Send" a particular CommandPacket to all clients.
Definition: network_command.cpp:236
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
IsCommandAllowedWhilePaused
bool IsCommandAllowedWhilePaused(uint32 cmd)
Returns whether the command is allowed while the game is paused.
Definition: command.cpp:419
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:96
IsLocalCompany
static bool IsLocalCompany()
Is the current company the local company?
Definition: company_func.h:43
_frame_counter_max
uint32 _frame_counter_max
To where we may go with our clients.
Definition: network.cpp:67
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:117
CcBuildBridge
void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2, uint32 cmd)
Callback executed after a build Bridge CMD has been called.
Definition: bridge_gui.cpp:61
CcAddVehicleNewGroup
void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Open rename window after adding a vehicle to a new group via drag and drop.
Definition: group_gui.cpp:1159
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:59
CommandQueue::Append
void Append(CommandPacket *p)
Append a CommandPacket at the end of the queue.
Definition: network_command.cpp:57
IsValidCommand
bool IsValidCommand(uint32 cmd)
Definition: command.cpp:379
CommandPacket::company
CompanyID company
company that is executing the command
Definition: network_internal.h:153
CcCreateGroup
void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Opens a 'Rename group' window for newly created group.
Definition: group_gui.cpp:1142
SVS_ALLOW_CONTROL_CODE
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:52
CcBuildIndustry
void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback.
Definition: industry_gui.cpp:226
CommandContainer::p1
uint32 p1
parameter p1.
Definition: command_type.h:481
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:246
CommandPacket
Everything we need to know about a command to be able to execute it.
Definition: network_internal.h:149
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
CcPlaceSign
void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Callback function that is called after a sign is placed.
Definition: signs_cmd.cpp:117
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
Packet::Send_string
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:148
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
CommandQueue
A queue of CommandPackets.
Definition: tcp_game.h:130
CcGame
void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by Game Scripts.
Definition: game_instance.cpp:90
network_client.h
CommandPacket::my_cmd
bool my_cmd
did the command originate from "me"
Definition: network_internal.h:155
CMD_FLAGS_MASK
@ CMD_FLAGS_MASK
mask for all command flags
Definition: command_type.h:381
network_server.h
Packet
Internal entity of a packet.
Definition: packet.h:40
NetworkGameSocketHandler::ReceiveCommand
const char * ReceiveCommand(Packet *p, CommandPacket *cp)
Receives a command from the network.
Definition: network_command.cpp:296
GetCommandFlags
CommandFlags GetCommandFlags(uint32 cmd)
Definition: command.cpp:393
CcBuildWagon
void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Callback for building wagons.
Definition: train_gui.cpp:30
CommandContainer::callback
CommandCallback * callback
any callback function executed upon successful completion of the command.
Definition: command_type.h:484
CommandQueue::first
CommandPacket * first
The first packet in the queue.
Definition: tcp_game.h:131
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
CMD_NETWORK_COMMAND
@ CMD_NETWORK_COMMAND
execute the command without sending it on the network
Definition: command_type.h:380
ClientNetworkGameSocketHandler::SendCommand
static NetworkRecvStatus SendCommand(const CommandPacket *cp)
Send a command to the server.
Definition: network_client.cpp:437
CommandQueue::Free
void Free()
Free everything that is in the queue.
Definition: network_command.cpp:112
CommandQueue::count
uint count
The number of items in the queue.
Definition: tcp_game.h:133
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:217
CcBuildRailTunnel
void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Command callback for building a tunnel.
Definition: rail_gui.cpp:278
NetworkGameSocketHandler::incoming_queue
CommandQueue incoming_queue
The command-queue awaiting handling.
Definition: tcp_game.h:521
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:131
NetworkSettings::commands_per_frame
uint16 commands_per_frame
how many commands may be sent each frame_freq frames?
Definition: settings_type.h:252
CommandQueue::Peek
CommandPacket * Peek(bool ignore_paused=false)
Return the first item in the queue, but don't remove it.
Definition: network_command.cpp:101
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:578
CcCloneVehicle
void CcCloneVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
This is the Callback method after the cloning attempt of a vehicle.
Definition: depot_gui.cpp:120
NetworkGameSocketHandler::SendCommand
void SendCommand(Packet *p, const CommandPacket *cp)
Sends a command over the network.
Definition: network_command.cpp:321
CcAI
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by AIs.
Definition: ai_instance.cpp:101
NetworkExecuteLocalCommandQueue
void NetworkExecuteLocalCommandQueue()
Execute all commands on the local command queue that ought to be executed this frame.
Definition: network_command.cpp:191
NetworkAdminCmdLogging
void NetworkAdminCmdLogging(const NetworkClientSocket *owner, const CommandPacket *cp)
Distribute CommandPacket details over the admin network for logging purposes.
Definition: network_admin.cpp:975
network_admin.h
CommandContainer::tile
TileIndex tile
tile command being executed on.
Definition: command_type.h:480
CMD_STR_CTRL
@ CMD_STR_CTRL
the command's string may contain control strings
Definition: command_type.h:400
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
NetworkFreeLocalCommandQueue
void NetworkFreeLocalCommandQueue()
Free the local command queues.
Definition: network_command.cpp:225
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
CcBuildRoadTunnel
void CcBuildRoadTunnel(const CommandCost &result, TileIndex start_tile, uint32 p1, uint32 p2, uint32 cmd)
Callback executed after a build road tunnel command has been called.
Definition: road_gui.cpp:99
NetworkDistributeCommands
void NetworkDistributeCommands()
Distribute the commands of ourself and the clients.
Definition: network_command.cpp:279
DistributeQueue
static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
"Send" a particular CommandQueue to all clients.
Definition: network_command.cpp:261
CommandQueue::Pop
CommandPacket * Pop(bool ignore_paused=false)
Return the first item in the queue and remove it from the queue.
Definition: network_command.cpp:76