summaryrefslogtreecommitdiff
path: root/src/net/eathena/network.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2013-02-10 02:57:42 +0300
committerAndrei Karas <akaras@inbox.ru>2013-02-10 03:07:44 +0300
commit0f67a984182336dac85b15e55cbe3bb11c29424a (patch)
treeabc6e375f4a2d8125803118bd43fc276e4d5f0ee /src/net/eathena/network.cpp
parent0dae81cae8d0d95b137f2fca1f74b7aa14e964ce (diff)
downloadmv-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.gz
mv-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.bz2
mv-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.xz
mv-0f67a984182336dac85b15e55cbe3bb11c29424a.zip
Improve network code speed again.
Diffstat (limited to 'src/net/eathena/network.cpp')
-rw-r--r--src/net/eathena/network.cpp78
1 files changed, 29 insertions, 49 deletions
diff --git a/src/net/eathena/network.cpp b/src/net/eathena/network.cpp
index b72f78487..419459e35 100644
--- a/src/net/eathena/network.cpp
+++ b/src/net/eathena/network.cpp
@@ -110,7 +110,7 @@ short packet_lengths[] =
static constexpr const int packet_lengths_size
= static_cast<int>(sizeof(packet_lengths) / sizeof (short));
-static constexpr const unsigned int messagesSize = 0xffff;
+static constexpr const int messagesSize = 0xffff;
Network *Network::mInstance = nullptr;
Network::Network() :
@@ -167,23 +167,42 @@ void Network::dispatchMessages()
{
while (messageReady())
{
- MessageIn *const msg = getNextMessage();
+ SDL_mutexP(mMutex);
+ const int msgId = readWord(0);
+ int len = -1;
+ if (msgId == SMSG_SERVER_VERSION_RESPONSE)
+ {
+ len = 10;
+ }
+ else if (msgId == SMSG_UPDATE_HOST2)
+ {
+ len = -1;
+ }
+ else
+ {
+ if (msgId >= 0 && msgId < packet_lengths_size)
+ len = packet_lengths[msgId];
+ }
+
+ if (len == -1)
+ len = readWord(2);
- if (msg->getLength() == 0)
+ MessageIn msg(mInBuffer, len);
+ SDL_mutexV(mMutex);
+
+ if (len == 0)
logger->safeError("Zero length packet received. Exiting.");
- const int id = msg->getId();
- if (id >= 0 && id < messagesSize)
+ if (msgId >= 0 && msgId < messagesSize)
{
- MessageHandler *const handler = mMessageHandlers[id];
+ MessageHandler *const handler = mMessageHandlers[msgId];
if (handler)
- handler->handleMessage(*msg);
+ handler->handleMessage(msg);
else
- logger->log("Unhandled packet: %x", msg->getId());
+ logger->log("Unhandled packet: %x", msgId);
}
- skip(msg->getLength());
- delete msg;
+ skip(len);
}
}
@@ -212,45 +231,6 @@ bool Network::messageReady()
return ret;
}
-MessageIn *Network::getNextMessage()
-{
- while (!messageReady())
- {
- if (mState == NET_ERROR)
- break;
- }
-
- SDL_mutexP(mMutex);
- const int msgId = readWord(0);
- int len = -1;
- if (msgId == SMSG_SERVER_VERSION_RESPONSE)
- {
- len = 10;
- }
- else if (msgId == SMSG_UPDATE_HOST2)
- {
- len = -1;
- }
- else
- {
- if (msgId >= 0 && msgId < packet_lengths_size)
- len = packet_lengths[msgId];
- }
-
- if (len == -1)
- len = readWord(2);
-
-#ifdef ENABLEDEBUGLOG
- logger->dlog(strprintf("Received packet 0x%x of length %d\n",
- msgId, len));
-#endif
-
- MessageIn *const msg = new MessageIn(mInBuffer, len);
- SDL_mutexV(mMutex);
-
- return msg;
-}
-
Network *Network::instance()
{
return mInstance;