(svn r14730) -Codechange: remove the need for networkclientsockets and networkclientinfo structs to be in a contiguous piece of memory and put them in a pool.

-Note: 255 should really be enough for now... making it any more means network protocol bumps.
This commit is contained in:
rubidium
2008-12-23 20:52:27 +00:00
parent 258ebb8173
commit 54f23ed0b3
9 changed files with 89 additions and 135 deletions

View File

@@ -16,30 +16,25 @@
#include "tcp.h"
#include "table/strings.h"
#include "../../oldpool_func.h"
/** Very ugly temporary hack !!! */
void NetworkClientSocket::Initialize()
/** Make very sure the preconditions given in network_type.h are actually followed */
assert_compile(MAX_CLIENT_SLOTS == (MAX_CLIENT_SLOTS >> NCI_BITS_PER_POOL_BLOCK) << NCI_BITS_PER_POOL_BLOCK);
assert_compile(MAX_CLIENT_SLOTS > MAX_CLIENTS);
typedef ClientIndex NetworkClientSocketID;
DEFINE_OLD_POOL_GENERIC(NetworkClientSocket, NetworkClientSocket);
NetworkClientSocket::NetworkClientSocket(ClientID client_id)
{
this->sock = INVALID_SOCKET;
this->client_id = INVALID_CLIENT_ID;
this->last_frame = 0;
this->last_frame_server = 0;
this->lag_test = 0;
this->client_id = client_id;
this->status = STATUS_INACTIVE;
this->has_quit = false;
this->writable = false;
this->packet_queue = NULL;
this->packet_recv = NULL;
this->command_queue = NULL;
}
void NetworkClientSocket::Destroy()
NetworkClientSocket::~NetworkClientSocket()
{
closesocket(this->sock);
if (this->sock != INVALID_SOCKET) closesocket(this->sock);
this->writable = false;
this->has_quit = true;
@@ -57,6 +52,10 @@ void NetworkClientSocket::Destroy()
free(this->command_queue);
this->command_queue = p;
}
this->sock = INVALID_SOCKET;
this->client_id = INVALID_CLIENT_ID;
this->status = STATUS_INACTIVE;
}
/**

View File

@@ -84,12 +84,17 @@ enum ClientStatus {
STATUS_ACTIVE, ///< The client is active within in the game
};
class NetworkClientSocket;
DECLARE_OLD_POOL(NetworkClientSocket, NetworkClientSocket, NCI_BITS_PER_POOL_BLOCK, MAX_CLIENT_SLOTS >> NCI_BITS_PER_POOL_BLOCK);
/** Base socket handler for all TCP sockets */
class NetworkClientSocket : public NetworkSocketHandler {
class NetworkClientSocket : public PoolItem<NetworkClientSocket, ClientIndex, &_NetworkClientSocket_pool>, public NetworkSocketHandler {
/* TODO: rewrite into a proper class */
private:
Packet *packet_queue; ///< Packets that are awaiting delivery
Packet *packet_recv; ///< Partially received packet
NetworkClientInfo *info; ///< Client info related to this socket
public:
ClientID client_id; ///< Client identifier
uint32 last_frame; ///< Last frame we have executed
@@ -102,8 +107,6 @@ public:
CommandPacket *command_queue; ///< The command-queue awaiting delivery
NetworkRecvStatus CloseConnection();
void Initialize();
void Destroy();
void Send_Packet(Packet *packet);
bool Send_Packets();
@@ -111,27 +114,24 @@ public:
Packet *Recv_Packet(NetworkRecvStatus *status);
inline bool IsValid() const { return this->IsConnected(); }
inline NetworkClientInfo *GetInfo() const
{
extern NetworkClientSocket _clients[MAX_CLIENTS];
return GetNetworkClientInfo(this - _clients);
}
};
NetworkClientSocket(ClientID client_id = INVALID_CLIENT_ID);
~NetworkClientSocket();
// Here we keep track of the clients
// (and the client uses [0] for his own communication)
extern NetworkClientSocket _clients[MAX_CLIENTS];
#define GetNetworkClientSocket(i) (&_clients[i])
inline bool IsValid() const { return this->IsConnected(); }
inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
inline NetworkClientInfo *GetInfo() const { return this->info; }
};
static inline bool IsValidNetworkClientSocketIndex(ClientIndex index)
{
return (uint)index < MAX_CLIENTS && GetNetworkClientSocket(index)->IsValid();
return (uint)index < GetNetworkClientSocketPoolSize() && GetNetworkClientSocket(index)->IsValid();
}
#define FOR_ALL_CLIENT_SOCKETS_FROM(d, start) for (d = GetNetworkClientSocket(start); d != GetNetworkClientSocket(MAX_CLIENTS); d++) if (d->IsValid())
#define FOR_ALL_CLIENT_SOCKETS_FROM(d, start) for (d = (start < GetNetworkClientSocketPoolSize() ? GetNetworkClientSocket(start) : NULL); d != NULL; d = (d->index + 1U < GetNetworkClientSocketPoolSize()) ? GetNetworkClientSocket(d->index + 1U) : NULL) if (d->IsValid())
#define FOR_ALL_CLIENT_SOCKETS(d) FOR_ALL_CLIENT_SOCKETS_FROM(d, 0)
typedef NetworkClientSocket NetworkTCPSocketHandler;
#endif /* ENABLE_NETWORK */
#endif /* NETWORK_CORE_TCP_H */