diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-04-14 12:59:54 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-05-05 22:51:32 +0200 |
commit | bb9a9b9b0f4ec7cc6a9ca3a6bd2dc35b0b6541e7 (patch) | |
tree | 503dcb475ece8e72e2fbef2e78e82b2a881e6d23 /src/net/messageout.cpp | |
parent | 05669aae551820f2183984d1c706d3a82eb37be6 (diff) | |
download | manaserv-bb9a9b9b0f4ec7cc6a9ca3a6bd2dc35b0b6541e7.tar.gz manaserv-bb9a9b9b0f4ec7cc6a9ca3a6bd2dc35b0b6541e7.tar.bz2 manaserv-bb9a9b9b0f4ec7cc6a9ca3a6bd2dc35b0b6541e7.tar.xz manaserv-bb9a9b9b0f4ec7cc6a9ca3a6bd2dc35b0b6541e7.zip |
Added debugging mode to the protocol
Allows inspection of message data. It is off by default since it consumes
additional bandwidth, but it can be turned on using the net_debugMode option
in manaserv.xml.
Currently the option only affects outgoing data for each host individually.
In particular, enabling this debug mode for the server does not automatically
make the client annotate its messages.
Reviewed-by: Erik Schilling
Diffstat (limited to 'src/net/messageout.cpp')
-rw-r--r-- | src/net/messageout.cpp | 78 |
1 files changed, 44 insertions, 34 deletions
diff --git a/src/net/messageout.cpp b/src/net/messageout.cpp index a5b0a53c..d39fd23e 100644 --- a/src/net/messageout.cpp +++ b/src/net/messageout.cpp @@ -18,6 +18,9 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ +#include "net/messageout.h" +#include "net/messagein.h" + #include <cstring> #include <iomanip> #include <iostream> @@ -28,28 +31,26 @@ #include <string> #include <enet/enet.h> -#include "net/messageout.h" - /** Initial amount of bytes allocated for the messageout data buffer. */ const unsigned int INITIAL_DATA_CAPACITY = 16; /** Factor by which the messageout data buffer is increased when too small. */ const unsigned int CAPACITY_GROW_FACTOR = 2; -MessageOut::MessageOut(): - mPos(0) -{ - mData = (char*) malloc(INITIAL_DATA_CAPACITY); - mDataSize = INITIAL_DATA_CAPACITY; -} +static bool debugModeEnabled = false; MessageOut::MessageOut(int id): - mPos(0) + mPos(0), + mDebugMode(false) { mData = (char*) malloc(INITIAL_DATA_CAPACITY); mDataSize = INITIAL_DATA_CAPACITY; + if (debugModeEnabled) + id |= ManaServ::XXMSG_DEBUG_FLAG; + writeInt16(id); + mDebugMode = debugModeEnabled; } MessageOut::~MessageOut() @@ -57,15 +58,7 @@ MessageOut::~MessageOut() free(mData); } -void MessageOut::clear() -{ - mData = (char *) realloc(mData, INITIAL_DATA_CAPACITY); - mDataSize = INITIAL_DATA_CAPACITY; - mPos = 0; -} - -void -MessageOut::expand(size_t bytes) +void MessageOut::expand(size_t bytes) { if (bytes > mDataSize) { @@ -81,6 +74,9 @@ MessageOut::expand(size_t bytes) void MessageOut::writeInt8(int value) { + if (mDebugMode) + writeValueType(ManaServ::Int8); + expand(mPos + 1); mData[mPos] = value; mPos += 1; @@ -88,6 +84,9 @@ void MessageOut::writeInt8(int value) void MessageOut::writeInt16(int value) { + if (mDebugMode) + writeValueType(ManaServ::Int16); + expand(mPos + 2); uint16_t t = ENET_HOST_TO_NET_16(value); memcpy(mData + mPos, &t, 2); @@ -96,6 +95,9 @@ void MessageOut::writeInt16(int value) void MessageOut::writeInt32(int value) { + if (mDebugMode) + writeValueType(ManaServ::Int32); + expand(mPos + 4); uint32_t t = ENET_HOST_TO_NET_32(value); memcpy(mData + mPos, &t, 4); @@ -104,6 +106,9 @@ void MessageOut::writeInt32(int value) void MessageOut::writeDouble(double value) { + if (mDebugMode) + writeValueType(ManaServ::Double); + #ifdef USE_NATIVE_DOUBLE expand(mPos + sizeof(double)); memcpy(mData + mPos, &value, sizeof(double)); @@ -121,19 +126,14 @@ void MessageOut::writeDouble(double value) #endif } -void MessageOut::writeCoordinates(int x, int y) +void MessageOut::writeString(const std::string &string, int length) { - expand(mPos + 3); - char *p = mData + mPos; - p[0] = x & 0x00FF; - p[1] = ((x & 0x0700) >> 8) | ((y & 0x001F) << 3); - p[2] = (y & 0x07E0) >> 5; - mPos += 3; -} + if (mDebugMode) + { + writeValueType(ManaServ::String); + writeInt16(length); + } -void -MessageOut::writeString(const std::string &string, int length) -{ int stringLength = string.length(); if (length < 0) { @@ -149,7 +149,7 @@ MessageOut::writeString(const std::string &string, int length) expand(mPos + length); // Write the actual string - memcpy(mData + mPos, string.c_str(), stringLength); + memcpy(mData + mPos, string.data(), stringLength); if (length > stringLength) { @@ -159,15 +159,20 @@ MessageOut::writeString(const std::string &string, int length) mPos += length; } +void MessageOut::writeValueType(ManaServ::ValueType type) +{ + expand(mPos + 1); + mData[mPos] = type; + mPos += 1; +} + std::ostream& operator <<(std::ostream &os, const MessageOut &msg) { if (msg.getLength() >= 2) { - unsigned short id = ENET_NET_TO_HOST_16(*(short*) msg.mData); - os << std::setw(6) << std::hex << std::showbase << std::internal - << std::setfill('0') << id - << std::dec << " (" << msg.getLength() << " B)"; + MessageIn m(msg.mData, msg.mPos); + os << m; } else { @@ -176,3 +181,8 @@ operator <<(std::ostream &os, const MessageOut &msg) } return os; } + +void MessageOut::setDebugModeEnabled(bool enabled) +{ + debugModeEnabled = enabled; +} |