Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / network / core / tcp_game.h
blob828e7ad446470c47a4980d3fe1762784220c096c
1 /* $Id: tcp_game.h 26056 2013-11-22 21:50:43Z rubidium $ */
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 "../../core/pool_type.hpp"
22 #ifdef ENABLE_NETWORK
24 /**
25 * Enum with all types of TCP packets.
26 * For the exact meaning, look at #NetworkGameSocketHandler.
28 enum PacketGameType {
30 * These first three pair of packets (thus six in
31 * total) must remain in this order for backward
32 * and forward compatibility between clients that
33 * are trying to join directly.
36 /* Packets sent by socket accepting code without ever constructing a client socket instance. */
37 PACKET_SERVER_FULL, ///< The server is full and has no place for you.
38 PACKET_SERVER_BANNED, ///< The server has banned you.
40 /* Packets used by the client to join and an error message when the revision is wrong. */
41 PACKET_CLIENT_JOIN, ///< The client telling the server it wants to join.
42 PACKET_SERVER_ERROR, ///< Server sending an error message to the client.
44 /* Packets used for the pre-game lobby. */
45 PACKET_CLIENT_COMPANY_INFO, ///< Request information about all companies.
46 PACKET_SERVER_COMPANY_INFO, ///< Information about a single company.
49 * Packets after here assume that the client
50 * and server are running the same version. As
51 * such ordering is unimportant from here on.
53 * The following is the remainder of the packets
54 * sent as part of authenticating and getting
55 * the map and other important data.
58 /* After the join step, the first is checking NewGRFs. */
59 PACKET_SERVER_CHECK_NEWGRFS, ///< Server sends NewGRF IDs and MD5 checksums for the client to check.
60 PACKET_CLIENT_NEWGRFS_CHECKED, ///< Client acknowledges that it has all required NewGRFs.
62 /* Checking the game, and then company passwords. */
63 PACKET_SERVER_NEED_GAME_PASSWORD, ///< Server requests the (hashed) game password.
64 PACKET_CLIENT_GAME_PASSWORD, ///< Clients sends the (hashed) game password.
65 PACKET_SERVER_NEED_COMPANY_PASSWORD, ///< Server requests the (hashed) company password.
66 PACKET_CLIENT_COMPANY_PASSWORD, ///< Client sends the (hashed) company password.
68 /* The server welcomes the authenticated client and sends information of other clients. */
69 PACKET_SERVER_WELCOME, ///< Server welcomes you and gives you your #ClientID.
70 PACKET_SERVER_CLIENT_INFO, ///< Server sends you information about a client.
72 /* Getting the savegame/map. */
73 PACKET_CLIENT_GETMAP, ///< Client requests the actual map.
74 PACKET_SERVER_WAIT, ///< Server tells the client there are some people waiting for the map as well.
75 PACKET_SERVER_MAP_BEGIN, ///< Server tells the client that it is beginning to send the map.
76 PACKET_SERVER_MAP_SIZE, ///< Server tells the client what the (compressed) size of the map is.
77 PACKET_SERVER_MAP_DATA, ///< Server sends bits of the map to the client.
78 PACKET_SERVER_MAP_DONE, ///< Server tells it has just sent the last bits of the map to the client.
79 PACKET_CLIENT_MAP_OK, ///< Client tells the server that it received the whole map.
81 PACKET_SERVER_JOIN, ///< Tells clients that a new client has joined.
84 * At this moment the client has the map and
85 * the client is fully authenticated. Now the
86 * normal communication starts.
89 /* Game progress monitoring. */
90 PACKET_SERVER_FRAME, ///< Server tells the client what frame it is in, and thus to where the client may progress.
91 PACKET_CLIENT_ACK, ///< The client tells the server which frame it has executed.
92 PACKET_SERVER_SYNC, ///< Server tells the client what the random state should be.
94 /* Sending commands around. */
95 PACKET_CLIENT_COMMAND, ///< Client executed a command and sends it to the server.
96 PACKET_SERVER_COMMAND, ///< Server distributes a command to (all) the clients.
98 /* Human communication! */
99 PACKET_CLIENT_CHAT, ///< Client said something that should be distributed.
100 PACKET_SERVER_CHAT, ///< Server distributing the message of a client (or itself).
102 /* Remote console. */
103 PACKET_CLIENT_RCON, ///< Client asks the server to execute some command.
104 PACKET_SERVER_RCON, ///< Response of the executed command on the server.
106 /* Moving a client.*/
107 PACKET_CLIENT_MOVE, ///< A client would like to be moved to another company.
108 PACKET_SERVER_MOVE, ///< Server tells everyone that someone is moved to another company.
110 /* Configuration updates. */
111 PACKET_CLIENT_SET_PASSWORD, ///< A client (re)sets its company's password.
112 PACKET_CLIENT_SET_NAME, ///< A client changes its name.
113 PACKET_SERVER_COMPANY_UPDATE, ///< Information (password) of a company changed.
114 PACKET_SERVER_CONFIG_UPDATE, ///< Some network configuration important to the client changed.
116 /* A server quitting this game. */
117 PACKET_SERVER_NEWGAME, ///< The server is preparing to start a new game.
118 PACKET_SERVER_SHUTDOWN, ///< The server is shutting down.
120 /* A client quitting. */
121 PACKET_CLIENT_QUIT, ///< A client tells the server it is going to quit.
122 PACKET_SERVER_QUIT, ///< A server tells that a client has quit.
123 PACKET_CLIENT_ERROR, ///< A client reports an error to the server.
124 PACKET_SERVER_ERROR_QUIT, ///< A server tells that a client has hit an error and did quit.
126 PACKET_END, ///< Must ALWAYS be on the end of this list!! (period)
129 /** Packet that wraps a command */
130 struct CommandPacket;
132 /** A queue of CommandPackets. */
133 class CommandQueue {
134 CommandPacket *first; ///< The first packet in the queue.
135 CommandPacket *last; ///< The last packet in the queue; only valid when first != NULL.
136 uint count; ///< The number of items in the queue.
138 public:
139 /** Initialise the command queue. */
140 CommandQueue() : first(NULL), last(NULL), count(0) {}
141 /** Clear the command queue. */
142 ~CommandQueue() { this->Free(); }
143 void Append(CommandPacket *p);
144 CommandPacket *Pop(bool ignore_paused = false);
145 CommandPacket *Peek(bool ignore_paused = false);
146 void Free();
147 /** Get the number of items in the queue. */
148 uint Count() const { return this->count; }
151 /** Base socket handler for all TCP sockets */
152 class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
153 /* TODO: rewrite into a proper class */
154 private:
155 NetworkClientInfo *info; ///< Client info related to this socket
157 protected:
158 NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type);
161 * Notification that the server is full.
162 * @param p The packet that was just received.
164 virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
167 * Notification that the client trying to join is banned.
168 * @param p The packet that was just received.
170 virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
173 * Try to join the server:
174 * string OpenTTD revision (norev000 if no revision).
175 * string Name of the client (max NETWORK_NAME_LENGTH).
176 * uint8 ID of the company to play as (1..MAX_COMPANIES).
177 * uint8 ID of the clients Language.
178 * @param p The packet that was just received.
180 virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
183 * The client made an error:
184 * uint8 Error code caused (see NetworkErrorCode).
185 * @param p The packet that was just received.
187 virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
190 * Request company information (in detail).
191 * @param p The packet that was just received.
193 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
196 * Sends information about the companies (one packet per company):
197 * uint8 Version of the structure of this packet (NETWORK_COMPANY_INFO_VERSION).
198 * bool Contains data (false marks the end of updates).
199 * uint8 ID of the company.
200 * string Name of the company.
201 * uint32 Year the company was inaugurated.
202 * uint64 Value.
203 * uint64 Money.
204 * uint64 Income.
205 * uint16 Performance (last quarter).
206 * bool Company is password protected.
207 * uint16 Number of trains.
208 * uint16 Number of lorries.
209 * uint16 Number of busses.
210 * uint16 Number of planes.
211 * uint16 Number of ships.
212 * uint16 Number of train stations.
213 * uint16 Number of lorry stations.
214 * uint16 Number of bus stops.
215 * uint16 Number of airports and heliports.
216 * uint16 Number of harbours.
217 * bool Company is an AI.
218 * string Client names (comma separated list)
219 * @param p The packet that was just received.
221 virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
224 * Send information about a client:
225 * uint32 ID of the client (always unique on a server. 1 = server, 0 is invalid).
226 * uint8 ID of the company the client is playing as (255 for spectators).
227 * string Name of the client.
228 * @param p The packet that was just received.
230 virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
233 * Indication to the client that the server needs a game password.
234 * @param p The packet that was just received.
236 virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
239 * Indication to the client that the server needs a company password:
240 * uint32 Generation seed.
241 * string Network ID of the server.
242 * @param p The packet that was just received.
244 virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
247 * Send a password to the server to authorize:
248 * uint8 Password type (see NetworkPasswordType).
249 * string The password.
250 * @param p The packet that was just received.
252 virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
255 * Send a password to the server to authorize
256 * uint8 Password type (see NetworkPasswordType).
257 * string The password.
258 * @param p The packet that was just received.
260 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
263 * The client is joined and ready to receive his map:
264 * uint32 Own client ID.
265 * uint32 Generation seed.
266 * string Network ID of the server.
267 * @param p The packet that was just received.
269 virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
272 * Request the map from the server.
273 * uint32 NewGRF version (release versions of OpenTTD only).
274 * @param p The packet that was just received.
276 virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
279 * Notification that another client is currently receiving the map:
280 * uint8 Number of clients waiting in front of you.
281 * @param p The packet that was just received.
283 virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
286 * Sends that the server will begin with sending the map to the client:
287 * uint32 Current frame.
288 * @param p The packet that was just received.
290 virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
293 * Sends the size of the map to the client.
294 * uint32 Size of the (compressed) map (in bytes).
295 * @param p The packet that was just received.
297 virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
300 * Sends the data of the map to the client:
301 * Contains a part of the map (until max size of packet).
302 * @param p The packet that was just received.
304 virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
307 * Sends that all data of the map are sent to the client:
308 * @param p The packet that was just received.
310 virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
313 * Tell the server that we are done receiving/loading the map.
314 * @param p The packet that was just received.
316 virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
319 * A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO:
320 * uint32 ID of the client that just joined the game.
321 * @param p The packet that was just received.
323 virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
326 * Sends the current frame counter to the client:
327 * uint32 Frame counter
328 * uint32 Frame counter max (how far may the client walk before the server?)
329 * uint32 General seed 1 (dependent on compile settings, not default).
330 * uint32 General seed 2 (dependent on compile settings, not default).
331 * uint8 Random token to validate the client is actually listening (only occasionally present).
332 * @param p The packet that was just received.
334 virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
337 * Sends a sync-check to the client:
338 * uint32 Frame counter.
339 * uint32 General seed 1.
340 * uint32 General seed 2 (dependent on compile settings, not default).
341 * @param p The packet that was just received.
343 virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
346 * Tell the server we are done with this frame:
347 * uint32 Current frame counter of the client.
348 * uint8 The random token that the server sent in the PACKET_SERVER_FRAME packet.
349 * @param p The packet that was just received.
351 virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
354 * Send a DoCommand to the Server:
355 * uint8 ID of the company (0..MAX_COMPANIES-1).
356 * uint32 ID of the command (see command.h).
357 * uint32 P1 (free variables used in DoCommand).
358 * uint32 P2
359 * uint32 Tile where this is taking place.
360 * string Text.
361 * uint8 ID of the callback.
362 * @param p The packet that was just received.
364 virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
367 * Sends a DoCommand to the client:
368 * uint8 ID of the company (0..MAX_COMPANIES-1).
369 * uint32 ID of the command (see command.h).
370 * uint32 P1 (free variable used in DoCommand).
371 * uint32 P2.
372 * uint32 Tile where this is taking place.
373 * string Text.
374 * uint8 ID of the callback.
375 * uint32 Frame of execution.
376 * @param p The packet that was just received.
378 virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
381 * Sends a chat-packet to the server:
382 * uint8 ID of the action (see NetworkAction).
383 * uint8 ID of the destination type (see DestType).
384 * uint32 ID of the client or company (destination of the chat).
385 * string Message (max NETWORK_CHAT_LENGTH).
386 * uint64 data (used e.g. for 'give money' actions).
387 * @param p The packet that was just received.
389 virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
392 * Sends a chat-packet to the client:
393 * uint8 ID of the action (see NetworkAction).
394 * uint32 ID of the client (origin of the chat).
395 * string Message (max NETWORK_CHAT_LENGTH).
396 * uint64 data (used e.g. for 'give money' actions).
397 * @param p The packet that was just received.
399 virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
402 * Set the password for the clients current company:
403 * string The password.
404 * @param p The packet that was just received.
406 virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
409 * Gives the client a new name:
410 * string New name of the client.
411 * @param p The packet that was just received.
413 virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
416 * The client is quitting the game.
417 * @param p The packet that was just received.
419 virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
422 * The client made an error and is quitting the game.
423 * uint8 Error of the code caused (see NetworkErrorCode).
424 * @param p The packet that was just received.
426 virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
429 * Notification that a client left the game:
430 * uint32 ID of the client.
431 * @param p The packet that was just received.
433 virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
436 * Inform all clients that one client made an error and thus has quit/been disconnected:
437 * uint32 ID of the client that caused the error.
438 * uint8 Code of the error caused (see NetworkErrorCode).
439 * @param p The packet that was just received.
441 virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
444 * Let the clients know that the server is closing.
445 * @param p The packet that was just received.
447 virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
450 * Let the clients know that the server is loading a new map.
451 * @param p The packet that was just received.
453 virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
456 * Send the result of an issues RCon command back to the client:
457 * uint16 Colour code.
458 * string Output of the RCon command
459 * @param p The packet that was just received.
461 virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
464 * Send an RCon command to the server:
465 * string RCon password.
466 * string Command to be executed.
467 * @param p The packet that was just received.
469 virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
472 * Sends information about all used GRFs to the client:
473 * uint8 Amount of GRFs (the following data is repeated this many times, i.e. per GRF data).
474 * uint32 GRF ID
475 * 16 * uint8 MD5 checksum of the GRF
476 * @param p The packet that was just received.
478 virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
481 * Tell the server that we have the required GRFs
482 * @param p The packet that was just received.
484 virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
487 * Move a client from one company into another:
488 * uint32 ID of the client.
489 * uint8 ID of the new company.
490 * @param p The packet that was just received.
492 virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
495 * Request the server to move this client into another company:
496 * uint8 ID of the company the client wants to join.
497 * string Password, if the company is password protected.
498 * @param p The packet that was just received.
500 virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
503 * Update the clients knowledge of which company is password protected:
504 * uint16 Bitwise representation of each company
505 * @param p The packet that was just received.
507 virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
510 * Update the clients knowledge of the max settings:
511 * uint8 Maximum number of companies allowed.
512 * uint8 Maximum number of spectators allowed.
513 * @param p The packet that was just received.
515 virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);
517 NetworkRecvStatus HandlePacket(Packet *p);
519 NetworkGameSocketHandler(SOCKET s);
520 public:
521 ClientID client_id; ///< Client identifier
522 uint32 last_frame; ///< Last frame we have executed
523 uint32 last_frame_server; ///< Last frame the server has executed
524 CommandQueue incoming_queue; ///< The command-queue awaiting handling
525 uint last_packet; ///< Time we received the last frame.
527 NetworkRecvStatus CloseConnection(bool error = true);
530 * Close the network connection due to the given status.
531 * @param status The reason the connection got closed.
533 virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
534 virtual ~NetworkGameSocketHandler() {}
537 * Sets the client info for this socket handler.
538 * @param info The new client info.
540 inline void SetInfo(NetworkClientInfo *info)
542 assert(info != NULL && this->info == NULL);
543 this->info = info;
547 * Gets the client info of this socket handler.
548 * @return The client info.
550 inline NetworkClientInfo *GetInfo() const
552 return this->info;
555 NetworkRecvStatus ReceivePackets();
557 const char *ReceiveCommand(Packet *p, CommandPacket *cp);
558 void SendCommand(Packet *p, const CommandPacket *cp);
561 #endif /* ENABLE_NETWORK */
563 #endif /* NETWORK_CORE_TCP_GAME_H */