(svn r20923) -Codechange: prepare creating sub-classes of NetworkClientSocket for server and client side

This commit is contained in:
rubidium 2010-10-15 13:22:00 +00:00
parent 951b725b8c
commit 8eb07d097e
7 changed files with 73 additions and 42 deletions

View File

@ -18,7 +18,6 @@
#include "../network.h"
#include "../network_internal.h"
#include "../../core/pool_func.hpp"
#include "../../order_backup.h"
#include "table/strings.h"
@ -29,18 +28,16 @@ assert_compile(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENT_SLOTS);
NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket");
INSTANTIATE_POOL_METHODS(NetworkClientSocket)
NetworkClientSocket::NetworkClientSocket(ClientID client_id)
/**
* Create a new socket for the game connection.
* @param s The socket to connect with.
*/
NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s)
{
this->client_id = client_id;
this->status = STATUS_INACTIVE;
}
NetworkClientSocket::~NetworkClientSocket()
{
if (_redirect_console_to_client == this->client_id) _redirect_console_to_client = INVALID_CLIENT_ID;
if (_network_server) OrderBackup::ResetUser(this->client_id);
this->client_id = INVALID_CLIENT_ID;
this->status = STATUS_INACTIVE;
this->sock = s;
this->last_frame = _frame_counter;
this->last_frame_server = _frame_counter;
}
/**
@ -51,7 +48,7 @@ NetworkClientSocket::~NetworkClientSocket()
* @return the new status
* TODO: needs to be splitted when using client and server socket packets
*/
NetworkRecvStatus NetworkClientSocket::CloseConnection(bool error)
NetworkRecvStatus NetworkGameSocketHandler::CloseConnection(bool error)
{
/* Clients drop back to the main menu */
if (!_network_server && _networking) {

View File

@ -108,15 +108,18 @@ enum ClientStatus {
STATUS_END ///< Must ALWAYS be on the end of this list!! (period)
};
class NetworkClientSocket;
class NetworkGameSocketHandler;
typedef NetworkGameSocketHandler NetworkClientSocket;
typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS> NetworkClientSocketPool;
extern NetworkClientSocketPool _networkclientsocket_pool;
/** Base socket handler for all TCP sockets */
class NetworkClientSocket : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkTCPSocketHandler {
class NetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkTCPSocketHandler {
/* TODO: rewrite into a proper class */
private:
NetworkClientInfo *info; ///< Client info related to this socket
protected:
NetworkGameSocketHandler(SOCKET s);
public:
ClientID client_id; ///< Client identifier
uint32 last_frame; ///< Last frame we have executed
@ -129,9 +132,7 @@ public:
CommandQueue outgoing_queue; ///< The command-queue awaiting delivery
NetworkRecvStatus CloseConnection(bool error = true);
NetworkClientSocket(ClientID client_id = INVALID_CLIENT_ID);
~NetworkClientSocket();
virtual ~NetworkGameSocketHandler() {}
inline void SetInfo(NetworkClientInfo *info) { assert(info != NULL && this->info == NULL); this->info = info; }
inline NetworkClientInfo *GetInfo() const { return this->info; }

View File

@ -43,8 +43,6 @@
bool _ddc_fastforward = true;
#endif /* DEBUG_DUMP_COMMANDS */
DECLARE_POSTFIX_INCREMENT(ClientID)
assert_compile(NetworkClientInfoPool::MAX_SIZE == NetworkClientSocketPool::MAX_SIZE);
NetworkClientInfoPool _networkclientinfo_pool("NetworkClientInfo");
@ -94,8 +92,6 @@ static SocketList _listensockets;
/* The amount of clients connected */
static byte _network_clients_connected = 0;
/* The identifier counter for new clients (is never decreased) */
static ClientID _network_client_id = CLIENT_ID_FIRST;
/* Some externs / forwards */
extern void StateGameLoop();
@ -483,31 +479,19 @@ void ParseConnectionString(const char **company, const char **port, char *connec
* Used both by the server and the client */
static NetworkClientSocket *NetworkAllocClient(SOCKET s)
{
if (_network_server) {
/* Can we handle a new client? */
if (_network_clients_connected >= MAX_CLIENTS) return NULL;
if (_network_game_info.clients_on >= _settings_client.network.max_clients) return NULL;
/* Register the login */
_network_clients_connected++;
if (!_network_server) {
return new ClientNetworkGameSocketHandler(s);
}
NetworkClientSocket *cs = new NetworkClientSocket(INVALID_CLIENT_ID);
cs->sock = s;
cs->last_frame = _frame_counter;
cs->last_frame_server = _frame_counter;
/* Can we handle a new client? */
if (_network_clients_connected >= MAX_CLIENTS) return NULL;
if (_network_game_info.clients_on >= _settings_client.network.max_clients) return NULL;
if (_network_server) {
cs->client_id = _network_client_id++;
NetworkClientInfo *ci = new NetworkClientInfo(cs->client_id);
cs->SetInfo(ci);
ci->client_playas = COMPANY_INACTIVE_CLIENT;
ci->join_date = _date;
/* Register the login */
_network_clients_connected++;
SetWindowDirty(WC_CLIENT_LIST, 0);
}
return cs;
SetWindowDirty(WC_CLIENT_LIST, 0);
return new ServerNetworkGameSocketHandler(s);
}
/* Close a connection */

View File

@ -28,11 +28,19 @@
#include "../rev.h"
#include "network.h"
#include "network_base.h"
#include "network_client.h"
#include "table/strings.h"
/* This file handles all the client-commands */
/**
* Create a new socket for the client side of the game connection.
* @param s The socket to connect with.
*/
ClientNetworkGameSocketHandler::ClientNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
{
}
/* So we don't make too much typos ;) */
#define MY_CLIENT NetworkClientSocket::Get(0)

View File

@ -16,6 +16,12 @@
#include "network_internal.h"
/** Class for handling the client side of the game connection. */
class ClientNetworkGameSocketHandler : public NetworkGameSocketHandler {
public:
ClientNetworkGameSocketHandler(SOCKET s);
};
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GAME_INFO);
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO);
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMMAND)(const CommandPacket *cp);

View File

@ -30,12 +30,40 @@
#include "../company_gui.h"
#include "../window_func.h"
#include "../roadveh.h"
#include "../order_backup.h"
#include "../rev.h"
#include "table/strings.h"
/* This file handles all the server-commands */
DECLARE_POSTFIX_INCREMENT(ClientID)
/** The identifier counter for new clients (is never decreased) */
static ClientID _network_client_id = CLIENT_ID_FIRST;
/**
* Create a new socket for the server side of the game connection.
* @param s The socket to connect with.
*/
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
{
this->client_id = _network_client_id++;
NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
this->SetInfo(ci);
ci->client_playas = COMPANY_INACTIVE_CLIENT;
ci->join_date = _date;
}
/**
* Clear everything related to this client.
*/
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler()
{
if (_redirect_console_to_client == this->client_id) _redirect_console_to_client = INVALID_CLIENT_ID;
OrderBackup::ResetUser(this->client_id);
}
static void NetworkHandleCommandQueue(NetworkClientSocket *cs);
/***********

View File

@ -16,6 +16,13 @@
#include "network_internal.h"
/** Class for handling the server side of the game connection. */
class ServerNetworkGameSocketHandler : public NetworkGameSocketHandler {
public:
ServerNetworkGameSocketHandler(SOCKET s);
~ServerNetworkGameSocketHandler();
};
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP);
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkClientSocket *cs, ClientID client_id, NetworkErrorCode errorno);
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkClientSocket *cs, NetworkErrorCode error);