summaryrefslogtreecommitdiff
path: root/src/messageout.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-05-14 16:19:53 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-05-14 16:19:53 +0000
commit0125cb14a9a4f4ffced61ba50412f13fe00adbb4 (patch)
tree9820d60b3b37f627ab5fb94e9e7d7bc70432fb9c /src/messageout.cpp
parent87be1a8a52a5485d1cf90240cbf628004cbd5350 (diff)
downloadmanaserv-0125cb14a9a4f4ffced61ba50412f13fe00adbb4.tar.gz
manaserv-0125cb14a9a4f4ffced61ba50412f13fe00adbb4.tar.bz2
manaserv-0125cb14a9a4f4ffced61ba50412f13fe00adbb4.tar.xz
manaserv-0125cb14a9a4f4ffced61ba50412f13fe00adbb4.zip
Applied another patch by Guillaume Melquiond which fixed a number of issues
with the message implementation (client side should also be checked).
Diffstat (limited to 'src/messageout.cpp')
-rw-r--r--src/messageout.cpp41
1 files changed, 20 insertions, 21 deletions
diff --git a/src/messageout.cpp b/src/messageout.cpp
index 7fa6e1fd..e8336694 100644
--- a/src/messageout.cpp
+++ b/src/messageout.cpp
@@ -58,55 +58,54 @@ MessageOut::expand(size_t bytes)
void
MessageOut::writeByte(char value)
{
- expand(mPos + sizeof(char));
+ expand(mPos + 1);
mData[mPos] = value;
- mPos += sizeof(char);
+ mPos += 1;
}
void MessageOut::writeShort(short value)
{
- expand(mPos + sizeof(short));
- (*(short *)&mData[mPos]) = ENET_HOST_TO_NET_16(value);
- mPos += sizeof(short);
+ expand(mPos + 2);
+ uint16_t t = ENET_HOST_TO_NET_16(value);
+ memcpy(mData + mPos, &t, 2);
+ mPos += 2;
}
void
MessageOut::writeLong(long value)
{
- expand(mPos + sizeof(long));
- (*(long *)&mData[mPos]) = ENET_HOST_TO_NET_32(value);
- mPos += sizeof(long);
+ expand(mPos + 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)
{
- std::string toWrite = string;
-
+ int stringLength = string.length();
if (length < 0)
{
// Write the length at the start if not fixed
- writeShort(string.length());
- expand(mPos + string.length());
+ writeShort(stringLength);
+ length = stringLength;
}
- else if (length < (int)string.length())
+ else if (length < stringLength)
{
// Make sure the length of the string is no longer than specified
- toWrite = string.substr(0, length);
-
- expand(mPos + length);
+ stringLength = length;
}
+ expand(mPos + length);
// Write the actual string
- memcpy(&mData[mPos], (void*)toWrite.c_str(), toWrite.length());
- mPos += 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();
+ memset(mData + mPos + stringLength, '\0', length - stringLength);
}
+ mPos += length;
}
const Packet*