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/>.
9 * @file tcp_game.h Basic functions to receive and send TCP packets for game purposes.
12 #ifndef NETWORK_CORE_TCP_GAME_H
13 #define NETWORK_CORE_TCP_GAME_H
15 #include "os_abstraction.h"
17 #include "../network_type.h"
18 #include "../../core/pool_type.hpp"
22 * Enum with all types of TCP packets.
23 * For the exact meaning, look at #NetworkGameSocketHandler.
27 * These first three pair of packets (thus six in
28 * total) must remain in this order for backward
29 * and forward compatibility between clients that
30 * are trying to join directly.
33 /* Packets sent by socket accepting code without ever constructing a client socket instance. */
34 PACKET_SERVER_FULL
, ///< The server is full and has no place for you.
35 PACKET_SERVER_BANNED
, ///< The server has banned you.
37 /* Packets used by the client to join and an error message when the revision is wrong. */
38 PACKET_CLIENT_JOIN
, ///< The client telling the server it wants to join.
39 PACKET_SERVER_ERROR
, ///< Server sending an error message to the client.
41 /* Packets used for the pre-game lobby. */
42 PACKET_CLIENT_COMPANY_INFO
, ///< Request information about all companies.
43 PACKET_SERVER_COMPANY_INFO
, ///< Information about a single company.
46 * Packets after here assume that the client
47 * and server are running the same version. As
48 * such ordering is unimportant from here on.
50 * The following is the remainder of the packets
51 * sent as part of authenticating and getting
52 * the map and other important data.
55 /* After the join step, the first is checking NewGRFs. */
56 PACKET_SERVER_CHECK_NEWGRFS
, ///< Server sends NewGRF IDs and MD5 checksums for the client to check.
57 PACKET_CLIENT_NEWGRFS_CHECKED
, ///< Client acknowledges that it has all required NewGRFs.
59 /* Checking the game, and then company passwords. */
60 PACKET_SERVER_NEED_GAME_PASSWORD
, ///< Server requests the (hashed) game password.
61 PACKET_CLIENT_GAME_PASSWORD
, ///< Clients sends the (hashed) game password.
62 PACKET_SERVER_NEED_COMPANY_PASSWORD
, ///< Server requests the (hashed) company password.
63 PACKET_CLIENT_COMPANY_PASSWORD
, ///< Client sends the (hashed) company password.
65 /* The server welcomes the authenticated client and sends information of other clients. */
66 PACKET_SERVER_WELCOME
, ///< Server welcomes you and gives you your #ClientID.
67 PACKET_SERVER_CLIENT_INFO
, ///< Server sends you information about a client.
69 /* Getting the savegame/map. */
70 PACKET_CLIENT_GETMAP
, ///< Client requests the actual map.
71 PACKET_SERVER_WAIT
, ///< Server tells the client there are some people waiting for the map as well.
72 PACKET_SERVER_MAP_BEGIN
, ///< Server tells the client that it is beginning to send the map.
73 PACKET_SERVER_MAP_SIZE
, ///< Server tells the client what the (compressed) size of the map is.
74 PACKET_SERVER_MAP_DATA
, ///< Server sends bits of the map to the client.
75 PACKET_SERVER_MAP_DONE
, ///< Server tells it has just sent the last bits of the map to the client.
76 PACKET_CLIENT_MAP_OK
, ///< Client tells the server that it received the whole map.
78 PACKET_SERVER_JOIN
, ///< Tells clients that a new client has joined.
81 * At this moment the client has the map and
82 * the client is fully authenticated. Now the
83 * normal communication starts.
86 /* Game progress monitoring. */
87 PACKET_SERVER_FRAME
, ///< Server tells the client what frame it is in, and thus to where the client may progress.
88 PACKET_CLIENT_ACK
, ///< The client tells the server which frame it has executed.
89 PACKET_SERVER_SYNC
, ///< Server tells the client what the random state should be.
91 /* Sending commands around. */
92 PACKET_CLIENT_COMMAND
, ///< Client executed a command and sends it to the server.
93 PACKET_SERVER_COMMAND
, ///< Server distributes a command to (all) the clients.
95 /* Human communication! */
96 PACKET_CLIENT_CHAT
, ///< Client said something that should be distributed.
97 PACKET_SERVER_CHAT
, ///< Server distributing the message of a client (or itself).
100 PACKET_CLIENT_RCON
, ///< Client asks the server to execute some command.
101 PACKET_SERVER_RCON
, ///< Response of the executed command on the server.
103 /* Moving a client.*/
104 PACKET_CLIENT_MOVE
, ///< A client would like to be moved to another company.
105 PACKET_SERVER_MOVE
, ///< Server tells everyone that someone is moved to another company.
107 /* Configuration updates. */
108 PACKET_CLIENT_SET_PASSWORD
, ///< A client (re)sets its company's password.
109 PACKET_CLIENT_SET_NAME
, ///< A client changes its name.
110 PACKET_SERVER_COMPANY_UPDATE
, ///< Information (password) of a company changed.
111 PACKET_SERVER_CONFIG_UPDATE
, ///< Some network configuration important to the client changed.
113 /* A server quitting this game. */
114 PACKET_SERVER_NEWGAME
, ///< The server is preparing to start a new game.
115 PACKET_SERVER_SHUTDOWN
, ///< The server is shutting down.
117 /* A client quitting. */
118 PACKET_CLIENT_QUIT
, ///< A client tells the server it is going to quit.
119 PACKET_SERVER_QUIT
, ///< A server tells that a client has quit.
120 PACKET_CLIENT_ERROR
, ///< A client reports an error to the server.
121 PACKET_SERVER_ERROR_QUIT
, ///< A server tells that a client has hit an error and did quit.
123 PACKET_END
, ///< Must ALWAYS be on the end of this list!! (period)
126 /** Packet that wraps a command */
127 struct CommandPacket
;
129 /** A queue of CommandPackets. */
131 CommandPacket
*first
; ///< The first packet in the queue.
132 CommandPacket
*last
; ///< The last packet in the queue; only valid when first != nullptr.
133 uint count
; ///< The number of items in the queue.
136 /** Initialise the command queue. */
137 CommandQueue() : first(nullptr), last(nullptr), count(0) {}
138 /** Clear the command queue. */
139 ~CommandQueue() { this->Free(); }
140 void Append(CommandPacket
*p
);
141 CommandPacket
*Pop(bool ignore_paused
= false);
142 CommandPacket
*Peek(bool ignore_paused
= false);
144 /** Get the number of items in the queue. */
145 uint
Count() const { return this->count
; }
148 /** Base socket handler for all TCP sockets */
149 class NetworkGameSocketHandler
: public NetworkTCPSocketHandler
{
150 /* TODO: rewrite into a proper class */
152 NetworkClientInfo
*info
; ///< Client info related to this socket
155 NetworkRecvStatus
ReceiveInvalidPacket(PacketGameType type
);
158 * Notification that the server is full.
159 * @param p The packet that was just received.
161 virtual NetworkRecvStatus
Receive_SERVER_FULL(Packet
*p
);
164 * Notification that the client trying to join is banned.
165 * @param p The packet that was just received.
167 virtual NetworkRecvStatus
Receive_SERVER_BANNED(Packet
*p
);
170 * Try to join the server:
171 * string OpenTTD revision (norev000 if no revision).
172 * string Name of the client (max NETWORK_NAME_LENGTH).
173 * uint8 ID of the company to play as (1..MAX_COMPANIES).
174 * uint8 ID of the clients Language.
175 * @param p The packet that was just received.
177 virtual NetworkRecvStatus
Receive_CLIENT_JOIN(Packet
*p
);
180 * The client made an error:
181 * uint8 Error code caused (see NetworkErrorCode).
182 * @param p The packet that was just received.
184 virtual NetworkRecvStatus
Receive_SERVER_ERROR(Packet
*p
);
187 * Request company information (in detail).
188 * @param p The packet that was just received.
190 virtual NetworkRecvStatus
Receive_CLIENT_COMPANY_INFO(Packet
*p
);
193 * Sends information about the companies (one packet per company):
194 * uint8 Version of the structure of this packet (NETWORK_COMPANY_INFO_VERSION).
195 * bool Contains data (false marks the end of updates).
196 * uint8 ID of the company.
197 * string Name of the company.
198 * uint32 Year the company was inaugurated.
202 * uint16 Performance (last quarter).
203 * bool Company is password protected.
204 * uint16 Number of trains.
205 * uint16 Number of lorries.
206 * uint16 Number of busses.
207 * uint16 Number of planes.
208 * uint16 Number of ships.
209 * uint16 Number of train stations.
210 * uint16 Number of lorry stations.
211 * uint16 Number of bus stops.
212 * uint16 Number of airports and heliports.
213 * uint16 Number of harbours.
214 * bool Company is an AI.
215 * string Client names (comma separated list)
216 * @param p The packet that was just received.
218 virtual NetworkRecvStatus
Receive_SERVER_COMPANY_INFO(Packet
*p
);
221 * Send information about a client:
222 * uint32 ID of the client (always unique on a server. 1 = server, 0 is invalid).
223 * uint8 ID of the company the client is playing as (255 for spectators).
224 * string Name of the client.
225 * @param p The packet that was just received.
227 virtual NetworkRecvStatus
Receive_SERVER_CLIENT_INFO(Packet
*p
);
230 * Indication to the client that the server needs a game password.
231 * @param p The packet that was just received.
233 virtual NetworkRecvStatus
Receive_SERVER_NEED_GAME_PASSWORD(Packet
*p
);
236 * Indication to the client that the server needs a company password:
237 * uint32 Generation seed.
238 * string Network ID of the server.
239 * @param p The packet that was just received.
241 virtual NetworkRecvStatus
Receive_SERVER_NEED_COMPANY_PASSWORD(Packet
*p
);
244 * Send a password to the server to authorize:
245 * uint8 Password type (see NetworkPasswordType).
246 * string The password.
247 * @param p The packet that was just received.
249 virtual NetworkRecvStatus
Receive_CLIENT_GAME_PASSWORD(Packet
*p
);
252 * Send a password to the server to authorize
253 * uint8 Password type (see NetworkPasswordType).
254 * string The password.
255 * @param p The packet that was just received.
257 virtual NetworkRecvStatus
Receive_CLIENT_COMPANY_PASSWORD(Packet
*p
);
260 * The client is joined and ready to receive his map:
261 * uint32 Own client ID.
262 * uint32 Generation seed.
263 * string Network ID of the server.
264 * @param p The packet that was just received.
266 virtual NetworkRecvStatus
Receive_SERVER_WELCOME(Packet
*p
);
269 * Request the map from the server.
270 * uint32 NewGRF version (release versions of OpenTTD only).
271 * @param p The packet that was just received.
273 virtual NetworkRecvStatus
Receive_CLIENT_GETMAP(Packet
*p
);
276 * Notification that another client is currently receiving the map:
277 * uint8 Number of clients waiting in front of you.
278 * @param p The packet that was just received.
280 virtual NetworkRecvStatus
Receive_SERVER_WAIT(Packet
*p
);
283 * Sends that the server will begin with sending the map to the client:
284 * uint32 Current frame.
285 * @param p The packet that was just received.
287 virtual NetworkRecvStatus
Receive_SERVER_MAP_BEGIN(Packet
*p
);
290 * Sends the size of the map to the client.
291 * uint32 Size of the (compressed) map (in bytes).
292 * @param p The packet that was just received.
294 virtual NetworkRecvStatus
Receive_SERVER_MAP_SIZE(Packet
*p
);
297 * Sends the data of the map to the client:
298 * Contains a part of the map (until max size of packet).
299 * @param p The packet that was just received.
301 virtual NetworkRecvStatus
Receive_SERVER_MAP_DATA(Packet
*p
);
304 * Sends that all data of the map are sent to the client:
305 * @param p The packet that was just received.
307 virtual NetworkRecvStatus
Receive_SERVER_MAP_DONE(Packet
*p
);
310 * Tell the server that we are done receiving/loading the map.
311 * @param p The packet that was just received.
313 virtual NetworkRecvStatus
Receive_CLIENT_MAP_OK(Packet
*p
);
316 * A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO:
317 * uint32 ID of the client that just joined the game.
318 * @param p The packet that was just received.
320 virtual NetworkRecvStatus
Receive_SERVER_JOIN(Packet
*p
);
323 * Sends the current frame counter to the client:
324 * uint32 Frame counter
325 * uint32 Frame counter max (how far may the client walk before the server?)
326 * uint32 General seed 1 (dependent on compile settings, not default).
327 * uint32 General seed 2 (dependent on compile settings, not default).
328 * uint8 Random token to validate the client is actually listening (only occasionally present).
329 * @param p The packet that was just received.
331 virtual NetworkRecvStatus
Receive_SERVER_FRAME(Packet
*p
);
334 * Sends a sync-check to the client:
335 * uint32 Frame counter.
336 * uint32 General seed 1.
337 * uint32 General seed 2 (dependent on compile settings, not default).
338 * @param p The packet that was just received.
340 virtual NetworkRecvStatus
Receive_SERVER_SYNC(Packet
*p
);
343 * Tell the server we are done with this frame:
344 * uint32 Current frame counter of the client.
345 * uint8 The random token that the server sent in the PACKET_SERVER_FRAME packet.
346 * @param p The packet that was just received.
348 virtual NetworkRecvStatus
Receive_CLIENT_ACK(Packet
*p
);
351 * Send a DoCommand to the Server:
352 * uint8 ID of the company (0..MAX_COMPANIES-1).
353 * uint32 ID of the command (see command.h).
354 * uint32 P1 (free variables used in DoCommand).
356 * uint32 Tile where this is taking place.
358 * uint8 ID of the callback.
359 * @param p The packet that was just received.
361 virtual NetworkRecvStatus
Receive_CLIENT_COMMAND(Packet
*p
);
364 * Sends a DoCommand to the client:
365 * uint8 ID of the company (0..MAX_COMPANIES-1).
366 * uint32 ID of the command (see command.h).
367 * uint32 P1 (free variable used in DoCommand).
369 * uint32 Tile where this is taking place.
371 * uint8 ID of the callback.
372 * uint32 Frame of execution.
373 * @param p The packet that was just received.
375 virtual NetworkRecvStatus
Receive_SERVER_COMMAND(Packet
*p
);
378 * Sends a chat-packet to the server:
379 * uint8 ID of the action (see NetworkAction).
380 * uint8 ID of the destination type (see DestType).
381 * uint32 ID of the client or company (destination of the chat).
382 * string Message (max NETWORK_CHAT_LENGTH).
383 * uint64 data (used e.g. for 'give money' actions).
384 * @param p The packet that was just received.
386 virtual NetworkRecvStatus
Receive_CLIENT_CHAT(Packet
*p
);
389 * Sends a chat-packet to the client:
390 * uint8 ID of the action (see NetworkAction).
391 * uint32 ID of the client (origin of the chat).
392 * string Message (max NETWORK_CHAT_LENGTH).
393 * uint64 data (used e.g. for 'give money' actions).
394 * @param p The packet that was just received.
396 virtual NetworkRecvStatus
Receive_SERVER_CHAT(Packet
*p
);
399 * Set the password for the clients current company:
400 * string The password.
401 * @param p The packet that was just received.
403 virtual NetworkRecvStatus
Receive_CLIENT_SET_PASSWORD(Packet
*p
);
406 * Gives the client a new name:
407 * string New name of the client.
408 * @param p The packet that was just received.
410 virtual NetworkRecvStatus
Receive_CLIENT_SET_NAME(Packet
*p
);
413 * The client is quitting the game.
414 * @param p The packet that was just received.
416 virtual NetworkRecvStatus
Receive_CLIENT_QUIT(Packet
*p
);
419 * The client made an error and is quitting the game.
420 * uint8 Error of the code caused (see NetworkErrorCode).
421 * @param p The packet that was just received.
423 virtual NetworkRecvStatus
Receive_CLIENT_ERROR(Packet
*p
);
426 * Notification that a client left the game:
427 * uint32 ID of the client.
428 * @param p The packet that was just received.
430 virtual NetworkRecvStatus
Receive_SERVER_QUIT(Packet
*p
);
433 * Inform all clients that one client made an error and thus has quit/been disconnected:
434 * uint32 ID of the client that caused the error.
435 * uint8 Code of the error caused (see NetworkErrorCode).
436 * @param p The packet that was just received.
438 virtual NetworkRecvStatus
Receive_SERVER_ERROR_QUIT(Packet
*p
);
441 * Let the clients know that the server is closing.
442 * @param p The packet that was just received.
444 virtual NetworkRecvStatus
Receive_SERVER_SHUTDOWN(Packet
*p
);
447 * Let the clients know that the server is loading a new map.
448 * @param p The packet that was just received.
450 virtual NetworkRecvStatus
Receive_SERVER_NEWGAME(Packet
*p
);
453 * Send the result of an issues RCon command back to the client:
454 * uint16 Colour code.
455 * string Output of the RCon command
456 * @param p The packet that was just received.
458 virtual NetworkRecvStatus
Receive_SERVER_RCON(Packet
*p
);
461 * Send an RCon command to the server:
462 * string RCon password.
463 * string Command to be executed.
464 * @param p The packet that was just received.
466 virtual NetworkRecvStatus
Receive_CLIENT_RCON(Packet
*p
);
469 * Sends information about all used GRFs to the client:
470 * uint8 Amount of GRFs (the following data is repeated this many times, i.e. per GRF data).
472 * 16 * uint8 MD5 checksum of the GRF
473 * @param p The packet that was just received.
475 virtual NetworkRecvStatus
Receive_SERVER_CHECK_NEWGRFS(Packet
*p
);
478 * Tell the server that we have the required GRFs
479 * @param p The packet that was just received.
481 virtual NetworkRecvStatus
Receive_CLIENT_NEWGRFS_CHECKED(Packet
*p
);
484 * Move a client from one company into another:
485 * uint32 ID of the client.
486 * uint8 ID of the new company.
487 * @param p The packet that was just received.
489 virtual NetworkRecvStatus
Receive_SERVER_MOVE(Packet
*p
);
492 * Request the server to move this client into another company:
493 * uint8 ID of the company the client wants to join.
494 * string Password, if the company is password protected.
495 * @param p The packet that was just received.
497 virtual NetworkRecvStatus
Receive_CLIENT_MOVE(Packet
*p
);
500 * Update the clients knowledge of which company is password protected:
501 * uint16 Bitwise representation of each company
502 * @param p The packet that was just received.
504 virtual NetworkRecvStatus
Receive_SERVER_COMPANY_UPDATE(Packet
*p
);
507 * Update the clients knowledge of the max settings:
508 * uint8 Maximum number of companies allowed.
509 * uint8 Maximum number of spectators allowed.
510 * @param p The packet that was just received.
512 virtual NetworkRecvStatus
Receive_SERVER_CONFIG_UPDATE(Packet
*p
);
514 NetworkRecvStatus
HandlePacket(Packet
*p
);
516 NetworkGameSocketHandler(SOCKET s
);
518 ClientID client_id
; ///< Client identifier
519 uint32 last_frame
; ///< Last frame we have executed
520 uint32 last_frame_server
; ///< Last frame the server has executed
521 CommandQueue incoming_queue
; ///< The command-queue awaiting handling
522 std::chrono::steady_clock::time_point last_packet
; ///< Time we received the last frame.
524 NetworkRecvStatus
CloseConnection(bool error
= true) override
;
527 * Close the network connection due to the given status.
528 * @param status The reason the connection got closed.
530 virtual NetworkRecvStatus
CloseConnection(NetworkRecvStatus status
) = 0;
531 virtual ~NetworkGameSocketHandler() {}
534 * Sets the client info for this socket handler.
535 * @param info The new client info.
537 inline void SetInfo(NetworkClientInfo
*info
)
539 assert(info
!= nullptr && this->info
== nullptr);
544 * Gets the client info of this socket handler.
545 * @return The client info.
547 inline NetworkClientInfo
*GetInfo() const
552 NetworkRecvStatus
ReceivePackets();
554 const char *ReceiveCommand(Packet
*p
, CommandPacket
*cp
);
555 void SendCommand(Packet
*p
, const CommandPacket
*cp
);
558 #endif /* NETWORK_CORE_TCP_GAME_H */