summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-10-15 13:22:00 +0000
committerrubidium <rubidium@openttd.org>2010-10-15 13:22:00 +0000
commit8eb07d097e2ab53f0b41a6b7af541c4937732e0b (patch)
tree6722210c26413da2881daeff5f1a8d387814c450 /src/network
parent951b725b8c001003b38f0b009b6d6df55cb6ec02 (diff)
downloadopenttd-8eb07d097e2ab53f0b41a6b7af541c4937732e0b.tar.xz
(svn r20923) -Codechange: prepare creating sub-classes of NetworkClientSocket for server and client side
Diffstat (limited to 'src/network')
-rw-r--r--src/network/core/tcp_game.cpp21
-rw-r--r--src/network/core/tcp_game.h11
-rw-r--r--src/network/network.cpp34
-rw-r--r--src/network/network_client.cpp8
-rw-r--r--src/network/network_client.h6
-rw-r--r--src/network/network_server.cpp28
-rw-r--r--src/network/network_server.h7
7 files changed, 73 insertions, 42 deletions
diff --git a/src/network/core/tcp_game.cpp b/src/network/core/tcp_game.cpp
index 494c12a87..6d8e174c8 100644
--- a/src/network/core/tcp_game.cpp
+++ b/src/network/core/tcp_game.cpp
@@ -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) {
diff --git a/src/network/core/tcp_game.h b/src/network/core/tcp_game.h
index acbd47ed8..77da6218c 100644
--- a/src/network/core/tcp_game.h
+++ b/src/network/core/tcp_game.h
@@ -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; }
diff --git a/src/network/network.cpp b/src/network/network.cpp
index 06c46a01d..a4b42d0d8 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -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;
-
- SetWindowDirty(WC_CLIENT_LIST, 0);
- }
+ /* Register the login */
+ _network_clients_connected++;
- return cs;
+ SetWindowDirty(WC_CLIENT_LIST, 0);
+ return new ServerNetworkGameSocketHandler(s);
}
/* Close a connection */
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index b6ffcbb22..c9d5791dd 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -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)
diff --git a/src/network/network_client.h b/src/network/network_client.h
index 88a02f036..e7fd0057f 100644
--- a/src/network/network_client.h
+++ b/src/network/network_client.h
@@ -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);
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 95b65b3f4..d11051ea5 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -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);
/***********
diff --git a/src/network/network_server.h b/src/network/network_server.h
index 8e7342a9e..55366d222 100644
--- a/src/network/network_server.h
+++ b/src/network/network_server.h
@@ -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);