diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-04-17 09:19:52 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-04-18 08:53:51 +0200 |
commit | 1c19fb5173c59b8dd7de10af93347bc9d9279e7e (patch) | |
tree | 48e1f3cf9ab986de82d2becd0dbb35f21c2f8ec2 /src/net | |
parent | aecbf876cd0a7894396a2e5034af9d93bf028aa0 (diff) | |
download | mana-1c19fb5173c59b8dd7de10af93347bc9d9279e7e.tar.gz mana-1c19fb5173c59b8dd7de10af93347bc9d9279e7e.tar.bz2 mana-1c19fb5173c59b8dd7de10af93347bc9d9279e7e.tar.xz mana-1c19fb5173c59b8dd7de10af93347bc9d9279e7e.zip |
Simplify TmwAthena::MessageOut
Since for tmwAthena we're writing messages directly into the output
buffer, the MessageOut implementation does not need any members.
Also used SDL_SwapLE16 and SDL_SwapLE32 for convenience.
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/tmwa/messagein.cpp | 15 | ||||
-rw-r--r-- | src/net/tmwa/messageout.cpp | 48 | ||||
-rw-r--r-- | src/net/tmwa/messageout.h | 27 | ||||
-rw-r--r-- | src/net/tmwa/network.cpp | 15 | ||||
-rw-r--r-- | src/net/tmwa/network.h | 6 |
5 files changed, 27 insertions, 84 deletions
diff --git a/src/net/tmwa/messagein.cpp b/src/net/tmwa/messagein.cpp index 899b135d..7c142619 100644 --- a/src/net/tmwa/messagein.cpp +++ b/src/net/tmwa/messagein.cpp @@ -21,7 +21,6 @@ #include "net/tmwa/messagein.h" -#include <SDL.h> #include <SDL_endian.h> #define MAKEWORD(low,high) \ @@ -55,13 +54,8 @@ uint16_t MessageIn::readInt16() uint16_t value = 0; if (mPos + 2 <= mLength) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint16_t swap; - memcpy(&swap, mData + mPos, sizeof(uint16_t)); - value = SDL_Swap16(swap); -#else memcpy(&value, mData + mPos, sizeof(uint16_t)); -#endif + value = SDL_SwapLE16(value); } mPos += 2; return value; @@ -72,13 +66,8 @@ uint32_t MessageIn::readInt32() uint32_t value = 0; if (mPos + 4 <= mLength) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint32_t swap; - memcpy(&swap, mData + mPos, sizeof(uint32_t)); - value = SDL_Swap32(swap); -#else memcpy(&value, mData + mPos, sizeof(uint32_t)); -#endif + value = SDL_SwapLE32(value); } mPos += 4; return value; diff --git a/src/net/tmwa/messageout.cpp b/src/net/tmwa/messageout.cpp index 19f5ee49..12c9419a 100644 --- a/src/net/tmwa/messageout.cpp +++ b/src/net/tmwa/messageout.cpp @@ -23,54 +23,40 @@ #include "net/tmwa/network.h" -#include <SDL.h> #include <SDL_endian.h> #include <cstring> namespace TmwAthena { -MessageOut::MessageOut(uint16_t id): - mNetwork(TmwAthena::Network::instance()), - mData(mNetwork->mOutBuffer + mNetwork->mOutSize) +MessageOut::MessageOut(uint16_t id) { writeInt16(id); } -void MessageOut::expand(size_t bytes) +char *MessageOut::expand(size_t bytes) { - mNetwork->mOutSize += bytes; + Network &net = *Network::mInstance; + char *data = net.mOutBuffer + net.mOutSize; + net.mOutSize += bytes; + return data; } void MessageOut::writeInt8(uint8_t value) { - expand(1); - mData[mPos] = value; - mPos += 1; + *expand(1) = value; } void MessageOut::writeInt16(uint16_t value) { - expand(2); -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint16_t swap = SDL_Swap16(value); - memcpy(mData + mPos, &swap, sizeof(uint16_t)); -#else - memcpy(mData + mPos, &value, sizeof(uint16_t)); -#endif - mPos += 2; + value = SDL_SwapLE16(value); + memcpy(expand(sizeof(uint16_t)), &value, sizeof(uint16_t)); } void MessageOut::writeInt32(uint32_t value) { - expand(4); -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - uint32_t swap = SDL_Swap32(value); - memcpy(mData + mPos, &swap, sizeof(uint32_t)); -#else - memcpy(mData + mPos, &value, sizeof(uint32_t)); -#endif - mPos += 4; + value = SDL_SwapLE32(value); + memcpy(expand(sizeof(uint32_t)), &value, sizeof(uint32_t)); } void MessageOut::writeString(const std::string &string, int length) @@ -87,24 +73,22 @@ void MessageOut::writeString(const std::string &string, int length) // Make sure the length of the string is no longer than specified stringLength = length; } - expand(length); + + char *data = expand(length); // Write the actual string - memcpy(mData + mPos, string.data(), stringLength); + memcpy(data, string.data(), stringLength); // Pad remaining space with zeros if (length > stringLength) { - memset(mData + mPos + stringLength, '\0', length - stringLength); + memset(data + stringLength, '\0', length - stringLength); } - mPos += length; } void MessageOut::writeCoordinates(uint16_t x, uint16_t y, uint8_t direction) { - char *data = mData + mPos; - expand(3); - mPos += 3; + char *data = expand(3); uint16_t temp = x; temp <<= 6; diff --git a/src/net/tmwa/messageout.h b/src/net/tmwa/messageout.h index 4f1faa9d..b60644de 100644 --- a/src/net/tmwa/messageout.h +++ b/src/net/tmwa/messageout.h @@ -27,8 +27,6 @@ namespace TmwAthena { -class Network; - /** * Used for building an outgoing message to eAthena. * @@ -66,31 +64,12 @@ class MessageOut void writeCoordinates(uint16_t x, uint16_t y, uint8_t direction); - /** - * Returns the content of the message. - */ - char *getData() const { return mData; } - - /** - * Returns the length of the data. - */ - unsigned int getDataSize() const { return mDataSize; } - private: /** - * Expand the packet data to be able to hold more data. - * - * NOTE: For performance enhancements this method could allocate extra - * memory in advance instead of expanding size every time more data is - * added. + * Expand the packet data to be able to hold more data. Returns a + * pointer to the start of the new data. */ - void expand(size_t size); - - Network *mNetwork; - - char *mData; /**< Data building up. */ - unsigned int mDataSize = 0; /**< Size of data. */ - unsigned int mPos = 0; /**< Position in the data. */ + static char *expand(size_t size); }; } // namespace TmwAthena diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp index b4ba0b20..a35d7ffe 100644 --- a/src/net/tmwa/network.cpp +++ b/src/net/tmwa/network.cpp @@ -580,11 +580,6 @@ void Network::receive() SDLNet_FreeSocketSet(set); } -Network *Network::instance() -{ - return mInstance; -} - void Network::setError(const std::string &error) { logger->log("Network error: %s", error.c_str()); @@ -592,13 +587,11 @@ void Network::setError(const std::string &error) mState = NET_ERROR; } -Uint16 Network::readWord(int pos) +uint16_t Network::readWord(int pos) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - return SDL_Swap16((*(Uint16*)(mInBuffer+(pos)))); -#else - return (*(Uint16*)(mInBuffer+(pos))); -#endif + uint16_t value; + memcpy(&value, mInBuffer + pos, sizeof(uint16_t)); + return SDL_SwapLE16(value); } } // namespace TmwAthena diff --git a/src/net/tmwa/network.h b/src/net/tmwa/network.h index 36d8cafd..4069f916 100644 --- a/src/net/tmwa/network.h +++ b/src/net/tmwa/network.h @@ -90,15 +90,13 @@ class Network NET_ERROR }; - protected: + private: friend int networkThread(void *data); friend class MessageOut; - static Network *instance(); - void setError(const std::string &error); - Uint16 readWord(int pos); + uint16_t readWord(int pos); bool realConnect(); |