# tcp_game.h

Go to the documentation of this file.

    00001 /* $Id: tcp_game.h 19624 2010-04-13 21:40:24Z rubidium $ */
    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
    00030 enum {
    00031   PACKET_SERVER_FULL,
    00032   PACKET_SERVER_BANNED,
    00033   PACKET_CLIENT_JOIN,
    00034   PACKET_SERVER_ERROR,
    00035   PACKET_CLIENT_COMPANY_INFO,
    00036   PACKET_SERVER_COMPANY_INFO,
    00037   PACKET_SERVER_CLIENT_INFO,
    00038   PACKET_SERVER_NEED_GAME_PASSWORD,
    00039   PACKET_SERVER_NEED_COMPANY_PASSWORD,
    00040   PACKET_CLIENT_GAME_PASSWORD,
    00041   PACKET_CLIENT_COMPANY_PASSWORD,
    00042   PACKET_SERVER_WELCOME,
    00043   PACKET_CLIENT_GETMAP,
    00044   PACKET_SERVER_WAIT,
    00045   PACKET_SERVER_MAP,
    00046   PACKET_CLIENT_MAP_OK,
    00047   PACKET_SERVER_JOIN,
    00048   PACKET_SERVER_FRAME,
    00049   PACKET_SERVER_SYNC,
    00050   PACKET_CLIENT_ACK,
    00051   PACKET_CLIENT_COMMAND,
    00052   PACKET_SERVER_COMMAND,
    00053   PACKET_CLIENT_CHAT,
    00054   PACKET_SERVER_CHAT,
    00055   PACKET_CLIENT_SET_PASSWORD,
    00056   PACKET_CLIENT_SET_NAME,
    00057   PACKET_CLIENT_QUIT,
    00058   PACKET_CLIENT_ERROR,
    00059   PACKET_SERVER_QUIT,
    00060   PACKET_SERVER_ERROR_QUIT,
    00061   PACKET_SERVER_SHUTDOWN,
    00062   PACKET_SERVER_NEWGAME,
    00063   PACKET_SERVER_RCON,
    00064   PACKET_CLIENT_RCON,
    00065   PACKET_SERVER_CHECK_NEWGRFS,
    00066   PACKET_CLIENT_NEWGRFS_CHECKED,
    00067   PACKET_SERVER_MOVE,
    00068   PACKET_CLIENT_MOVE,
    00069   PACKET_SERVER_COMPANY_UPDATE,
    00070   PACKET_SERVER_CONFIG_UPDATE,
    00071   PACKET_END
    00072 };
    00073
    00075 struct CommandPacket;
    00076
    00078 enum ClientStatus {
    00079   STATUS_INACTIVE,
    00080   STATUS_AUTH_GAME,
    00081   STATUS_AUTH_COMPANY,
    00082   STATUS_AUTHORIZED,
    00083   STATUS_MAP_WAIT,
    00084   STATUS_MAP,
    00085   STATUS_DONE_MAP,
    00086   STATUS_PRE_ACTIVE,
    00087   STATUS_ACTIVE,
    00088 };
    00089
    00090 class NetworkClientSocket;
    00091 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS> NetworkClientSocketPool;
    00092 extern NetworkClientSocketPool _networkclientsocket_pool;
    00093
    00095 class NetworkClientSocket : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkTCPSocketHandler {
    00096 /* TODO: rewrite into a proper class */
    00097 private:
    00098   NetworkClientInfo *info;
    00099 public:
    00100   ClientID client_id;
    00101   uint32 last_frame;
    00102   uint32 last_frame_server;
    00103   byte lag_test;
    00104
    00105   ClientStatus status;
    00106
    00107   CommandPacket *command_queue;
    00108
    00109   NetworkRecvStatus CloseConnection(bool error = true);
    00110
    00111   NetworkClientSocket(ClientID client_id = INVALID_CLIENT_ID);
    00112   ~NetworkClientSocket();
    00113
    00114   inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
    00115   inline NetworkClientInfo *GetInfo() const { return this->info; }
    00116
    00117   const char *Recv_Command(Packet *p, CommandPacket *cp);
    00118   void Send_Command(Packet *p, const CommandPacket *cp);
    00119 };
    00120
    00121 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
    00122 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
    00123
    00124 #endif /* ENABLE_NETWORK */
    00125
    00126 #endif /* NETWORK_CORE_TCP_GAME_H */