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 8 /** 9 * @file tcp_game.h Basic functions to receive and send TCP packets for game purposes. 10 */ 11 12 #ifndef NETWORK_CORE_TCP_GAME_H 13 #define NETWORK_CORE_TCP_GAME_H 14 15 #include "os_abstraction.h" 16 #include "tcp.h" 17 #include "../network_type.h" 18 #include "../../core/pool_type.hpp" 19 #include <chrono> 20 21 /** 22 * Enum with all types of TCP packets. 23 * For the exact meaning, look at #NetworkGameSocketHandler. 24 */ 25 enum PacketGameType { 26 /* 27 * These first four pair of packets (thus eight in 28 * total) must remain in this order for backward 29 * and forward compatibility between clients that 30 * are trying to join directly. 31 */ 32 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. 36 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. 40 41 /* Unused packet types, formerly used for the pre-game lobby. */ 42 PACKET_CLIENT_UNUSED, ///< Unused. 43 PACKET_SERVER_UNUSED, ///< Unused. 44 45 /* Packets used to get the game info. */ 46 PACKET_SERVER_GAME_INFO, ///< Information about the server. 47 PACKET_CLIENT_GAME_INFO, ///< Request information about the server. 48 49 /* 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. 53 * 54 * The following is the remainder of the packets 55 * sent as part of authenticating and getting 56 * the map and other important data. 57 */ 58 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. 62 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. 68 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. 72 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. 81 82 PACKET_SERVER_JOIN, ///< Tells clients that a new client has joined. 83 84 /* 85 * At this moment the client has the map and 86 * the client is fully authenticated. Now the 87 * normal communication starts. 88 */ 89 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. 94 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. 98 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). 102 PACKET_SERVER_EXTERNAL_CHAT, ///< Server distributing the message from external source. 103 104 /* Remote console. */ 105 PACKET_CLIENT_RCON, ///< Client asks the server to execute some command. 106 PACKET_SERVER_RCON, ///< Response of the executed command on the server. 107 108 /* Moving a client.*/ 109 PACKET_CLIENT_MOVE, ///< A client would like to be moved to another company. 110 PACKET_SERVER_MOVE, ///< Server tells everyone that someone is moved to another company. 111 112 /* Configuration updates. */ 113 PACKET_CLIENT_SET_PASSWORD, ///< A client (re)sets its company's password. 114 PACKET_CLIENT_SET_NAME, ///< A client changes its name. 115 PACKET_SERVER_COMPANY_UPDATE, ///< Information (password) of a company changed. 116 PACKET_SERVER_CONFIG_UPDATE, ///< Some network configuration important to the client changed. 117 118 /* A server quitting this game. */ 119 PACKET_SERVER_NEWGAME, ///< The server is preparing to start a new game. 120 PACKET_SERVER_SHUTDOWN, ///< The server is shutting down. 121 122 /* A client quitting. */ 123 PACKET_CLIENT_QUIT, ///< A client tells the server it is going to quit. 124 PACKET_SERVER_QUIT, ///< A server tells that a client has quit. 125 PACKET_CLIENT_ERROR, ///< A client reports an error to the server. 126 PACKET_SERVER_ERROR_QUIT, ///< A server tells that a client has hit an error and did quit. 127 128 PACKET_END, ///< Must ALWAYS be on the end of this list!! (period) 129 }; 130 131 /** Packet that wraps a command */ 132 struct CommandPacket; 133 134 /** A queue of CommandPackets. */ 135 class CommandQueue { 136 CommandPacket *first; ///< The first packet in the queue. 137 CommandPacket *last; ///< The last packet in the queue; only valid when first != nullptr. 138 uint count; ///< The number of items in the queue. 139 140 public: 141 /** Initialise the command queue. */ CommandQueue()142 CommandQueue() : first(nullptr), last(nullptr), count(0) {} 143 /** Clear the command queue. */ ~CommandQueue()144 ~CommandQueue() { this->Free(); } 145 void Append(CommandPacket *p); 146 CommandPacket *Pop(bool ignore_paused = false); 147 CommandPacket *Peek(bool ignore_paused = false); 148 void Free(); 149 /** Get the number of items in the queue. */ Count()150 uint Count() const { return this->count; } 151 }; 152 153 /** Base socket handler for all TCP sockets */ 154 class NetworkGameSocketHandler : public NetworkTCPSocketHandler { 155 /* TODO: rewrite into a proper class */ 156 private: 157 NetworkClientInfo *info; ///< Client info related to this socket 158 159 protected: 160 NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type); 161 162 /** 163 * Notification that the server is full. 164 * @param p The packet that was just received. 165 */ 166 virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p); 167 168 /** 169 * Notification that the client trying to join is banned. 170 * @param p The packet that was just received. 171 */ 172 virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p); 173 174 /** 175 * Try to join the server: 176 * string OpenTTD revision (norev000 if no revision). 177 * string Name of the client (max NETWORK_NAME_LENGTH). 178 * uint8 ID of the company to play as (1..MAX_COMPANIES). 179 * uint8 ID of the clients Language. 180 * @param p The packet that was just received. 181 */ 182 virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p); 183 184 /** 185 * The client made an error: 186 * uint8 Error code caused (see NetworkErrorCode). 187 * @param p The packet that was just received. 188 */ 189 virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p); 190 191 /** 192 * Request game information. 193 * @param p The packet that was just received. 194 */ 195 virtual NetworkRecvStatus Receive_CLIENT_GAME_INFO(Packet *p); 196 197 /** 198 * Sends information about the game. 199 * Serialized NetworkGameInfo. See game_info.h for details. 200 * @param p The packet that was just received. 201 */ 202 virtual NetworkRecvStatus Receive_SERVER_GAME_INFO(Packet *p); 203 204 /** 205 * Send information about a client: 206 * uint32 ID of the client (always unique on a server. 1 = server, 0 is invalid). 207 * uint8 ID of the company the client is playing as (255 for spectators). 208 * string Name of the client. 209 * @param p The packet that was just received. 210 */ 211 virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p); 212 213 /** 214 * Indication to the client that the server needs a game password. 215 * @param p The packet that was just received. 216 */ 217 virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p); 218 219 /** 220 * Indication to the client that the server needs a company password: 221 * uint32 Generation seed. 222 * string Network ID of the server. 223 * @param p The packet that was just received. 224 */ 225 virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p); 226 227 /** 228 * Send a password to the server to authorize: 229 * uint8 Password type (see NetworkPasswordType). 230 * string The password. 231 * @param p The packet that was just received. 232 */ 233 virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p); 234 235 /** 236 * Send a password to the server to authorize 237 * uint8 Password type (see NetworkPasswordType). 238 * string The password. 239 * @param p The packet that was just received. 240 */ 241 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p); 242 243 /** 244 * The client is joined and ready to receive their map: 245 * uint32 Own client ID. 246 * uint32 Generation seed. 247 * string Network ID of the server. 248 * @param p The packet that was just received. 249 */ 250 virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p); 251 252 /** 253 * Request the map from the server. 254 * uint32 NewGRF version (release versions of OpenTTD only). 255 * @param p The packet that was just received. 256 */ 257 virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p); 258 259 /** 260 * Notification that another client is currently receiving the map: 261 * uint8 Number of clients waiting in front of you. 262 * @param p The packet that was just received. 263 */ 264 virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p); 265 266 /** 267 * Sends that the server will begin with sending the map to the client: 268 * uint32 Current frame. 269 * @param p The packet that was just received. 270 */ 271 virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p); 272 273 /** 274 * Sends the size of the map to the client. 275 * uint32 Size of the (compressed) map (in bytes). 276 * @param p The packet that was just received. 277 */ 278 virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p); 279 280 /** 281 * Sends the data of the map to the client: 282 * Contains a part of the map (until max size of packet). 283 * @param p The packet that was just received. 284 */ 285 virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p); 286 287 /** 288 * Sends that all data of the map are sent to the client: 289 * @param p The packet that was just received. 290 */ 291 virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p); 292 293 /** 294 * Tell the server that we are done receiving/loading the map. 295 * @param p The packet that was just received. 296 */ 297 virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p); 298 299 /** 300 * A client joined (PACKET_CLIENT_MAP_OK), what usually directly follows is a PACKET_SERVER_CLIENT_INFO: 301 * uint32 ID of the client that just joined the game. 302 * @param p The packet that was just received. 303 */ 304 virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p); 305 306 /** 307 * Sends the current frame counter to the client: 308 * uint32 Frame counter 309 * uint32 Frame counter max (how far may the client walk before the server?) 310 * uint32 General seed 1 (dependent on compile settings, not default). 311 * uint32 General seed 2 (dependent on compile settings, not default). 312 * uint8 Random token to validate the client is actually listening (only occasionally present). 313 * @param p The packet that was just received. 314 */ 315 virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p); 316 317 /** 318 * Sends a sync-check to the client: 319 * uint32 Frame counter. 320 * uint32 General seed 1. 321 * uint32 General seed 2 (dependent on compile settings, not default). 322 * @param p The packet that was just received. 323 */ 324 virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p); 325 326 /** 327 * Tell the server we are done with this frame: 328 * uint32 Current frame counter of the client. 329 * uint8 The random token that the server sent in the PACKET_SERVER_FRAME packet. 330 * @param p The packet that was just received. 331 */ 332 virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p); 333 334 /** 335 * Send a DoCommand to the Server: 336 * uint8 ID of the company (0..MAX_COMPANIES-1). 337 * uint32 ID of the command (see command.h). 338 * uint32 P1 (free variables used in DoCommand). 339 * uint32 P2 340 * uint32 Tile where this is taking place. 341 * string Text. 342 * uint8 ID of the callback. 343 * @param p The packet that was just received. 344 */ 345 virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p); 346 347 /** 348 * Sends a DoCommand to the client: 349 * uint8 ID of the company (0..MAX_COMPANIES-1). 350 * uint32 ID of the command (see command.h). 351 * uint32 P1 (free variable used in DoCommand). 352 * uint32 P2. 353 * uint32 Tile where this is taking place. 354 * string Text. 355 * uint8 ID of the callback. 356 * uint32 Frame of execution. 357 * @param p The packet that was just received. 358 */ 359 virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p); 360 361 /** 362 * Sends a chat-packet to the server: 363 * uint8 ID of the action (see NetworkAction). 364 * uint8 ID of the destination type (see DestType). 365 * uint32 ID of the client or company (destination of the chat). 366 * string Message (max NETWORK_CHAT_LENGTH). 367 * uint64 data (used e.g. for 'give money' actions). 368 * @param p The packet that was just received. 369 */ 370 virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p); 371 372 /** 373 * Sends a chat-packet to the client: 374 * uint8 ID of the action (see NetworkAction). 375 * uint32 ID of the client (origin of the chat). 376 * string Message (max NETWORK_CHAT_LENGTH). 377 * uint64 data (used e.g. for 'give money' actions). 378 * @param p The packet that was just received. 379 */ 380 virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p); 381 382 /** 383 * Sends a chat-packet for external source to the client: 384 * string Name of the source this message came from. 385 * uint16 TextColour to use for the message. 386 * string Name of the user who sent the messsage. 387 * string Message (max NETWORK_CHAT_LENGTH). 388 * @param p The packet that was just received. 389 */ 390 virtual NetworkRecvStatus Receive_SERVER_EXTERNAL_CHAT(Packet *p); 391 392 /** 393 * Set the password for the clients current company: 394 * string The password. 395 * @param p The packet that was just received. 396 */ 397 virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p); 398 399 /** 400 * Gives the client a new name: 401 * string New name of the client. 402 * @param p The packet that was just received. 403 */ 404 virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p); 405 406 /** 407 * The client is quitting the game. 408 * @param p The packet that was just received. 409 */ 410 virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p); 411 412 /** 413 * The client made an error and is quitting the game. 414 * uint8 Error of the code caused (see NetworkErrorCode). 415 * @param p The packet that was just received. 416 */ 417 virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p); 418 419 /** 420 * Notification that a client left the game: 421 * uint32 ID of the client. 422 * @param p The packet that was just received. 423 */ 424 virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p); 425 426 /** 427 * Inform all clients that one client made an error and thus has quit/been disconnected: 428 * uint32 ID of the client that caused the error. 429 * uint8 Code of the error caused (see NetworkErrorCode). 430 * @param p The packet that was just received. 431 */ 432 virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p); 433 434 /** 435 * Let the clients know that the server is closing. 436 * @param p The packet that was just received. 437 */ 438 virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p); 439 440 /** 441 * Let the clients know that the server is loading a new map. 442 * @param p The packet that was just received. 443 */ 444 virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p); 445 446 /** 447 * Send the result of an issues RCon command back to the client: 448 * uint16 Colour code. 449 * string Output of the RCon command 450 * @param p The packet that was just received. 451 */ 452 virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p); 453 454 /** 455 * Send an RCon command to the server: 456 * string RCon password. 457 * string Command to be executed. 458 * @param p The packet that was just received. 459 */ 460 virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p); 461 462 /** 463 * Sends information about all used GRFs to the client: 464 * uint8 Amount of GRFs (the following data is repeated this many times, i.e. per GRF data). 465 * uint32 GRF ID 466 * 16 * uint8 MD5 checksum of the GRF 467 * @param p The packet that was just received. 468 */ 469 virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p); 470 471 /** 472 * Tell the server that we have the required GRFs 473 * @param p The packet that was just received. 474 */ 475 virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p); 476 477 /** 478 * Move a client from one company into another: 479 * uint32 ID of the client. 480 * uint8 ID of the new company. 481 * @param p The packet that was just received. 482 */ 483 virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p); 484 485 /** 486 * Request the server to move this client into another company: 487 * uint8 ID of the company the client wants to join. 488 * string Password, if the company is password protected. 489 * @param p The packet that was just received. 490 */ 491 virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p); 492 493 /** 494 * Update the clients knowledge of which company is password protected: 495 * uint16 Bitwise representation of each company 496 * @param p The packet that was just received. 497 */ 498 virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p); 499 500 /** 501 * Update the clients knowledge of the max settings: 502 * uint8 Maximum number of companies allowed. 503 * uint8 Maximum number of spectators allowed. 504 * @param p The packet that was just received. 505 */ 506 virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p); 507 508 NetworkRecvStatus HandlePacket(Packet *p); 509 510 NetworkGameSocketHandler(SOCKET s); 511 public: 512 ClientID client_id; ///< Client identifier 513 uint32 last_frame; ///< Last frame we have executed 514 uint32 last_frame_server; ///< Last frame the server has executed 515 CommandQueue incoming_queue; ///< The command-queue awaiting handling 516 std::chrono::steady_clock::time_point last_packet; ///< Time we received the last frame. 517 518 NetworkRecvStatus CloseConnection(bool error = true) override; 519 520 /** 521 * Close the network connection due to the given status. 522 * @param status The reason the connection got closed. 523 */ 524 virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0; ~NetworkGameSocketHandler()525 virtual ~NetworkGameSocketHandler() {} 526 527 /** 528 * Sets the client info for this socket handler. 529 * @param info The new client info. 530 */ SetInfo(NetworkClientInfo * info)531 inline void SetInfo(NetworkClientInfo *info) 532 { 533 assert(info != nullptr && this->info == nullptr); 534 this->info = info; 535 } 536 537 /** 538 * Gets the client info of this socket handler. 539 * @return The client info. 540 */ GetInfo()541 inline NetworkClientInfo *GetInfo() const 542 { 543 return this->info; 544 } 545 546 NetworkRecvStatus ReceivePackets(); 547 548 const char *ReceiveCommand(Packet *p, CommandPacket *cp); 549 void SendCommand(Packet *p, const CommandPacket *cp); 550 }; 551 552 #endif /* NETWORK_CORE_TCP_GAME_H */ 553