summaryrefslogtreecommitdiff
path: root/src/net/messageout.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/messageout.cpp')
-rw-r--r--src/net/messageout.cpp106
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;
}