Translations update
[openttd/fttd.git] / src / network / core / tcp_game.h
blobf66fa18e44c55a9bb2b83863a327972c0dd1b56d
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 /**
11 * @file tcp_game.h Basic functions to receive and send TCP packets for game purposes.
14 #ifndef NETWORK_CORE_TCP_GAME_H
15 #define NETWORK_CORE_TCP_GAME_H
17 #include "os_abstraction.h"
18 #include "tcp.h"
19 #include "../network_type.h"
20 #include "../network_command.h"
21 #include "../../core/pool_type.hpp"
23 #ifdef ENABLE_NETWORK
25 /**
26 * Enum with all types of TCP packets.
27 * For the exact meaning, look at #NetworkGameSocketHandler.
29 enum PacketGameType {
31 * These first three pair of packets (thus six in
32 * total) must remain in this order for backward
33 * and forward compatibility between clients that
34 * are trying to join directly.
37 /* Packets sent by socket accepting code without ever constructing a client socket instance. */
38 PACKET_SERVER_FULL, ///< The server is full and has no place for you.
39 PACKET_SERVER_BANNED, ///< The server has banned you.
41 /* Packets used by the client to join and an error message when the revision is wrong. */
42 PACKET_CLIENT_JOIN, ///< The client telling the server it wants to join.
43 PACKET_SERVER_ERROR, ///< Server sending an error message to the client.
45 /* Packets used for the pre-game lobby. */
46 PACKET_CLIENT_COMPANY_INFO, ///< Request information about all companies.
47 PACKET_SERVER_COMPANY_INFO, ///< Information about a single company.
50 * Packets after here assume that the client
51 * and server are running the same version. As
52 * such ordering is unimportant from here on.
54 * The following is the remainder of the packets
55 * sent as part of authenticating and getting
56 * the map and other important data.
59 /* After the join step, the first is checking NewGRFs. */
60 PACKET_SERVER_CHECK_NEWGRFS, ///< Server sends NewGRF IDs and MD5 checksums for the client to check.
61 PACKET_CLIENT_NEWGRFS_CHECKED, ///< Client acknowledges that it has all required NewGRFs.
63 /* Checking the game, and then company passwords. */
64 PACKET_SERVER_NEED_GAME_PASSWORD, ///< Server requests the (hashed) game password.
65 PACKET_CLIENT_GAME_PASSWORD, ///< Clients sends the (hashed) game password.
66 PACKET_SERVER_NEED_COMPANY_PASSWORD, ///< Server requests the (hashed) company password.
67 PACKET_CLIENT_COMPANY_PASSWORD, ///< Client sends the (hashed) company password.
69 /* The server welcomes the authenticated client and sends information of other clients. */
70 PACKET_SERVER_WELCOME, ///< Server welcomes you and gives you your #ClientID.
71 PACKET_SERVER_CLIENT_INFO, ///< Server sends you information about a client.
73 /* Getting the savegame/map. */
74 PACKET_CLIENT_GETMAP, ///< Client requests the actual map.
75 PACKET_SERVER_WAIT, ///< Server tells the client there are some people waiting for the map as well.
76 PACKET_SERVER_MAP_BEGIN, ///< Server tells the client that it is beginning to send the map.
77 PACKET_SERVER_MAP_SIZE, ///< Server tells the client what the (compressed) size of the map is.
78 PACKET_SERVER_MAP_DATA, ///< Server sends bits of the map to the client.
79 PACKET_SERVER_MAP_DONE, ///< Server tells it has just sent the last bits of the map to the client.
80 PACKET_CLIENT_MAP_OK, ///< Client tells the server that it received the whole map.
82 PACKET_SERVER_JOIN, ///< Tells clients that a new client has joined.
85 * At this moment the client has the map and
86 * the client is fully authenticated. Now the
87 * normal communication starts.
90 /* Game progress monitoring. */
91 PACKET_SERVER_FRAME, ///< Server tells the client what frame it is in, and thus to where the client may progress.
92 PACKET_CLIENT_ACK, ///< The client tells the server which frame it has executed.
93 PACKET_SERVER_SYNC, ///< Server tells the client what the random state should be.
95 /* Sending commands around. */
96 PACKET_CLIENT_COMMAND, ///< Client executed a command and sends it to the server.
97 PACKET_SERVER_COMMAND, ///< Server distributes a command to (all) the clients.
99 /* Human communication! */
100 PACKET_CLIENT_CHAT, ///< Client said something that should be distributed.
101 PACKET_SERVER_CHAT, ///< Server distributing the message of a client (or itself).
103 /* Remote console. */
104 PACKET_CLIENT_RCON, ///< Client asks the server to execute some command.
105 PACKET_SERVER_RCON, ///< Response of the executed command on the server.
107 /* Moving a client.*/
108 PACKET_CLIENT_MOVE, ///< A client would like to be moved to another company.
109 PACKET_SERVER_MOVE, ///< Server tells everyone that someone is moved to another company.
111 /* Configuration updates. */
112 PACKET_CLIENT_SET_PASSWORD, ///< A client (re)sets its company's password.
113 PACKET_CLIENT_SET_NAME, ///< A client changes its name.
114 PACKET_SERVER_COMPANY_UPDATE, ///< Information (password) of a company changed.
115 PACKET_SERVER_CONFIG_UPDATE, ///< Some network configuration important to the client changed.
117 /* A server quitting this game. */
118 PACKET_SERVER_NEWGAME, ///< The server is preparing to start a new game.
119 PACKET_SERVER_SHUTDOWN, ///< The server is shutting down.
121 /* A client quitting. */
122 PACKET_CLIENT_QUIT, ///< A client tells the server it is going to quit.
123 PACKET_SERVER_QUIT, ///< A server tells that a client has quit.
124 PACKET_CLIENT_ERROR, ///< A client reports an error to the server.
125 PACKET_SERVER_ERROR_QUIT, ///< A server tells that a client has hit an error and did quit.
127 PACKET_END, ///< Must ALWAYS be on the end of this list!! (period)
130 /** Base socket handler for all TCP sockets */
131 class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
132 /* TODO: rewrite into a proper class */
133 private:
134 NetworkClientInfo *info; ///< Client info related to this socket
136 protected:
137 NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type);
140 * Notification that the server is full.
141 * @param p The packet that was just received.
143 virtual NetworkRecvStatus Receive_SERVER_FULL (RecvPacket *p);
146 * Notification that the client trying to join is banned.
147 * @param p The packet that was just received.
149 virtual NetworkRecvStatus Receive_SERVER_BANNED (RecvPacket *p);
152 * Try to join the server:
153 * string OpenTTD revision (norev000 if no revision).
154 * string Name of the client (max NETWORK_NAME_LENGTH).
155 * uint8 ID of the company to play as (1..MAX_COMPANIES).
156 * uint8 ID of the clients Language.
157 * @param p The packet that was just received.
159 virtual NetworkRecvStatus Receive_CLIENT_JOIN (RecvPacket *p);
162 * The client made an error:
163 * uint8 Error code caused (see NetworkErrorCode).
164 * @param p The packet that was just received.
166 virtual NetworkRecvStatus Receive_SERVER_ERROR (RecvPacket *p);
169 * Request company information (in detail).
170 * @param p The packet that was just received.
172 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO (RecvPacket *p);
175 * Sends information about the companies (one packet per company):
176 * uint8 Version of the structure of this packet (NETWORK_COMPANY_INFO_VERSION).
177 * bool Contains data (false marks the end of updates).
178 * uint8 ID of the company.
179 * string Name of the company.
180 * uint32 Year the company was inaugurated.
181 * uint64 Value.
182 * uint64 Money.
183 * uint64 Income.
184 * uint16 Performance (last quarter).
185 * bool Company is password protected.
186 * uint16 Number of trains.
187 * uint16 Number of lorries.
188 * uint16 Number of busses.
189 * uint16 Number of planes.
190 * uint16 Number of ships.
191 * uint16 Number of train stations.
192 * uint16 Number of lorry stations.
193 * uint16 Number of bus stops.
194 * uint16 Number of airports and heliports.
195 * uint16 Number of harbours.
196 * bool Company is an AI.
197 * string Client names (comma separated list)
198 * @param p The packet that was just received.
200 virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO (RecvPacket *p);
203 * Send information about a client:
204 * uint32 ID of the client (always unique on a server. 1 = server, 0 is invalid).
205 * uint8 ID of the company the client is playing as (255 for spectators).
206 * string Name of the client.
207 * @param p The packet that was just received.
209 virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO (RecvPacket *p);
212 * Indication to the client that the server needs a game password.
213 * @param p The packet that was just received.
215 virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD (RecvPacket *p);
218 * Indication to the client that the server needs a company password:
219 * uint32 Generation seed.
220 * string Network ID of the server.
221 * @param p The packet that was just received.
223 virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD (RecvPacket *p);
226 * Send a password to the server to authorize:
227 * uint8 Password type (see NetworkPasswordType).
228 * string The password.
229 * @param p The packet that was just received.
231 virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD (RecvPacket *p);
234 * Send a password to the server to authorize
235 * uint8 Password type (see NetworkPasswordType).
236 * string The password.
237 * @param p The packet that was just received.
239 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD (RecvPacket *p);
242 * The client is joined and ready to receive his map:
243 * uint32 Own client ID.
244 * uint32 Generation seed.
245 * string Network ID of the server.
246 * @param p The packet that was just received.
248 virtual NetworkRecvStatus Receive_SERVER_WELCOME (RecvPacket *p);
251 * Request the map from the server.
252 * uint32 NewGRF version (release versions of OpenTTD only).
253 * @param p The packet that was just received.
255 virtual NetworkRecvStatus Receive_CLIENT_GETMAP (RecvPacket *p);
258 * Notification that another client is currently receiving the map:
259 * uint8 Number of clients waiting in front of you.
260 * @param p The packet that was just received.
262 virtual NetworkRecvStatus Receive_SERVER_WAIT (RecvPacket *p);
265 * Sends that the server will begin with sending the map to the client:
266 * uint32 Current frame.
267 * @param p The packet that was just received.
269 virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN (RecvPacket *p);
272 * Sends the size of the map to the client.
273 * uint32 Size of the (compressed) map (in bytes).
274 * @param p The packet that was just received.
276 virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE (RecvPacket *p);
279 * Sends the data of the map to the client:
280 * Contains a part of the map (until max size of packet).
281 * @param p The packet that was just received.
283 virtual NetworkRecvStatus Receive_SERVER_MAP_DATA (RecvPacket *p);
286 * Sends that all data of the map are sent to the client:
287 * @param p The packet that was just received.
289 virtual NetworkRecvStatus Receive_SERVER_MAP_DONE (RecvPacket *p);
292 * Tell the server that we are done receiving/loading the map.
293 * @param p The packet that was just received.
295 virtual NetworkRecvStatus Receive_CLIENT_MAP_OK (RecvPacket *p);
298 * A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO:
299 * uint32 ID of the client that just joined the game.
300 * @param p The packet that was just received.
302 virtual NetworkRecvStatus Receive_SERVER_JOIN (RecvPacket *p);
305 * Sends the current frame counter to the client:
306 * uint32 Frame counter
307 * uint32 Frame counter max (how far may the client walk before the server?)
308 * uint32 General seed 1 (dependent on compile settings, not default).
309 * uint32 General seed 2 (dependent on compile settings, not default).
310 * uint8 Random token to validate the client is actually listening (only occasionally present).
311 * @param p The packet that was just received.
313 virtual NetworkRecvStatus Receive_SERVER_FRAME (RecvPacket *p);
316 * Sends a sync-check to the client:
317 * uint32 Frame counter.
318 * uint32 General seed 1.
319 * uint32 General seed 2 (dependent on compile settings, not default).
320 * @param p The packet that was just received.
322 virtual NetworkRecvStatus Receive_SERVER_SYNC (RecvPacket *p);
325 * Tell the server we are done with this frame:
326 * uint32 Current frame counter of the client.
327 * uint8 The random token that the server sent in the PACKET_SERVER_FRAME packet.
328 * @param p The packet that was just received.
330 virtual NetworkRecvStatus Receive_CLIENT_ACK (RecvPacket *p);
333 * Send a DoCommand to the Server:
334 * uint8 ID of the company (0..MAX_COMPANIES-1).
335 * uint32 ID of the command (see command.h).
336 * uint32 P1 (free variables used in DoCommand).
337 * uint32 P2
338 * uint32 Tile where this is taking place.
339 * string Text.
340 * uint8 ID of the callback.
341 * @param p The packet that was just received.
343 virtual NetworkRecvStatus Receive_CLIENT_COMMAND (RecvPacket *p);
346 * Sends a DoCommand to the client:
347 * uint8 ID of the company (0..MAX_COMPANIES-1).
348 * uint32 ID of the command (see command.h).
349 * uint32 P1 (free variable used in DoCommand).
350 * uint32 P2.
351 * uint32 Tile where this is taking place.
352 * string Text.
353 * uint8 ID of the callback.
354 * uint32 Frame of execution.
355 * @param p The packet that was just received.
357 virtual NetworkRecvStatus Receive_SERVER_COMMAND (RecvPacket *p);
360 * Sends a chat-packet to the server:
361 * uint8 ID of the action (see NetworkAction).
362 * uint8 ID of the destination type (see DestType).
363 * uint32 ID of the client or company (destination of the chat).
364 * string Message (max NETWORK_CHAT_LENGTH).
365 * uint64 data (used e.g. for 'give money' actions).
366 * @param p The packet that was just received.
368 virtual NetworkRecvStatus Receive_CLIENT_CHAT (RecvPacket *p);
371 * Sends a chat-packet to the client:
372 * uint8 ID of the action (see NetworkAction).
373 * uint32 ID of the client (origin of the chat).
374 * string Message (max NETWORK_CHAT_LENGTH).
375 * uint64 data (used e.g. for 'give money' actions).
376 * @param p The packet that was just received.
378 virtual NetworkRecvStatus Receive_SERVER_CHAT (RecvPacket *p);
381 * Set the password for the clients current company:
382 * string The password.
383 * @param p The packet that was just received.
385 virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD (RecvPacket *p);
388 * Gives the client a new name:
389 * string New name of the client.
390 * @param p The packet that was just received.
392 virtual NetworkRecvStatus Receive_CLIENT_SET_NAME (RecvPacket *p);
395 * The client is quitting the game.
396 * @param p The packet that was just received.
398 virtual NetworkRecvStatus Receive_CLIENT_QUIT (RecvPacket *p);
401 * The client made an error and is quitting the game.
402 * uint8 Error of the code caused (see NetworkErrorCode).
403 * @param p The packet that was just received.
405 virtual NetworkRecvStatus Receive_CLIENT_ERROR (RecvPacket *p);
408 * Notification that a client left the game:
409 * uint32 ID of the client.
410 * @param p The packet that was just received.
412 virtual NetworkRecvStatus Receive_SERVER_QUIT (RecvPacket *p);
415 * Inform all clients that one client made an error and thus has quit/been disconnected:
416 * uint32 ID of the client that caused the error.
417 * uint8 Code of the error caused (see NetworkErrorCode).
418 * @param p The packet that was just received.
420 virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT (RecvPacket *p);
423 * Let the clients know that the server is closing.
424 * @param p The packet that was just received.
426 virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN (RecvPacket *p);
429 * Let the clients know that the server is loading a new map.
430 * @param p The packet that was just received.
432 virtual NetworkRecvStatus Receive_SERVER_NEWGAME (RecvPacket *p);
435 * Send the result of an issues RCon command back to the client:
436 * uint16 Colour code.
437 * string Output of the RCon command
438 * @param p The packet that was just received.
440 virtual NetworkRecvStatus Receive_SERVER_RCON (RecvPacket *p);
443 * Send an RCon command to the server:
444 * string RCon password.
445 * string Command to be executed.
446 * @param p The packet that was just received.
448 virtual NetworkRecvStatus Receive_CLIENT_RCON (RecvPacket *p);
451 * Sends information about all used GRFs to the client:
452 * uint8 Amount of GRFs (the following data is repeated this many times, i.e. per GRF data).
453 * uint32 GRF ID
454 * 16 * uint8 MD5 checksum of the GRF
455 * @param p The packet that was just received.
457 virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS (RecvPacket *p);
460 * Tell the server that we have the required GRFs
461 * @param p The packet that was just received.
463 virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED (RecvPacket *p);
466 * Move a client from one company into another:
467 * uint32 ID of the client.
468 * uint8 ID of the new company.
469 * @param p The packet that was just received.
471 virtual NetworkRecvStatus Receive_SERVER_MOVE (RecvPacket *p);
474 * Request the server to move this client into another company:
475 * uint8 ID of the company the client wants to join.
476 * string Password, if the company is password protected.
477 * @param p The packet that was just received.
479 virtual NetworkRecvStatus Receive_CLIENT_MOVE (RecvPacket *p);
482 * Update the clients knowledge of which company is password protected:
483 * uint16 Bitwise representation of each company
484 * @param p The packet that was just received.
486 virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE (RecvPacket *p);
489 * Update the clients knowledge of the max settings:
490 * uint8 Maximum number of companies allowed.
491 * uint8 Maximum number of spectators allowed.
492 * @param p The packet that was just received.
494 virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE (RecvPacket *p);
496 NetworkRecvStatus HandlePacket (RecvPacket *p);
498 NetworkGameSocketHandler(SOCKET s);
499 public:
500 ClientID client_id; ///< Client identifier
501 uint32 last_frame; ///< Last frame we have executed
502 uint32 last_frame_server; ///< Last frame the server has executed
503 CommandQueue incoming_queue; ///< The command-queue awaiting handling
504 uint last_packet; ///< Time we received the last frame.
506 NetworkRecvStatus CloseConnection(bool error = true);
509 * Close the network connection due to the given status.
510 * @param status The reason the connection got closed.
512 virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
513 virtual ~NetworkGameSocketHandler() {}
516 * Sets the client info for this socket handler.
517 * @param info The new client info.
519 inline void SetInfo(NetworkClientInfo *info)
521 assert(info != NULL && this->info == NULL);
522 this->info = info;
526 * Gets the client info of this socket handler.
527 * @return The client info.
529 inline NetworkClientInfo *GetInfo() const
531 return this->info;
534 NetworkRecvStatus ReceivePackets();
537 #endif /* ENABLE_NETWORK */
539 #endif /* NETWORK_CORE_TCP_GAME_H */