summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2013-02-10 02:13:26 +0300
committerAndrei Karas <akaras@inbox.ru>2013-02-10 03:07:44 +0300
commit0dae81cae8d0d95b137f2fca1f74b7aa14e964ce (patch)
tree73476a1f0d7296286f10261c0aa4457961389835 /src
parent45f076bd429c40d81607f43837f1cc571f3f6be0 (diff)
downloadmv-0dae81cae8d0d95b137f2fca1f74b7aa14e964ce.tar.gz
mv-0dae81cae8d0d95b137f2fca1f74b7aa14e964ce.tar.bz2
mv-0dae81cae8d0d95b137f2fca1f74b7aa14e964ce.tar.xz
mv-0dae81cae8d0d95b137f2fca1f74b7aa14e964ce.zip
Improve network code speed.
Diffstat (limited to 'src')
-rw-r--r--src/net/eathena/network.cpp41
-rw-r--r--src/net/eathena/network.h4
-rw-r--r--src/net/tmwa/network.cpp42
-rw-r--r--src/net/tmwa/network.h4
4 files changed, 46 insertions, 45 deletions
diff --git a/src/net/eathena/network.cpp b/src/net/eathena/network.cpp
index 00e2fa9cf..b72f78487 100644
--- a/src/net/eathena/network.cpp
+++ b/src/net/eathena/network.cpp
@@ -108,17 +108,24 @@ short packet_lengths[] =
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
+static constexpr const int packet_lengths_size
+ = static_cast<int>(sizeof(packet_lengths) / sizeof (short));
+static constexpr const unsigned int messagesSize = 0xffff;
Network *Network::mInstance = nullptr;
Network::Network() :
Ea::Network()
{
mInstance = this;
+ mMessageHandlers = new MessageHandler*[messagesSize];
+ memset(&mMessageHandlers[0], 0, sizeof(MessageHandler*) * 0xffff);
}
Network::~Network()
{
clearHandlers();
+ delete mMessageHandlers;
+ mMessageHandlers = nullptr;
mInstance = nullptr;
}
@@ -139,19 +146,21 @@ void Network::unregisterHandler(MessageHandler *handler)
return;
for (const uint16_t *i = handler->handledMessages; *i; ++i)
- mMessageHandlers.erase(*i);
+ mMessageHandlers[*i] = nullptr;
handler->setNetwork(nullptr);
}
void Network::clearHandlers()
{
- FOR_EACH (MessageHandlerIterator, i, mMessageHandlers)
+ for (int f = 0; f < messagesSize; f ++)
{
- if (i->second)
- i->second->setNetwork(nullptr);
+ if (mMessageHandlers[f])
+ {
+ mMessageHandlers[f]->setNetwork(nullptr);
+ mMessageHandlers[f] = nullptr;
+ }
}
- mMessageHandlers.clear();
}
void Network::dispatchMessages()
@@ -160,20 +169,17 @@ void Network::dispatchMessages()
{
MessageIn *const msg = getNextMessage();
- const MessageHandlerIterator iter = mMessageHandlers.find(
- msg->getId());
-
if (msg->getLength() == 0)
logger->safeError("Zero length packet received. Exiting.");
- if (iter != mMessageHandlers.end())
- {
- if (iter->second)
- iter->second->handleMessage(*msg);
- }
- else
+ const int id = msg->getId();
+ if (id >= 0 && id < messagesSize)
{
- logger->log("Unhandled packet: %x", msg->getId());
+ MessageHandler *const handler = mMessageHandlers[id];
+ if (handler)
+ handler->handleMessage(*msg);
+ else
+ logger->log("Unhandled packet: %x", msg->getId());
}
skip(msg->getLength());
@@ -227,11 +233,8 @@ MessageIn *Network::getNextMessage()
}
else
{
- if (msgId >= 0 && msgId < static_cast<signed>(
- sizeof(packet_lengths) / sizeof (short)))
- {
+ if (msgId >= 0 && msgId < packet_lengths_size)
len = packet_lengths[msgId];
- }
}
if (len == -1)
diff --git a/src/net/eathena/network.h b/src/net/eathena/network.h
index 52ab60a90..e2405f63d 100644
--- a/src/net/eathena/network.h
+++ b/src/net/eathena/network.h
@@ -69,9 +69,7 @@ class Network final : public Ea::Network
static Network *instance() A_WARN_UNUSED;
- typedef std::map<uint16_t, MessageHandler*> MessageHandlers;
- typedef MessageHandlers::iterator MessageHandlerIterator;
- MessageHandlers mMessageHandlers;
+ MessageHandler **mMessageHandlers;
static Network *mInstance;
};
diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp
index d0e0686fc..6f940cf23 100644
--- a/src/net/tmwa/network.cpp
+++ b/src/net/tmwa/network.cpp
@@ -86,17 +86,24 @@ short packet_lengths[] =
-1, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
+static constexpr const int packet_lengths_size
+ = static_cast<int>(sizeof(packet_lengths) / sizeof (short));
+static constexpr const unsigned int messagesSize = 0xffff;
Network *Network::mInstance = nullptr;
Network::Network() :
Ea::Network()
{
mInstance = this;
+ mMessageHandlers = new MessageHandler*[messagesSize];
+ memset(&mMessageHandlers[0], 0, sizeof(MessageHandler*) * 0xffff);
}
Network::~Network()
{
clearHandlers();
+ delete mMessageHandlers;
+ mMessageHandlers = nullptr;
mInstance = nullptr;
}
@@ -117,19 +124,21 @@ void Network::unregisterHandler(MessageHandler *handler)
return;
for (const uint16_t *i = handler->handledMessages; *i; ++i)
- mMessageHandlers.erase(*i);
+ mMessageHandlers[*i] = nullptr;
handler->setNetwork(nullptr);
}
void Network::clearHandlers()
{
- FOR_EACH (MessageHandlerIterator, i, mMessageHandlers)
+ for (int f = 0; f < messagesSize; f ++)
{
- if (i->second)
- i->second->setNetwork(nullptr);
+ if (mMessageHandlers[f])
+ {
+ mMessageHandlers[f]->setNetwork(nullptr);
+ mMessageHandlers[f] = nullptr;
+ }
}
- mMessageHandlers.clear();
}
void Network::dispatchMessages()
@@ -138,21 +147,17 @@ void Network::dispatchMessages()
while (messageReady())
{
MessageIn *const msg = getNextMessage();
-
- const MessageHandlerIterator iter = mMessageHandlers.find(
- msg->getId());
-
if (msg->getLength() == 0)
logger->safeError("Zero length packet received. Exiting.");
- if (iter != mMessageHandlers.end())
- {
- if (iter->second)
- iter->second->handleMessage(*msg);
- }
- else
+ const int id = msg->getId();
+ if (id >= 0 && id < messagesSize)
{
- logger->log("Unhandled packet: %x", msg->getId());
+ MessageHandler *const handler = mMessageHandlers[id];
+ if (handler)
+ handler->handleMessage(*msg);
+ else
+ logger->log("Unhandled packet: %x", msg->getId());
}
skip(msg->getLength());
@@ -179,11 +184,8 @@ bool Network::messageReady()
}
else
{
- if (msgId >= 0 && msgId < static_cast<signed>(
- sizeof(packet_lengths) / sizeof (short)))
- {
+ if (msgId >= 0 && msgId < packet_lengths_size)
len = packet_lengths[msgId];
- }
}
if (len == -1 && mInSize > 4)
diff --git a/src/net/tmwa/network.h b/src/net/tmwa/network.h
index 7806147a9..9878239d5 100644
--- a/src/net/tmwa/network.h
+++ b/src/net/tmwa/network.h
@@ -70,9 +70,7 @@ class Network final : public Ea::Network
static Network *instance() A_WARN_UNUSED;
- typedef std::map<uint16_t, MessageHandler*> MessageHandlers;
- typedef MessageHandlers::iterator MessageHandlerIterator;
- MessageHandlers mMessageHandlers;
+ MessageHandler **mMessageHandlers;
static Network *mInstance;
};