diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-02-10 02:57:42 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-02-10 03:07:44 +0300 |
commit | 0f67a984182336dac85b15e55cbe3bb11c29424a (patch) | |
tree | abc6e375f4a2d8125803118bd43fc276e4d5f0ee | |
parent | 0dae81cae8d0d95b137f2fca1f74b7aa14e964ce (diff) | |
download | manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.gz manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.bz2 manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.xz manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.zip |
Improve network code speed again.
-rw-r--r-- | src/net/eathena/network.cpp | 78 | ||||
-rw-r--r-- | src/net/eathena/network.h | 2 | ||||
-rw-r--r-- | src/net/tmwa/network.cpp | 67 | ||||
-rw-r--r-- | src/net/tmwa/network.h | 2 |
4 files changed, 52 insertions, 97 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; diff --git a/src/net/eathena/network.h b/src/net/eathena/network.h index e2405f63d..c2f6e0bb6 100644 --- a/src/net/eathena/network.h +++ b/src/net/eathena/network.h @@ -60,8 +60,6 @@ class Network final : public Ea::Network bool messageReady(); - MessageIn *getNextMessage() A_WARN_UNUSED; - void dispatchMessages(); protected: diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp index 6f940cf23..52cd5cf62 100644 --- a/src/net/tmwa/network.cpp +++ b/src/net/tmwa/network.cpp @@ -88,7 +88,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() : @@ -146,22 +146,35 @@ void Network::dispatchMessages() BLOCK_START("Network::dispatchMessages") while (messageReady()) { - MessageIn *const msg = getNextMessage(); - if (msg->getLength() == 0) + SDL_mutexP(mMutex); + const int msgId = readWord(0); + int len; + if (msgId == SMSG_SERVER_VERSION_RESPONSE) + len = 10; + else if (msgId == SMSG_UPDATE_HOST2) + len = -1; + else + len = packet_lengths[msgId]; + + if (len == -1) + len = readWord(2); + + 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); } BLOCK_END("Network::dispatchMessages") } @@ -198,40 +211,6 @@ bool Network::messageReady() return ret; } -MessageIn *Network::getNextMessage() -{ - BLOCK_START("Network::getNextMessage") - while (!messageReady()) - { - if (mState == NET_ERROR) - break; - } - - SDL_mutexP(mMutex); - const int msgId = readWord(0); - int len; - if (msgId == SMSG_SERVER_VERSION_RESPONSE) - len = 10; - else if (msgId == SMSG_UPDATE_HOST2) - len = -1; - else - 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); - - BLOCK_END("Network::getNextMessage") - return msg; -} - Network *Network::instance() { return mInstance; diff --git a/src/net/tmwa/network.h b/src/net/tmwa/network.h index 9878239d5..66c1acb26 100644 --- a/src/net/tmwa/network.h +++ b/src/net/tmwa/network.h @@ -61,8 +61,6 @@ class Network final : public Ea::Network bool messageReady(); - MessageIn *getNextMessage() A_WARN_UNUSED; - void dispatchMessages(); protected: |