# tcp_game.h

Go to the documentation of this file.

    00001 /* $Id: tcp_game.h 15242 2009-01-23 22:18:06Z rubidium $ */
    00002
    00007 #ifndef NETWORK_CORE_TCP_GAME_H
    00008 #define NETWORK_CORE_TCP_GAME_H
    00009
    00010 #ifdef ENABLE_NETWORK
    00011
    00012 #include "os_abstraction.h"
    00013 #include "tcp.h"
    00014 #include "packet.h"
    00015
    00022 enum {
    00023   PACKET_SERVER_FULL,
    00024   PACKET_SERVER_BANNED,
    00025   PACKET_CLIENT_JOIN,
    00026   PACKET_SERVER_ERROR,
    00027   PACKET_CLIENT_COMPANY_INFO,
    00028   PACKET_SERVER_COMPANY_INFO,
    00029   PACKET_SERVER_CLIENT_INFO,
    00030   PACKET_SERVER_NEED_PASSWORD,
    00031   PACKET_CLIENT_PASSWORD,
    00032   PACKET_SERVER_WELCOME,
    00033   PACKET_CLIENT_GETMAP,
    00034   PACKET_SERVER_WAIT,
    00035   PACKET_SERVER_MAP,
    00036   PACKET_CLIENT_MAP_OK,
    00037   PACKET_SERVER_JOIN,
    00038   PACKET_SERVER_FRAME,
    00039   PACKET_SERVER_SYNC,
    00040   PACKET_CLIENT_ACK,
    00041   PACKET_CLIENT_COMMAND,
    00042   PACKET_SERVER_COMMAND,
    00043   PACKET_CLIENT_CHAT,
    00044   PACKET_SERVER_CHAT,
    00045   PACKET_CLIENT_SET_PASSWORD,
    00046   PACKET_CLIENT_SET_NAME,
    00047   PACKET_CLIENT_QUIT,
    00048   PACKET_CLIENT_ERROR,
    00049   PACKET_SERVER_QUIT,
    00050   PACKET_SERVER_ERROR_QUIT,
    00051   PACKET_SERVER_SHUTDOWN,
    00052   PACKET_SERVER_NEWGAME,
    00053   PACKET_SERVER_RCON,
    00054   PACKET_CLIENT_RCON,
    00055   PACKET_SERVER_CHECK_NEWGRFS,
    00056   PACKET_CLIENT_NEWGRFS_CHECKED,
    00057   PACKET_SERVER_MOVE,
    00058   PACKET_CLIENT_MOVE,
    00059   PACKET_SERVER_COMPANY_UPDATE,
    00060   PACKET_SERVER_CONFIG_UPDATE,
    00061   PACKET_END
    00062 };
    00063
    00065 struct CommandPacket;
    00066
    00068 enum ClientStatus {
    00069   STATUS_INACTIVE,
    00070   STATUS_AUTHORIZING,
    00071   STATUS_AUTH,
    00072   STATUS_MAP_WAIT,
    00073   STATUS_MAP,
    00074   STATUS_DONE_MAP,
    00075   STATUS_PRE_ACTIVE,
    00076   STATUS_ACTIVE,
    00077 };
    00078
    00079
    00080 class NetworkClientSocket;
    00081 DECLARE_OLD_POOL(NetworkClientSocket, NetworkClientSocket, NCI_BITS_PER_POOL_BLOCK, MAX_CLIENT_SLOTS >> NCI_BITS_PER_POOL_BLOCK);
    00082
    00084 class NetworkClientSocket : public PoolItem<NetworkClientSocket, ClientIndex, &_NetworkClientSocket_pool>, public NetworkTCPSocketHandler {
    00085 /* TODO: rewrite into a proper class */
    00086 private:
    00087   NetworkClientInfo *info;
    00088 public:
    00089   ClientID client_id;
    00090   uint32 last_frame;
    00091   uint32 last_frame_server;
    00092   byte lag_test;
    00093
    00094   ClientStatus status;
    00095
    00096   CommandPacket *command_queue;
    00097
    00098   NetworkRecvStatus CloseConnection();
    00099
    00100   NetworkClientSocket(ClientID client_id = INVALID_CLIENT_ID);
    00101   ~NetworkClientSocket();
    00102
    00103   inline bool IsValid() const { return this->IsConnected(); }
    00104   inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
    00105   inline NetworkClientInfo *GetInfo() const { return this->info; }
    00106
    00107   const char *Recv_Command(Packet *p, CommandPacket *cp);
    00108   void Send_Command(Packet *p, const CommandPacket *cp);
    00109 };
    00110
    00111 static inline bool IsValidNetworkClientSocketIndex(ClientIndex index)
    00112 {
    00113   return (uint)index < GetNetworkClientSocketPoolSize() && GetNetworkClientSocket(index)->IsValid();
    00114 }
    00115
    00116 #define FOR_ALL_CLIENT_SOCKETS_FROM(d, start) for (d = GetNetworkClientSocket(start); d != NULL; d = (d->index + 1U < GetNetworkClientSocketPoolSize()) ? GetNetworkClientSocket(d->index + 1U) : NULL) if (d->IsValid())
    00117 #define FOR_ALL_CLIENT_SOCKETS(d) FOR_ALL_CLIENT_SOCKETS_FROM(d, 0)
    00118
    00119 #endif /* ENABLE_NETWORK */
    00120
    00121 #endif /* NETWORK_CORE_TCP_GAME_H */