# tcp_game.h

Go to the documentation of this file.

    00001 /* $Id: tcp_game.h 19854 2010-05-18 21:25:03Z 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_NEWGRFS_CHECK,
    00081   STATUS_AUTH_GAME,
    00082   STATUS_AUTH_COMPANY,
    00083   STATUS_AUTHORIZED,
    00084   STATUS_MAP_WAIT,
    00085   STATUS_MAP,
    00086   STATUS_DONE_MAP,
    00087   STATUS_PRE_ACTIVE,
    00088   STATUS_ACTIVE,
    00089   STATUS_END
    00090 };
    00091
    00092 class NetworkClientSocket;
    00093 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS> NetworkClientSocketPool;
    00094 extern NetworkClientSocketPool _networkclientsocket_pool;
    00095
    00097 class NetworkClientSocket : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkTCPSocketHandler {
    00098 /* TODO: rewrite into a proper class */
    00099 private:
    00100   NetworkClientInfo *info;
    00101 public:
    00102   ClientID client_id;
    00103   uint32 last_frame;
    00104   uint32 last_frame_server;
    00105   byte lag_test;
    00106
    00107   ClientStatus status;
    00108
    00109   CommandPacket *command_queue;
    00110
    00111   NetworkRecvStatus CloseConnection(bool error = true);
    00112
    00113   NetworkClientSocket(ClientID client_id = INVALID_CLIENT_ID);
    00114   ~NetworkClientSocket();
    00115
    00116   inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
    00117   inline NetworkClientInfo *GetInfo() const { return this->info; }
    00118
    00119   const char *Recv_Command(Packet *p, CommandPacket *cp);
    00120   void Send_Command(Packet *p, const CommandPacket *cp);
    00121 };
    00122
    00123 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
    00124 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
    00125
    00126 #endif /* ENABLE_NETWORK */
    00127
    00128 #endif /* NETWORK_CORE_TCP_GAME_H */