# tcp_game.h

Go to the documentation of this file.

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