# 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 compatibility 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
    00152 class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
    00153 /* TODO: rewrite into a proper class */
    00154 private:
    00155   NetworkClientInfo *info;
    00156
    00157 protected:
    00158   NetworkRecvStatus ReceiveInvalidPacket(PacketGameType type);
    00159
    00164   virtual NetworkRecvStatus Receive_SERVER_FULL(Packet *p);
    00165
    00170   virtual NetworkRecvStatus Receive_SERVER_BANNED(Packet *p);
    00171
    00180   virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
    00181
    00187   virtual NetworkRecvStatus Receive_SERVER_ERROR(Packet *p);
    00188
    00193   virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
    00194
    00221   virtual NetworkRecvStatus Receive_SERVER_COMPANY_INFO(Packet *p);
    00222
    00230   virtual NetworkRecvStatus Receive_SERVER_CLIENT_INFO(Packet *p);
    00231
    00236   virtual NetworkRecvStatus Receive_SERVER_NEED_GAME_PASSWORD(Packet *p);
    00237
    00244   virtual NetworkRecvStatus Receive_SERVER_NEED_COMPANY_PASSWORD(Packet *p);
    00245
    00252   virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
    00253
    00260   virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
    00261
    00269   virtual NetworkRecvStatus Receive_SERVER_WELCOME(Packet *p);
    00270
    00276   virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
    00277
    00283   virtual NetworkRecvStatus Receive_SERVER_WAIT(Packet *p);
    00284
    00290   virtual NetworkRecvStatus Receive_SERVER_MAP_BEGIN(Packet *p);
    00291
    00297   virtual NetworkRecvStatus Receive_SERVER_MAP_SIZE(Packet *p);
    00298
    00304   virtual NetworkRecvStatus Receive_SERVER_MAP_DATA(Packet *p);
    00305
    00310   virtual NetworkRecvStatus Receive_SERVER_MAP_DONE(Packet *p);
    00311
    00316   virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
    00317
    00323   virtual NetworkRecvStatus Receive_SERVER_JOIN(Packet *p);
    00324
    00334   virtual NetworkRecvStatus Receive_SERVER_FRAME(Packet *p);
    00335
    00343   virtual NetworkRecvStatus Receive_SERVER_SYNC(Packet *p);
    00344
    00351   virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
    00352
    00364   virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
    00365
    00378   virtual NetworkRecvStatus Receive_SERVER_COMMAND(Packet *p);
    00379
    00389   virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
    00390
    00399   virtual NetworkRecvStatus Receive_SERVER_CHAT(Packet *p);
    00400
    00406   virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
    00407
    00413   virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
    00414
    00419   virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
    00420
    00426   virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
    00427
    00433   virtual NetworkRecvStatus Receive_SERVER_QUIT(Packet *p);
    00434
    00441   virtual NetworkRecvStatus Receive_SERVER_ERROR_QUIT(Packet *p);
    00442
    00447   virtual NetworkRecvStatus Receive_SERVER_SHUTDOWN(Packet *p);
    00448
    00453   virtual NetworkRecvStatus Receive_SERVER_NEWGAME(Packet *p);
    00454
    00461   virtual NetworkRecvStatus Receive_SERVER_RCON(Packet *p);
    00462
    00469   virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
    00470
    00478   virtual NetworkRecvStatus Receive_SERVER_CHECK_NEWGRFS(Packet *p);
    00479
    00484   virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
    00485
    00492   virtual NetworkRecvStatus Receive_SERVER_MOVE(Packet *p);
    00493
    00500   virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
    00501
    00507   virtual NetworkRecvStatus Receive_SERVER_COMPANY_UPDATE(Packet *p);
    00508
    00515   virtual NetworkRecvStatus Receive_SERVER_CONFIG_UPDATE(Packet *p);
    00516
    00517   NetworkRecvStatus HandlePacket(Packet *p);
    00518
    00519   NetworkGameSocketHandler(SOCKET s);
    00520 public:
    00521   ClientID client_id;
    00522   uint32 last_frame;
    00523   uint32 last_frame_server;
    00524   CommandQueue incoming_queue;
    00525   uint last_packet;
    00526
    00527   NetworkRecvStatus CloseConnection(bool error = true);
    00528
    00533   virtual NetworkRecvStatus CloseConnection(NetworkRecvStatus status) = 0;
    00534   virtual ~NetworkGameSocketHandler() {}
    00535
    00540   inline void SetInfo(NetworkClientInfo *info)
    00541   {
    00542     assert(info != NULL && this->info == NULL);
    00543     this->info = info;
    00544   }
    00545
    00550   inline NetworkClientInfo *GetInfo() const
    00551   {
    00552     return this->info;
    00553   }
    00554
    00555   NetworkRecvStatus ReceivePackets();
    00556
    00557   const char *ReceiveCommand(Packet *p, CommandPacket *cp);
    00558   void SendCommand(Packet *p, const CommandPacket *cp);
    00559 };
    00560
    00561 #endif /* ENABLE_NETWORK */
    00562
    00563 #endif /* NETWORK_CORE_TCP_GAME_H */