summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2013-04-09 00:21:13 +0300
committerAndrei Karas <akaras@inbox.ru>2013-04-09 00:33:31 +0300
commitd9c62405db9a3f8bffaa5789bd63f59a65e29e3a (patch)
tree78c5417e3d2818f1844cd0a0730b6570eefbb9ef
parent02215574dc2b24eeef9c4ab55776334856bfbaf5 (diff)
downloadmanaverse-d9c62405db9a3f8bffaa5789bd63f59a65e29e3a.tar.gz
manaverse-d9c62405db9a3f8bffaa5789bd63f59a65e29e3a.tar.bz2
manaverse-d9c62405db9a3f8bffaa5789bd63f59a65e29e3a.tar.xz
manaverse-d9c62405db9a3f8bffaa5789bd63f59a65e29e3a.zip
improve messagein class.
-rw-r--r--src/net/eathena/messagein.cpp6
-rw-r--r--src/net/eathena/messagein.h2
-rw-r--r--src/net/messagein.cpp42
-rw-r--r--src/net/messagein.h5
-rw-r--r--src/net/tmwa/messagein.cpp6
-rw-r--r--src/net/tmwa/messagein.h2
-rw-r--r--src/utils/stringutils.cpp7
-rw-r--r--src/utils/stringutils.h2
8 files changed, 40 insertions, 32 deletions
diff --git a/src/net/eathena/messagein.cpp b/src/net/eathena/messagein.cpp
index 923be96af..cb5f2ecfd 100644
--- a/src/net/eathena/messagein.cpp
+++ b/src/net/eathena/messagein.cpp
@@ -36,7 +36,7 @@
namespace EAthena
{
-MessageIn::MessageIn(const char *data, unsigned int length):
+MessageIn::MessageIn(const char *const data, const unsigned int length) :
Net::MessageIn(data, length)
{
// Read the message ID
@@ -58,7 +58,7 @@ int16_t MessageIn::readInt16()
}
mPos += 2;
PacketCounters::incInBytes(2);
- DEBUGLOG("readInt16: " + toString(static_cast<int>(value)));
+ DEBUGLOG("readInt16: " + toStringPrint(static_cast<int>(value)));
return value;
}
@@ -77,7 +77,7 @@ int32_t MessageIn::readInt32()
}
mPos += 4;
PacketCounters::incInBytes(4);
- DEBUGLOG(strprintf("readInt32: %u", value));
+ DEBUGLOG("readInt32: " + toStringPrint(value));
return value;
}
diff --git a/src/net/eathena/messagein.h b/src/net/eathena/messagein.h
index 47e3b3004..7279d8dcf 100644
--- a/src/net/eathena/messagein.h
+++ b/src/net/eathena/messagein.h
@@ -44,7 +44,7 @@ class MessageIn final : public Net::MessageIn
/**
* Constructor.
*/
- MessageIn(const char *data, unsigned int length);
+ MessageIn(const char *const data, const unsigned int length);
A_DELETE_COPY(MessageIn)
diff --git a/src/net/messagein.cpp b/src/net/messagein.cpp
index 2858368e2..a833c6cde 100644
--- a/src/net/messagein.cpp
+++ b/src/net/messagein.cpp
@@ -54,7 +54,7 @@ unsigned char MessageIn::readInt8()
mPos += 1;
PacketCounters::incInBytes(1);
- DEBUGLOG("readInt8: " + toString(static_cast<int>(value)));
+ DEBUGLOG("readInt8: " + toStringPrint(static_cast<int>(value)));
return value;
}
@@ -62,7 +62,7 @@ void MessageIn::readCoordinates(uint16_t &x, uint16_t &y)
{
if (mPos + 3 <= mLength)
{
- unsigned char const *const p
+ const unsigned char *const p
= reinterpret_cast<unsigned char const *const>(mData + mPos);
x = static_cast<short unsigned>(p[0] | ((p[1] & 0x07) << 8));
y = static_cast<short unsigned>((p[1] >> 3) | ((p[2] & 0x3F) << 5));
@@ -74,7 +74,7 @@ void MessageIn::readCoordinates(uint16_t &x, uint16_t &y)
static_cast<int>(y))));
}
-uint8_t MessageIn::fromServerDirection(uint8_t serverDir)
+uint8_t MessageIn::fromServerDirection(const uint8_t serverDir)
{
// Translate from eAthena format
switch (serverDir)
@@ -113,9 +113,7 @@ void MessageIn::readCoordinates(uint16_t &x, uint16_t &y, uint8_t &direction)
if (mPos + 3 <= mLength)
{
const char *const data = mData + mPos;
- int16_t temp;
-
- temp = MAKEWORD(data[1] & 0x00c0, data[0] & 0x00ff);
+ int16_t temp = MAKEWORD(data[1] & 0x00c0, data[0] & 0x00ff);
x = static_cast<unsigned short>(temp >> 6);
temp = MAKEWORD(data[2] & 0x00f0, data[1] & 0x003f);
y = static_cast<unsigned short>(temp >> 4);
@@ -137,9 +135,7 @@ void MessageIn::readCoordinatePair(uint16_t &srcX, uint16_t &srcY,
if (mPos + 5 <= mLength)
{
const char *const data = mData + mPos;
- int16_t temp;
-
- temp = MAKEWORD(data[3], data[2] & 0x000f);
+ int16_t temp = MAKEWORD(data[3], data[2] & 0x000f);
dstX = static_cast<unsigned short>(temp >> 2);
dstY = MAKEWORD(data[4], data[3] & 0x0003);
@@ -159,7 +155,7 @@ void MessageIn::readCoordinatePair(uint16_t &srcX, uint16_t &srcY,
PacketCounters::incInBytes(5);
}
-void MessageIn::skip(unsigned int length)
+void MessageIn::skip(const unsigned int length)
{
mPos += length;
PacketCounters::incInBytes(length);
@@ -181,11 +177,12 @@ std::string MessageIn::readString(int length)
}
// Read the string
- char const *const stringBeg = mData + mPos;
- char const *const stringEnd
- = static_cast<char const *>(memchr(stringBeg, '\0', length));
+ const char *const stringBeg = mData + mPos;
+ const char *const stringEnd
+ = static_cast<const char *const>(memchr(stringBeg, '\0', length));
- std::string str(stringBeg, stringEnd ? stringEnd - stringBeg : length);
+ const std::string str(stringBeg, stringEnd
+ ? stringEnd - stringBeg : length);
mPos += length;
PacketCounters::incInBytes(length);
DEBUGLOG("readString: " + str);
@@ -206,10 +203,11 @@ std::string MessageIn::readRawString(int length)
}
// Read the string
- char const *const stringBeg = mData + mPos;
- char const *const stringEnd
- = static_cast<char const *const>(memchr(stringBeg, '\0', length));
- std::string str(stringBeg, stringEnd ? stringEnd - stringBeg : length);
+ const char *const stringBeg = mData + mPos;
+ const char *const stringEnd
+ = static_cast<const char *const>(memchr(stringBeg, '\0', length));
+ std::string str(stringBeg, stringEnd
+ ? stringEnd - stringBeg : length);
mPos += length;
PacketCounters::incInBytes(length);
@@ -218,10 +216,10 @@ std::string MessageIn::readRawString(int length)
if (stringEnd)
{
const long len2 = length - (stringEnd - stringBeg) - 1;
- char const *const stringBeg2 = stringEnd + 1;
- char const *const stringEnd2
- = static_cast<char const *const>(memchr(stringBeg2, '\0', len2));
- std::string hiddenPart = std::string(stringBeg2,
+ const char *const stringBeg2 = stringEnd + 1;
+ const char *const stringEnd2
+ = static_cast<const char *const>(memchr(stringBeg2, '\0', len2));
+ const std::string hiddenPart = std::string(stringBeg2,
stringEnd2 ? stringEnd2 - stringBeg2 : len2);
if (hiddenPart.length() > 0)
{
diff --git a/src/net/messagein.h b/src/net/messagein.h
index c6c5ce7eb..2f152924d 100644
--- a/src/net/messagein.h
+++ b/src/net/messagein.h
@@ -89,7 +89,7 @@ class MessageIn
/**
* Skips a given number of bytes.
*/
- virtual void skip(unsigned int length);
+ virtual void skip(const unsigned int length);
/**
* Reads a string. If a length is not given (-1), it is assumed
@@ -105,7 +105,8 @@ class MessageIn
virtual ~MessageIn()
{ }
- static uint8_t fromServerDirection(uint8_t serverDir) A_WARN_UNUSED;
+ static uint8_t fromServerDirection(const uint8_t serverDir)
+ A_WARN_UNUSED;
protected:
/**
diff --git a/src/net/tmwa/messagein.cpp b/src/net/tmwa/messagein.cpp
index 3b774f158..3414b0091 100644
--- a/src/net/tmwa/messagein.cpp
+++ b/src/net/tmwa/messagein.cpp
@@ -36,7 +36,7 @@
namespace TmwAthena
{
-MessageIn::MessageIn(const char *data, unsigned int length):
+MessageIn::MessageIn(const char *const data, const unsigned int length) :
Net::MessageIn(data, length)
{
// Read the message ID
@@ -58,7 +58,7 @@ int16_t MessageIn::readInt16()
}
mPos += 2;
PacketCounters::incInBytes(2);
- DEBUGLOG("readInt16: " + toString(static_cast<int>(value)));
+ DEBUGLOG("readInt16: " + toStringPrint(static_cast<int>(value)));
return value;
}
@@ -77,7 +77,7 @@ int32_t MessageIn::readInt32()
}
mPos += 4;
PacketCounters::incInBytes(4);
- DEBUGLOG(strprintf("readInt32: %u", static_cast<unsigned>(value)));
+ DEBUGLOG("readInt32:" + toStringPrint(value));
return value;
}
diff --git a/src/net/tmwa/messagein.h b/src/net/tmwa/messagein.h
index 8bd9641ab..a5db62334 100644
--- a/src/net/tmwa/messagein.h
+++ b/src/net/tmwa/messagein.h
@@ -44,7 +44,7 @@ class MessageIn final : public Net::MessageIn
/**
* Constructor.
*/
- MessageIn(const char *data, unsigned int length);
+ MessageIn(const char *const data, const unsigned int length);
A_DELETE_COPY(MessageIn)
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index f1fd8d82b..bd95ec2d7 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -676,3 +676,10 @@ std::string decodeLinkText(std::string data)
{
return replaceAll(data, "\342\235\230", "|");
}
+
+std::string toStringPrint(const unsigned int val)
+{
+ static char str[100];
+ sprintf(str, "%u 0x%x", val, val);
+ return str;
+}
diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h
index 8b3ae0563..6aff503e2 100644
--- a/src/utils/stringutils.h
+++ b/src/utils/stringutils.h
@@ -79,6 +79,8 @@ template<typename T> std::string toString(const T &arg)
return ss.str();
}
+std::string toStringPrint(const unsigned int val);
+
/**
* Converts the given IP address to a string.
*