diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-05-01 22:38:07 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-05-05 22:30:00 +0200 |
commit | df9ed1ab4364fe29bf260cae51827cc2722eff78 (patch) | |
tree | 45324f700d2b2c87983fa4b8c5ed01ee2e68ba70 /src/net/manaserv/messagein.cpp | |
parent | 96abc4a9658b3318d0052dc5cd31a3c15d76a494 (diff) | |
download | mana-df9ed1ab4364fe29bf260cae51827cc2722eff78.tar.gz mana-df9ed1ab4364fe29bf260cae51827cc2722eff78.tar.bz2 mana-df9ed1ab4364fe29bf260cae51827cc2722eff78.tar.xz mana-df9ed1ab4364fe29bf260cae51827cc2722eff78.zip |
Added debugging mode to the protocol
This makes the client able to send and receive messages sent in debugging
mode, where the contents of the messages are annotated.
For messages sent from the client the debugging mode is currently always
enabled. Later on this could be an internal option or controlled from the
server side.
Reviewed-by: Erik Schilling
Diffstat (limited to 'src/net/manaserv/messagein.cpp')
-rw-r--r-- | src/net/manaserv/messagein.cpp | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/src/net/manaserv/messagein.cpp b/src/net/manaserv/messagein.cpp index aeb02b8d..8065d313 100644 --- a/src/net/manaserv/messagein.cpp +++ b/src/net/manaserv/messagein.cpp @@ -29,15 +29,24 @@ namespace ManaServ { MessageIn::MessageIn(const char *data, unsigned int length): mData(data), mLength(length), + mDebugMode(false), mPos(0) { // Read the message ID mId = readInt16(); + + // Read and clear the debug flag + mDebugMode = mId & ManaServ::XXMSG_DEBUG_FLAG; + mId &= ~ManaServ::XXMSG_DEBUG_FLAG; } uint8_t MessageIn::readInt8() { uint8_t value = 0; + + if (!readValueType(ManaServ::Int8)) + return value; + if (mPos < mLength) { value = mData[mPos]; @@ -49,6 +58,10 @@ uint8_t MessageIn::readInt8() uint16_t MessageIn::readInt16() { uint16_t value = 0; + + if (!readValueType(ManaServ::Int16)) + return value; + if (mPos + 2 <= mLength) { uint16_t t; @@ -62,6 +75,10 @@ uint16_t MessageIn::readInt16() uint32_t MessageIn::readInt32() { uint32_t value = 0; + + if (!readValueType(ManaServ::Int32)) + return value; + if (mPos + 4 <= mLength) { uint32_t t; @@ -74,24 +91,55 @@ uint32_t MessageIn::readInt32() std::string MessageIn::readString(int length) { + if (!readValueType(ManaServ::String)) + return std::string(); + + if (mDebugMode) + { + int fixedLength = (int16_t) readInt16(); + if (fixedLength != length) + { + // String does not have the expected length + mPos = mLength + 1; + return std::string(); + } + } + // Get string length if (length < 0) + { length = readInt16(); + } // Make sure the string isn't erroneous if (length < 0 || mPos + length > mLength) { mPos = mLength + 1; - return ""; + return std::string(); } // Read the string - char const *stringBeg = mData + mPos; - char const *stringEnd = (char const *)memchr(stringBeg, '\0', length); + const char *stringBeg = mData + mPos; + const char *stringEnd = (const char *)memchr(stringBeg, '\0', length); std::string readString(stringBeg, stringEnd ? stringEnd - stringBeg : length); mPos += length; + return readString; } +bool MessageIn::readValueType(ManaServ::ValueType type) +{ + if (!mDebugMode) // Verification not possible + return true; + + if (mPos >= mLength) + return false; + + uint8_t t = mData[mPos]; + ++mPos; + + return t == type; +} + } // ManaServ |