diff options
Diffstat (limited to 'src/net/manaserv/messageout.cpp')
-rw-r--r-- | src/net/manaserv/messageout.cpp | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/src/net/manaserv/messageout.cpp b/src/net/manaserv/messageout.cpp index 6a8482e8..1197176f 100644 --- a/src/net/manaserv/messageout.cpp +++ b/src/net/manaserv/messageout.cpp @@ -28,9 +28,17 @@ namespace ManaServ { MessageOut::MessageOut(uint16_t id): - Net::MessageOut(id) + mData(0), + mPos(0), + mDataSize(0), + mDebugMode(false) { + bool debug = true; + if (debug) + id |= ManaServ::XXMSG_DEBUG_FLAG; + writeInt16(id); + mDebugMode = debug; } MessageOut::~MessageOut() @@ -44,8 +52,21 @@ void MessageOut::expand(size_t bytes) mDataSize = mPos + bytes; } +void MessageOut::writeInt8(uint8_t value) +{ + if (mDebugMode) + writeValueType(ManaServ::Int8); + + expand(1); + mData[mPos] = value; + mPos += 1; +} + void MessageOut::writeInt16(uint16_t value) { + if (mDebugMode) + writeValueType(ManaServ::Int16); + expand(2); uint16_t t = ENET_HOST_TO_NET_16(value); memcpy(mData + mPos, &t, 2); @@ -54,10 +75,53 @@ void MessageOut::writeInt16(uint16_t value) void MessageOut::writeInt32(uint32_t value) { + if (mDebugMode) + writeValueType(ManaServ::Int32); + expand(4); uint32_t t = ENET_HOST_TO_NET_32(value); memcpy(mData + mPos, &t, 4); mPos += 4; } +void MessageOut::writeString(const std::string &string, int length) +{ + if (mDebugMode) + { + writeValueType(ManaServ::String); + writeInt16(length); + } + + int stringLength = string.length(); + if (length < 0) + { + // Write the length at the start if not fixed + writeInt16(stringLength); + length = stringLength; + } + else if (length < stringLength) + { + // Make sure the length of the string is no longer than specified + stringLength = length; + } + expand(length); + + // Write the actual string + memcpy(mData + mPos, string.data(), stringLength); + + if (length > stringLength) + { + // Pad remaining space with zeros + memset(mData + mPos + stringLength, '\0', length - stringLength); + } + mPos += length; +} + +void MessageOut::writeValueType(ManaServ::ValueType type) +{ + expand(1); + mData[mPos] = type; + mPos += 1; +} + } // namespace ManaServ |