(svn r20935) -Codechange: only let the server side use a pool of connected sockets

This commit is contained in:
rubidium 2010-10-15 19:58:56 +00:00
parent 04ce759165
commit 66087c5e60
8 changed files with 39 additions and 36 deletions

View File

@ -17,18 +17,10 @@
#include "../network.h" #include "../network.h"
#include "../network_internal.h" #include "../network_internal.h"
#include "../../core/pool_func.hpp"
#include "../../debug.h" #include "../../debug.h"
#include "table/strings.h" #include "table/strings.h"
/** Make very sure the preconditions given in network_type.h are actually followed */
assert_compile(MAX_CLIENT_SLOTS > MAX_CLIENTS);
assert_compile(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENT_SLOTS);
NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket");
INSTANTIATE_POOL_METHODS(NetworkClientSocket)
/** /**
* Create a new socket for the game connection. * Create a new socket for the game connection.
* @param s The socket to connect with. * @param s The socket to connect with.
@ -75,7 +67,7 @@ NetworkRecvStatus NetworkGameSocketHandler::CloseConnection(bool error)
* @param p the packet to handle * @param p the packet to handle
* @return #NetworkRecvStatus of handling. * @return #NetworkRecvStatus of handling.
*/ */
NetworkRecvStatus NetworkClientSocket::HandlePacket(Packet *p) NetworkRecvStatus NetworkGameSocketHandler::HandlePacket(Packet *p)
{ {
PacketGameType type = (PacketGameType)p->Recv_uint8(); PacketGameType type = (PacketGameType)p->Recv_uint8();
@ -140,7 +132,7 @@ NetworkRecvStatus NetworkClientSocket::HandlePacket(Packet *p)
* HandlePacket is returned. * HandlePacket is returned.
* @return #NetworkRecvStatus of the last handled packet. * @return #NetworkRecvStatus of the last handled packet.
*/ */
NetworkRecvStatus NetworkClientSocket::Recv_Packets() NetworkRecvStatus NetworkGameSocketHandler::Recv_Packets()
{ {
Packet *p; Packet *p;
while ((p = this->Recv_Packet()) != NULL) { while ((p = this->Recv_Packet()) != NULL) {
@ -158,7 +150,7 @@ NetworkRecvStatus NetworkClientSocket::Recv_Packets()
* @param type the packet type to create the stub for * @param type the packet type to create the stub for
*/ */
#define DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(type) \ #define DEFINE_UNAVAILABLE_GAME_RECEIVE_COMMAND(type) \
NetworkRecvStatus NetworkClientSocket::NetworkPacketReceive_## type ##_command(Packet *p) \ NetworkRecvStatus NetworkGameSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) \
{ \ { \
DEBUG(net, 0, "[tcp/game] received illegal packet type %d from client %d", \ DEBUG(net, 0, "[tcp/game] received illegal packet type %d from client %d", \
type, this->client_id); \ type, this->client_id); \

View File

@ -108,16 +108,11 @@ enum ClientStatus {
STATUS_END ///< Must ALWAYS be on the end of this list!! (period) STATUS_END ///< Must ALWAYS be on the end of this list!! (period)
}; };
class NetworkGameSocketHandler;
typedef NetworkGameSocketHandler NetworkClientSocket;
typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS> NetworkClientSocketPool;
extern NetworkClientSocketPool _networkclientsocket_pool;
#define DECLARE_GAME_RECEIVE_COMMAND(type) virtual NetworkRecvStatus NetworkPacketReceive_## type ##_command(Packet *p) #define DECLARE_GAME_RECEIVE_COMMAND(type) virtual NetworkRecvStatus NetworkPacketReceive_## type ##_command(Packet *p)
#define DEF_GAME_RECEIVE_COMMAND(cls, type) NetworkRecvStatus cls ##NetworkGameSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p) #define DEF_GAME_RECEIVE_COMMAND(cls, type) NetworkRecvStatus cls ##NetworkGameSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p)
/** Base socket handler for all TCP sockets */ /** Base socket handler for all TCP sockets */
class NetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkTCPSocketHandler { class NetworkGameSocketHandler : public NetworkTCPSocketHandler {
/* TODO: rewrite into a proper class */ /* TODO: rewrite into a proper class */
private: private:
NetworkClientInfo *info; ///< Client info related to this socket NetworkClientInfo *info; ///< Client info related to this socket
@ -191,9 +186,6 @@ public:
void Send_Command(Packet *p, const CommandPacket *cp); void Send_Command(Packet *p, const CommandPacket *cp);
}; };
#define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
#define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
#endif /* ENABLE_NETWORK */ #endif /* ENABLE_NETWORK */
#endif /* NETWORK_CORE_TCP_GAME_H */ #endif /* NETWORK_CORE_TCP_GAME_H */

View File

@ -514,25 +514,23 @@ static void InitializeNetworkPools()
} }
/* Close all current connections */ /* Close all current connections */
static void NetworkClose() void NetworkClose()
{ {
NetworkClientSocket *cs;
FOR_ALL_CLIENT_SOCKETS(cs) {
if (!_network_server) {
MyClient::SendQuit();
cs->Send_Packets();
}
cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
}
if (_network_server) { if (_network_server) {
NetworkClientSocket *cs;
FOR_ALL_CLIENT_SOCKETS(cs) {
cs->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
}
/* We are a server, also close the listensocket */ /* We are a server, also close the listensocket */
for (SocketList::iterator s = _listensockets.Begin(); s != _listensockets.End(); s++) { for (SocketList::iterator s = _listensockets.Begin(); s != _listensockets.End(); s++) {
closesocket(s->second); closesocket(s->second);
} }
_listensockets.Clear(); _listensockets.Clear();
DEBUG(net, 1, "[tcp] closed listeners"); DEBUG(net, 1, "[tcp] closed listeners");
} else if (MyClient::my_client != NULL) {
MyClient::SendQuit();
MyClient::my_client->Send_Packets();
MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CONN_LOST);
} }
TCPConnecter::KillAll(); TCPConnecter::KillAll();

View File

@ -17,8 +17,10 @@
#include "network_internal.h" #include "network_internal.h"
/** Class for handling the client side of the game connection. */ /** Class for handling the client side of the game connection. */
class ClientNetworkGameSocketHandler : public NetworkGameSocketHandler { class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
protected: protected:
friend void NetworkExecuteLocalCommandQueue();
friend void NetworkClose();
static ClientNetworkGameSocketHandler *my_client; static ClientNetworkGameSocketHandler *my_client;
DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_FULL); DECLARE_GAME_RECEIVE_COMMAND(PACKET_SERVER_FULL);

View File

@ -14,6 +14,7 @@
#include "../stdafx.h" #include "../stdafx.h"
#include "../debug.h" #include "../debug.h"
#include "network_client.h" #include "network_client.h"
#include "network_server.h"
#include "network.h" #include "network.h"
#include "../command_func.h" #include "../command_func.h"
#include "../company_func.h" #include "../company_func.h"
@ -179,7 +180,7 @@ void NetworkExecuteLocalCommandQueue()
{ {
assert(IsLocalCompany()); assert(IsLocalCompany());
CommandQueue &queue = (_network_server ? _local_execution_queue : NetworkClientSocket::Get(0)->incoming_queue); CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
CommandPacket *cp; CommandPacket *cp;
while ((cp = queue.Peek()) != NULL) { while ((cp = queue.Peek()) != NULL) {
@ -274,7 +275,7 @@ void NetworkDistributeCommands()
* @param cp the struct to write the data to. * @param cp the struct to write the data to.
* @return an error message. When NULL there has been no error. * @return an error message. When NULL there has been no error.
*/ */
const char *NetworkClientSocket::Recv_Command(Packet *p, CommandPacket *cp) const char *NetworkGameSocketHandler::Recv_Command(Packet *p, CommandPacket *cp)
{ {
cp->company = (CompanyID)p->Recv_uint8(); cp->company = (CompanyID)p->Recv_uint8();
cp->cmd = p->Recv_uint32(); cp->cmd = p->Recv_uint32();
@ -299,7 +300,7 @@ const char *NetworkClientSocket::Recv_Command(Packet *p, CommandPacket *cp)
* @param p the packet to send it in. * @param p the packet to send it in.
* @param cp the packet to actually send. * @param cp the packet to actually send.
*/ */
void NetworkClientSocket::Send_Command(Packet *p, const CommandPacket *cp) void NetworkGameSocketHandler::Send_Command(Packet *p, const CommandPacket *cp)
{ {
p->Send_uint8 (cp->company); p->Send_uint8 (cp->company);
p->Send_uint32(cp->cmd); p->Send_uint32(cp->cmd);

View File

@ -50,6 +50,8 @@ extern bool _ddc_fastforward;
#define _ddc_fastforward (false) #define _ddc_fastforward (false)
#endif /* DEBUG_DUMP_COMMANDS */ #endif /* DEBUG_DUMP_COMMANDS */
typedef class ServerNetworkGameSocketHandler NetworkClientSocket;
enum MapPacket { enum MapPacket {
MAP_PACKET_START, MAP_PACKET_START,
MAP_PACKET_NORMAL, MAP_PACKET_NORMAL,

View File

@ -31,6 +31,7 @@
#include "../window_func.h" #include "../window_func.h"
#include "../roadveh.h" #include "../roadveh.h"
#include "../order_backup.h" #include "../order_backup.h"
#include "../core/pool_func.hpp"
#include "../rev.h" #include "../rev.h"
#include "table/strings.h" #include "table/strings.h"
@ -41,6 +42,13 @@ DECLARE_POSTFIX_INCREMENT(ClientID)
/** The identifier counter for new clients (is never decreased) */ /** The identifier counter for new clients (is never decreased) */
static ClientID _network_client_id = CLIENT_ID_FIRST; static ClientID _network_client_id = CLIENT_ID_FIRST;
/** Make very sure the preconditions given in network_type.h are actually followed */
assert_compile(MAX_CLIENT_SLOTS > MAX_CLIENTS);
assert_compile(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENT_SLOTS);
NetworkClientSocketPool _networkclientsocket_pool("NetworkClientSocket");
INSTANTIATE_POOL_METHODS(NetworkClientSocket)
/** /**
* Create a new socket for the server side of the game connection. * Create a new socket for the server side of the game connection.
* @param s The socket to connect with. * @param s The socket to connect with.

View File

@ -16,8 +16,13 @@
#include "network_internal.h" #include "network_internal.h"
class ServerNetworkGameSocketHandler;
typedef ServerNetworkGameSocketHandler NetworkClientSocket;
typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS> NetworkClientSocketPool;
extern NetworkClientSocketPool _networkclientsocket_pool;
/** Class for handling the server side of the game connection. */ /** Class for handling the server side of the game connection. */
class ServerNetworkGameSocketHandler : public NetworkGameSocketHandler { class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler {
protected: protected:
DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_JOIN); DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_JOIN);
DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO); DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO);
@ -54,6 +59,9 @@ DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_MOVE)(NetworkClientSocket *cs, uint1
void NetworkServer_ReadPackets(NetworkClientSocket *cs); void NetworkServer_ReadPackets(NetworkClientSocket *cs);
void NetworkServer_Tick(bool send_frame); void NetworkServer_Tick(bool send_frame);
#define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
#define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
#else /* ENABLE_NETWORK */ #else /* ENABLE_NETWORK */
/* Network function stubs when networking is disabled */ /* Network function stubs when networking is disabled */