summaryrefslogtreecommitdiff
path: root/src/net/network.cpp
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2006-08-26 17:24:43 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2006-08-26 17:24:43 +0000
commit07b6070c25faedd78c2e26825bd700dc294f00cf (patch)
tree6a3053ad2a88ee6f0091fafba6607bc82231f7f6 /src/net/network.cpp
parent00d7badb647b4293c444c0374985b7f1ea8a1ed1 (diff)
downloadmana-07b6070c25faedd78c2e26825bd700dc294f00cf.tar.gz
mana-07b6070c25faedd78c2e26825bd700dc294f00cf.tar.bz2
mana-07b6070c25faedd78c2e26825bd700dc294f00cf.tar.xz
mana-07b6070c25faedd78c2e26825bd700dc294f00cf.zip
Made the Network class a purely static interface, as there is only one instance.
Diffstat (limited to 'src/net/network.cpp')
-rw-r--r--src/net/network.cpp47
1 files changed, 27 insertions, 20 deletions
diff --git a/src/net/network.cpp b/src/net/network.cpp
index 08b5fe92..e56f6df0 100644
--- a/src/net/network.cpp
+++ b/src/net/network.cpp
@@ -29,10 +29,25 @@
#include "../log.h"
-Network *network;
+static Network::State mState;
-Network::Network():
- mState(NET_OK)
+/**
+ * The local host.
+ */
+static ENetHost *mClient;
+
+/**
+ * An array holding the peers of the account, game and chat servers.
+ */
+static ENetPeer *mServers[3];
+
+typedef std::map<unsigned short, MessageHandler*> MessageHandlers;
+typedef MessageHandlers::iterator MessageHandlerIterator;
+static MessageHandlers mMessageHandlers;
+
+Network::State Network::getState() { return mState; }
+
+void Network::initialize()
{
// Initialize server peers
for (int i = 0; i < 3; ++i)
@@ -48,7 +63,7 @@ Network::Network():
}
}
-Network::~Network()
+void Network::finalize()
{
clearHandlers();
@@ -110,15 +125,10 @@ Network::disconnect(Server server)
void
Network::registerHandler(MessageHandler *handler)
{
- const Uint16 *i = handler->handledMessages;
-
- while(*i)
+ for (const Uint16 *i = handler->handledMessages; *i; i++)
{
mMessageHandlers[*i] = handler;
- i++;
}
-
- handler->setNetwork(this);
}
void
@@ -128,30 +138,27 @@ Network::unregisterHandler(MessageHandler *handler)
{
mMessageHandlers.erase(*i);
}
-
- handler->setNetwork(0);
}
void
Network::clearHandlers()
{
- MessageHandlerIterator i;
- for (i = mMessageHandlers.begin(); i != mMessageHandlers.end(); i++)
- {
- i->second->setNetwork(0);
- }
mMessageHandlers.clear();
}
bool
-Network::isConnected(Server server) const
+Network::isConnected(Server server)
{
return mServers[server] != NULL &&
mServers[server]->state == ENET_PEER_STATE_CONNECTED;
}
-void
-Network::dispatchMessage(ENetPacket *packet)
+/**
+ * Dispatches a message to the appropriate message handler and
+ * destroys it afterwards.
+ */
+static void
+dispatchMessage(ENetPacket *packet)
{
MessageIn msg((const char *)packet->data, packet->dataLength);