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 /src/net/tmwa | |
parent | 0dae81cae8d0d95b137f2fca1f74b7aa14e964ce (diff) | |
download | manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.gz manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.bz2 manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.tar.xz manaverse-0f67a984182336dac85b15e55cbe3bb11c29424a.zip |
Improve network code speed again.
Diffstat (limited to 'src/net/tmwa')
-rw-r--r-- | src/net/tmwa/network.cpp | 67 | ||||
-rw-r--r-- | src/net/tmwa/network.h | 2 |
2 files changed, 23 insertions, 46 deletions
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: |