diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-03-22 19:45:03 +0100 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-03-22 19:45:56 +0100 |
commit | 0c43d04b438d41c277ae80402d4b4888db1a0b64 (patch) | |
tree | 3aaeb75ecd1bcbe85decedab5f1fa426fe0411e3 /src/net/messageout.cpp | |
parent | a7f5eaeb7f643658d356533a608f0f18d85b6d32 (diff) | |
parent | 401802c1d7a1b3d659bdc53a45d9a6292fc1121e (diff) | |
download | mana-0c43d04b438d41c277ae80402d4b4888db1a0b64.tar.gz mana-0c43d04b438d41c277ae80402d4b4888db1a0b64.tar.bz2 mana-0c43d04b438d41c277ae80402d4b4888db1a0b64.tar.xz mana-0c43d04b438d41c277ae80402d4b4888db1a0b64.zip |
Merged the tmwserv client with the eAthena client
This merge involved major changes on both sides, and as such took
several weeks. Lots of things are expected to be broken now, however, we
now have a single code base to improve and extend, which can be compiled
to support either eAthena or tmwserv.
In the coming months, the plan is to work towards a client that supports
both eAthena and tmwserv, without needing to be recompiled.
Conflicts:
Everywhere!
Diffstat (limited to 'src/net/messageout.cpp')
-rw-r--r-- | src/net/messageout.cpp | 106 |
1 files changed, 71 insertions, 35 deletions
diff --git a/src/net/messageout.cpp b/src/net/messageout.cpp index bf4957be..f7ab6b41 100644 --- a/src/net/messageout.cpp +++ b/src/net/messageout.cpp @@ -19,95 +19,131 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <cstring> +#include "messageout.h" + +#ifdef TMWSERV_SUPPORT +#include <enet/enet.h> +#else +#include "ea/network.h" #include <SDL.h> #include <SDL_endian.h> -#include <string> +#endif -#include "messageout.h" -#include "network.h" +#include <cstring> +#include <string> +#ifdef TMWSERV_SUPPORT +MessageOut::MessageOut(short id): + mData(0), +#else MessageOut::MessageOut(Network *network): mNetwork(network), - mData(0), +#endif mDataSize(0), mPos(0) { +#ifdef TMWSERV_SUPPORT + writeInt16(id); +#else mData = mNetwork->mOutBuffer + mNetwork->mOutSize; +#endif +} + +#ifdef TMWSERV_SUPPORT +MessageOut::~MessageOut() +{ + free(mData); +} + +void MessageOut::expand(size_t bytes) +{ + mData = (char*)realloc(mData, bytes); + mDataSize = bytes; } +#endif void MessageOut::writeInt8(Sint8 value) { +#ifdef TMWSERV_SUPPORT + expand(mPos + 1); +#else + mNetwork->mOutSize += 1; +#endif mData[mPos] = value; - mPos += sizeof(Sint8); - mNetwork->mOutSize+= sizeof(Sint8); + mPos += 1; } void MessageOut::writeInt16(Sint16 value) { +#ifdef TMWSERV_SUPPORT + expand(mPos + 2); + uint16_t t = ENET_HOST_TO_NET_16(value); + memcpy(mData + mPos, &t, 2); +#else #if SDL_BYTEORDER == SDL_BIG_ENDIAN (*(Sint16 *)(mData + mPos)) = SDL_Swap16(value); #else (*(Sint16 *)(mData + mPos)) = value; #endif - mPos += sizeof(Sint16); - mNetwork->mOutSize += sizeof(Sint16); + mNetwork->mOutSize += 2; +#endif // TMWSERV_SUPPORT + mPos += 2; } void MessageOut::writeInt32(Sint32 value) { +#ifdef TMWSERV_SUPPORT + expand(mPos + 4); + uint32_t t = ENET_HOST_TO_NET_32(value); + memcpy(mData + mPos, &t, 4); +#else #if SDL_BYTEORDER == SDL_BIG_ENDIAN (*(Sint32 *)(mData + mPos)) = SDL_Swap32(value); #else (*(Sint32 *)(mData + mPos)) = value; #endif - mPos += sizeof(Sint32); - mNetwork->mOutSize += sizeof(Sint32); + mNetwork->mOutSize += 4; +#endif // TMWSERV_SUPPORT + mPos += 4; } void MessageOut::writeString(const std::string &string, int length) { - std::string toWrite = string; - + int stringLength = string.length(); if (length < 0) { // Write the length at the start if not fixed - writeInt16(string.length()); + writeInt16(stringLength); + length = stringLength; } - else + else if (length < stringLength) { // Make sure the length of the string is no longer than specified - toWrite = string.substr(0, length); + stringLength = length; } +#ifdef TMWSERV_SUPPORT + expand(mPos + length); +#else + mNetwork->mOutSize += length; +#endif // Write the actual string - memcpy(&mData[mPos], (void*)toWrite.c_str(), toWrite.length()); - mPos += toWrite.length(); - mNetwork->mOutSize += toWrite.length(); + memcpy(mData + mPos, string.c_str(), stringLength); // Pad remaining space with zeros - if (length > (int)toWrite.length()) + if (length > stringLength) { - memset(&mData[mPos], '\0', length - toWrite.length()); - mPos += length - toWrite.length(); - mNetwork->mOutSize += length - toWrite.length(); + memset(mData + mPos + stringLength, '\0', length - stringLength); } + mPos += length; } -MessageOut& operator<<(MessageOut &msg, const Sint8 &rhs) -{ - msg.writeInt8(rhs); - return msg; -} - -MessageOut& operator<<(MessageOut &msg, const Sint16 &rhs) +char *MessageOut::getData() const { - msg.writeInt16(rhs); - return msg; + return mData; } -MessageOut& operator<<(MessageOut &msg, const Sint32 &rhs) +unsigned int MessageOut::getDataSize() const { - msg.writeInt32(rhs); - return msg; + return mDataSize; } |