summaryrefslogtreecommitdiff
path: root/src/messageout.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-08-25 19:47:33 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-08-25 19:47:33 +0000
commit1221231dc656368bf1595b7827ad3a48f38b33b7 (patch)
treed0b5691a3a027d46d57364f86727baaecce5f4e0 /src/messageout.cpp
parent87eecbfc7c7ac72cb1a57ba83e967c655213d116 (diff)
downloadmanaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.tar.gz
manaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.tar.bz2
manaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.tar.xz
manaserv-1221231dc656368bf1595b7827ad3a48f38b33b7.zip
Applied patch by Rogier, addressing TODOs in MessageOut and NetComputer.
Diffstat (limited to 'src/messageout.cpp')
-rw-r--r--src/messageout.cpp38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/messageout.cpp b/src/messageout.cpp
index d9578577..71e81542 100644
--- a/src/messageout.cpp
+++ b/src/messageout.cpp
@@ -24,36 +24,51 @@
#include "messageout.h"
#include <string>
+#include <stdlib.h>
+#include <iosfwd>
#include <enet/enet.h>
+/** Initial amount of bytes allocated for the messageout data buffer. */
+const unsigned int INITIAL_DATA_CAPACITY = 2;
+
+/** Factor by which the messageout data buffer is increased when too small. */
+const unsigned int CAPACITY_GROW_FACTOR = 2;
+
MessageOut::MessageOut():
- mData(0),
- mDataSize(0),
mPos(0)
{
+ mData = (char*) malloc(INITIAL_DATA_CAPACITY);
+ mDataSize = INITIAL_DATA_CAPACITY;
}
MessageOut::MessageOut(short id):
- mData(0),
- mDataSize(0),
mPos(0)
{
+ mData = (char*) malloc(INITIAL_DATA_CAPACITY);
+ mDataSize = INITIAL_DATA_CAPACITY;
+
writeShort(id);
}
MessageOut::~MessageOut()
{
- if (mData) {
- free(mData);
- }
+ free(mData);
}
void
MessageOut::expand(size_t bytes)
{
- mData = (char*)realloc(mData, bytes);
- mDataSize = bytes;
+ if (bytes > mDataSize)
+ {
+ do
+ {
+ mDataSize *= CAPACITY_GROW_FACTOR;
+ }
+ while (bytes > mDataSize);
+
+ mData = (char*) realloc(mData, mDataSize);
+ }
}
void
@@ -64,7 +79,8 @@ MessageOut::writeByte(char value)
mPos += 1;
}
-void MessageOut::writeShort(short value)
+void
+MessageOut::writeShort(short value)
{
expand(mPos + 2);
uint16_t t = ENET_HOST_TO_NET_16(value);
@@ -101,9 +117,9 @@ MessageOut::writeString(const std::string &string, int length)
// Write the actual string
memcpy(mData + mPos, string.c_str(), stringLength);
- // Pad remaining space with zeros
if (length > stringLength)
{
+ // Pad remaining space with zeros
memset(mData + mPos + stringLength, '\0', length - stringLength);
}
mPos += length;