# tcp_game.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00014 #ifndef NETWORK_CORE_TCP_GAME_H
    00015 #define NETWORK_CORE_TCP_GAME_H
    00016
    00017 #include "os_abstraction.h"
    00018 #include "tcp.h"
    00019 #include "../network_type.h"
    00020 #include "../../core/pool_type.hpp"
    00021
    00022 #ifdef ENABLE_NETWORK
    00023
    00028 enum PacketGameType {
    00029   /*
    00030    * These first three pair of packets (thus six in
    00031    * total) must remain in this order for backward
    00032    * and forward compatability between clients that
    00033    * are trying to join directly.
    00034    */
    00035
    00036   /* Packets sent by socket accepting code without ever constructing a client socket instance. */
    00037   PACKET_SERVER_FULL,
    00038   PACKET_SERVER_BANNED,
    00039
    00040   /* Packets used by the client to join and an error message when the revision is wrong. */
    00041   PACKET_CLIENT_JOIN,
    00042   PACKET_SERVER_ERROR,
    00043
    00044   /* Packets used for the pre-game lobby. */
    00045   PACKET_CLIENT_COMPANY_INFO,
    00046   PACKET_SERVER_COMPANY_INFO,
    00047
    00048   /*
    00049    * Packets after here assume that the client
    00050    * and server are running the same version. As
    00051    * such ordering is unimportant from here on.
    00052    *
    00053    * The following is the remainder of the packets
    00054    * sent as part of authenticating and getting
    00055    * the map and other important data.
    00056    */
    00057
    00058   /* After the join step, the first is checking NewGRFs. */
    00059   PACKET_SERVER_CHECK_NEWGRFS,
    00060   PACKET_CLIENT_NEWGRFS_CHECKED,
    00061
    00062   /* Checking the game, and then company passwords. */
    00063   PACKET_SERVER_NEED_GAME_PASSWORD,
    00064   PACKET_CLIENT_GAME_PASSWORD,
    00065   PACKET_SERVER_NEED_COMPANY_PASSWORD,
    00066   PACKET_CLIENT_COMPANY_PASSWORD,
    00067
    00068   /* The server welcomes the authenticated client and sends information of other clients. */
    00069   PACKET_SERVER_WELCOME,
    00070   PACKET_SERVER_CLIENT_INFO,
    00071
    00072   /* Getting the savegame/map. */
    00073   PACKET_CLIENT_GETMAP,
    00074   PACKET_SERVER_WAIT,
    00075   PACKET_SERVER_MAP_BEGIN,
    00076   PACKET_SERVER_MAP_SIZE,
    00077   PACKET_SERVER_MAP_DATA,
    00078   PACKET_SERVER_MAP_DONE,
    00079   PACKET_CLIENT_MAP_OK,
    00080
    00081   PACKET_SERVER_JOIN,
    00082
    00083   /*
    00084    * At this moment the client has the map and
    00085    * the client is fully authenticated. Now the
    00086    * normal communication starts.
    00087    */
    00088
    00089   /* Game progress monitoring. */
    00090   PACKET_SERVER_FRAME,
    00091   PACKET_CLIENT_ACK,
    00092   PACKET_SERVER_SYNC,
    00093
    00094   /* Sending commands around. */
    00095   PACKET_CLIENT_COMMAND,
    00096   PACKET_SERVER_COMMAND,
    00097
    00098   /* Human communication! */
    00099   PACKET_CLIENT_CHAT,
    00100   PACKET_SERVER_CHAT,
    00101
    00102   /* Remote console. */
    00103   PACKET_CLIENT_RCON,
    00104   PACKET_SERVER_RCON,
    00105
    00106   /* Moving a client.*/
    00107   PACKET_CLIENT_MOVE,
    00108   PACKET_SERVER_MOVE,
    00109
    00110   /* Configuration updates. */
    00111   PACKET_CLIENT_SET_PASSWORD,
    00112   PACKET_CLIENT_SET_NAME,
    00113   PACKET_SERVER_COMPANY_UPDATE,
    00114   PACKET_SERVER_CONFIG_UPDATE,
    00115
    00116   /* A server quitting this game. */
    00117   PACKET_SERVER_NEWGAME,
    00118   PACKET_SERVER_SHUTDOWN,
    00119
    00120   /* A client quitting. */
    00121   PACKET_CLIENT_QUIT,
    00122   PACKET_SERVER_QUIT,
    00123   PACKET_CLIENT_ERROR,
    00124   PACKET_SERVER_ERROR_QUIT,
    00125
    00126   PACKET_END
    00127 };
    00128
    00130 struct CommandPacket;
    00131
    00133 class CommandQueue {
    00134   CommandPacket *first;
    00135   CommandPacket *last;
    00136   uint count;
    00137
    00138 public:
    00140   CommandQueue() : first(NULL), last(NULL) {}
    00142   ~CommandQueue() { this->Free(); }
    00143   void Append(CommandPacket *p);
    00144   CommandPacket *Pop(bool ignore_paused = false);
    00145   CommandPacket *Peek(bool ignore_paused = false);
    00146   void Free();
    00148   uint Count() const { return this->count; }
    00149 };
    00150
    00151 #define DECLARE_GAME_RECEIVE_COMMAND(type) virtual NetworkRecvStatus NetworkPacketReceive_## type ##_command(Packet *p)
    00152 #define DEF_GAME_RECEIVE_COMMAND(cls, type) NetworkRecvStatus cls ##NetworkGameSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p)
    00153
    00155 class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
    00156 /* TODO: rewrite into a proper class */
    00157 private:
    00158   NetworkClientInfo *info;
    00159
    00160 protected:
    00161
    00165   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_FULL);
    00166
    00170   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_BANNED);
    00171
    00179   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_JOIN);
    00180
    00185   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_ERROR);
    00186
    00190   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO);
    00191
    00217   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO);
    00218
    00225   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO);
    00226
    00230   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_NEED_GAME_PASSWORD);
    00231
    00237   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_NEED_COMPANY_PASSWORD);
    00238
    00244   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_GAME_PASSWORD);
    00245
    00251   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD);
    00252
    00259   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_WELCOME);
    00260
    00265   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP);
    00266
    00271   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_WAIT);
    00272
    00277   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_BEGIN);
    00278
    00283   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_SIZE);
    00284
    00289   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_DATA);
    00290
    00294   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MAP_DONE);
    00295
    00299   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK);
    00300
    00305   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_JOIN);
    00306
    00315   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_FRAME);
    00316
    00323   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_SYNC);
    00324
    00330   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_ACK);
    00331
    00342   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND);
    00343
    00355   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_COMMAND);
    00356
    00365   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_CHAT);
    00366
    00374   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CHAT);
    00375
    00380   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD);
    00381
    00386   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME);
    00387
    00391   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_QUIT);
    00392
    00397   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_ERROR);
    00398
    00403   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_QUIT);
    00404
    00410   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT);
    00411
    00415   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN);
    00416
    00420   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME);
    00421
    00427   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_RCON);
    00428
    00434   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_RCON);
    00435
    00442   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS);
    00443
    00447   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED);
    00448
    00454   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_MOVE);
    00455
    00461   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_MOVE);
    00462
    00467   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_UPDATE);
    00468
    00474   DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_CONFIG_UPDATE);
    00475
    00476   NetworkRecvStatus HandlePacket(Packet *p);
    00477
    00478   NetworkGameSocketHandler(SOCKET s);
    00479 public:
    00480   ClientID client_id;
    00481   uint32 last_frame;
    00482   uint32 last_frame_server;
    00483   CommandQueue incoming_queue;
    00484   uint last_packet;
    00485
    00486   NetworkRecvStatus CloseConnection(bool error = true);
    00487   virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
    00488   virtual ~NetworkGameSocketHandler() {}
    00489
    00490   inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
    00491   inline NetworkClientInfo *GetInfo() const { return this->info; }
    00492
    00493   NetworkRecvStatus ReceivePackets();
    00494
    00495   const char *ReceiveCommand(Packet *p, CommandPacket *cp);
    00496   void SendCommand(Packet *p, const CommandPacket *cp);
    00497 };
    00498
    00499 #endif /* ENABLE_NETWORK */
    00500
    00501 #endif /* NETWORK_CORE_TCP_GAME_H */