diff options
Diffstat (limited to 'src/net/eathena/network.cpp')
-rw-r--r-- | src/net/eathena/network.cpp | 78 |
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; |