diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-20 11:56:23 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-20 11:56:23 +0000 |
commit | ae45030b2ebd5b07f8cfd234696fd024e6de9e66 (patch) | |
tree | 465affc2bd7df675edb428d7f003e5dbbba84c0e /src/net | |
parent | 01591924a4f33d5a5e4a86db6c256c8ce797a820 (diff) | |
download | mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.tar.gz mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.tar.bz2 mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.tar.xz mana-ae45030b2ebd5b07f8cfd234696fd024e6de9e66.zip |
Added documentation and did a bit of cleanup.
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/network.cpp | 89 | ||||
-rw-r--r-- | src/net/network.h | 55 |
2 files changed, 72 insertions, 72 deletions
diff --git a/src/net/network.cpp b/src/net/network.cpp index 23d9e291..911353b1 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -32,11 +32,12 @@ Network *network; Network::Network(): - mAccountServer(NULL), - mGameServer(NULL), - mChatServer(NULL), mState(NET_OK) { + // Initialize server peers + for (int i = 0; i < 3; ++i) + mServers[i] = NULL; + mClient = enet_host_create(NULL, 3, 0, 0); if (!mClient) @@ -68,15 +69,7 @@ Network::connect(Server server, const std::string &address, short port) return false; } - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - if (peer != NULL) + if (mServers[server] != NULL) { logger->log("Network::connect() already connected (or connecting) to " "this server!"); @@ -89,46 +82,28 @@ Network::connect(Server server, const std::string &address, short port) enetAddress.port = port; // Initiate the connection, allocating channel 0. - peer = enet_host_connect(mClient, &enetAddress, 1); + mServers[server] = enet_host_connect(mClient, &enetAddress, 1); - if (peer == NULL) + if (mServers[server] == NULL) { logger->log("Unable to initiate connection to the server."); mState = NET_ERROR; return false; } - switch (server) { - case ACCOUNT: mAccountServer = peer; break; - case GAME: mGameServer = peer; break; - case CHAT: mChatServer = peer; break; - } - return true; } void Network::disconnect(Server server) { - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - if (peer) + if (mServers[server]) { - enet_peer_disconnect(peer, 0); + enet_peer_disconnect(mServers[server], 0); enet_host_flush(mClient); - enet_peer_reset(peer); + enet_peer_reset(mServers[server]); - switch (server) { - case ACCOUNT: mAccountServer = NULL; break; - case GAME: mGameServer = NULL; break; - case CHAT: mChatServer = NULL; break; - } + mServers[server] = NULL; } } @@ -171,15 +146,8 @@ Network::clearHandlers() bool Network::isConnected(Server server) const { - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - return peer->state == ENET_PEER_STATE_CONNECTED; + return mServers[server] != NULL && + mServers[server]->state == ENET_PEER_STATE_CONNECTED; } void @@ -253,27 +221,20 @@ void Network::send(Server server, const MessageOut &msg) "ready."); return; } - - ENetPeer *peer = NULL; - - switch (server) { - case ACCOUNT: peer = mAccountServer; break; - case GAME: peer = mGameServer; break; - case CHAT: peer = mChatServer; break; - } - - if (peer) + else if (!isConnected(server)) { - logger->log("Sending message of size %d to server %d...", - msg.getDataSize(), server); - - // Directly send away the packet (TODO: check what ENet does in case - // this is done before connection is ready) - ENetPacket *packet = enet_packet_create(msg.getData(), - msg.getDataSize(), - ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(peer, 0, packet); + logger->log("Warning: cannot send message to not connected server %d!", + server); + return; } + + logger->log("Sending message of size %d to server %d...", + msg.getDataSize(), server); + + ENetPacket *packet = enet_packet_create(msg.getData(), + msg.getDataSize(), + ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(mServers[server], 0, packet); } char *iptostring(int address) diff --git a/src/net/network.h b/src/net/network.h index 49583b9d..40255e44 100644 --- a/src/net/network.h +++ b/src/net/network.h @@ -25,7 +25,6 @@ #define _TMW_NETWORK_ #include <map> -#include <queue> #include <string> #include <guichan.hpp> @@ -33,15 +32,26 @@ #include <enet/enet.h> class MessageHandler; -class MessageIn; class MessageOut; +/** + * The client network layer. Facilitates connecting and communicating to the + * account, game and chat servers. Also routes incoming message to the + * appropriate message handlers. + */ class Network { public: friend class MessageOut; + /** + * Constructor. Sets up the local host. + */ Network(); + + /** + * Destructor. + */ ~Network(); typedef enum { @@ -50,15 +60,30 @@ class Network CHAT } Server; + /** + * Connects to the given server with the specified address and port. + * This method is non-blocking, use isConnected to check whether the + * server is connected. + */ bool connect(Server server, const std::string &address, short port); + /** + * Disconnects from the given server. + */ void disconnect(Server server); + /** + * Registers a message handler. A message handler handles a certain + * subset of incoming messages. + */ void registerHandler(MessageHandler *handler); + /** + * Unregisters a message handler. + */ void unregisterHandler(MessageHandler *handler); @@ -68,15 +93,18 @@ class Network int getState() const { return mState; } + /** + * Returns whether the given server is connected. + */ bool isConnected(Server server) const; void - dispatchMessage(ENetPacket *packet); - - void flush(); + /** + * Send a message to a given server. The server should be connected. + */ void send(Server server, const MessageOut &msg); @@ -86,11 +114,22 @@ class Network }; private: + /** + * The local host. + */ ENetHost *mClient; - ENetPeer *mAccountServer; - ENetPeer *mGameServer; - ENetPeer *mChatServer; + /** + * An array holding the peers of the account, game and chat servers. + */ + ENetPeer *mServers[3]; + + /** + * Dispatches a message to the appropriate message handler and + * destroys it afterwards. + */ + void + dispatchMessage(ENetPacket *packet); unsigned int mToSkip; |