1
/* $Id: tcp_game.h 15903 2009-03-30 23:15:05Z rubidium $ */
4
* @file tcp_game.h Basic functions to receive and send TCP packets for game purposes.
7
#ifndef NETWORK_CORE_TCP_GAME_H
8
#define NETWORK_CORE_TCP_GAME_H
12
#include "os_abstraction.h"
17
* Enum with all types of UDP packets.
18
* The order of the first 4 packets MUST not be changed, as
19
* it protects old clients from joining newer servers
20
* (because SERVER_ERROR is the respond to a wrong revision)
27
PACKET_CLIENT_COMPANY_INFO,
28
PACKET_SERVER_COMPANY_INFO,
29
PACKET_SERVER_CLIENT_INFO,
30
PACKET_SERVER_NEED_PASSWORD,
31
PACKET_CLIENT_PASSWORD,
32
PACKET_SERVER_WELCOME,
41
PACKET_CLIENT_COMMAND,
42
PACKET_SERVER_COMMAND,
45
PACKET_CLIENT_SET_PASSWORD,
46
PACKET_CLIENT_SET_NAME,
50
PACKET_SERVER_ERROR_QUIT,
51
PACKET_SERVER_SHUTDOWN,
52
PACKET_SERVER_NEWGAME,
55
PACKET_SERVER_CHECK_NEWGRFS,
56
PACKET_CLIENT_NEWGRFS_CHECKED,
59
PACKET_SERVER_COMPANY_UPDATE,
60
PACKET_SERVER_CONFIG_UPDATE,
61
PACKET_END ///< Must ALWAYS be on the end of this list!! (period)
64
/** Packet that wraps a command */
67
/** Status of a client */
69
STATUS_INACTIVE, ///< The client is not connected nor active
70
STATUS_AUTHORIZING,///< The client is authorizing
71
STATUS_AUTH, ///< The client is authorized
72
STATUS_MAP_WAIT, ///< The client is waiting as someone else is downloading the map
73
STATUS_MAP, ///< The client is downloading the map
74
STATUS_DONE_MAP, ///< The client has downloaded the map
75
STATUS_PRE_ACTIVE, ///< The client is catching up the delayed frames
76
STATUS_ACTIVE, ///< The client is active within in the game
80
class NetworkClientSocket;
81
DECLARE_OLD_POOL(NetworkClientSocket, NetworkClientSocket, NCI_BITS_PER_POOL_BLOCK, MAX_CLIENT_SLOTS >> NCI_BITS_PER_POOL_BLOCK);
83
/** Base socket handler for all TCP sockets */
84
class NetworkClientSocket : public PoolItem<NetworkClientSocket, ClientIndex, &_NetworkClientSocket_pool>, public NetworkTCPSocketHandler {
85
/* TODO: rewrite into a proper class */
87
NetworkClientInfo *info; ///< Client info related to this socket
89
ClientID client_id; ///< Client identifier
90
uint32 last_frame; ///< Last frame we have executed
91
uint32 last_frame_server; ///< Last frame the server has executed
92
byte lag_test; ///< Byte used for lag-testing the client
94
ClientStatus status; ///< Status of this client
96
CommandPacket *command_queue; ///< The command-queue awaiting delivery
98
NetworkRecvStatus CloseConnection();
100
NetworkClientSocket(ClientID client_id = INVALID_CLIENT_ID);
101
~NetworkClientSocket();
103
inline bool IsValid() const { return this->IsConnected(); }
104
inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
105
inline NetworkClientInfo *GetInfo() const { return this->info; }
107
const char *Recv_Command(Packet *p, CommandPacket *cp);
108
void Send_Command(Packet *p, const CommandPacket *cp);
111
static inline bool IsValidNetworkClientSocketIndex(ClientIndex index)
113
return (uint)index < GetNetworkClientSocketPoolSize() && GetNetworkClientSocket(index)->IsValid();
116
#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())
117
#define FOR_ALL_CLIENT_SOCKETS(d) FOR_ALL_CLIENT_SOCKETS_FROM(d, 0)
119
#endif /* ENABLE_NETWORK */
121
#endif /* NETWORK_CORE_TCP_GAME_H */