summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2011-06-02 23:49:49 -0700
committerJared Adams <jaxad0127@gmail.com>2011-06-03 11:03:25 -0600
commit894038adf52a3e2b42542239a147d6c1cc1ad204 (patch)
treeb881994e4605bcf0ba2d1df68068a78fb21ecf01
parentd50f35d13e73b72eae1d744971657bd665aa1f33 (diff)
downloadmana-894038adf52a3e2b42542239a147d6c1cc1ad204.tar.gz
mana-894038adf52a3e2b42542239a147d6c1cc1ad204.tar.bz2
mana-894038adf52a3e2b42542239a147d6c1cc1ad204.tar.xz
mana-894038adf52a3e2b42542239a147d6c1cc1ad204.zip
Cleanup network code to use unsigned integers of the relevant size
-rw-r--r--src/net/manaserv/messagein.cpp8
-rw-r--r--src/net/manaserv/messagein.h4
-rw-r--r--src/net/manaserv/messageout.cpp6
-rw-r--r--src/net/manaserv/messageout.h6
-rw-r--r--src/net/messagein.cpp8
-rw-r--r--src/net/messagein.h8
-rw-r--r--src/net/messageout.cpp4
-rw-r--r--src/net/messageout.h6
-rw-r--r--src/net/tmwa/messagein.cpp28
-rw-r--r--src/net/tmwa/messagein.h4
-rw-r--r--src/net/tmwa/messageout.cpp47
-rw-r--r--src/net/tmwa/messageout.h10
12 files changed, 55 insertions, 84 deletions
diff --git a/src/net/manaserv/messagein.cpp b/src/net/manaserv/messagein.cpp
index 592182f7..128dd14f 100644
--- a/src/net/manaserv/messagein.cpp
+++ b/src/net/manaserv/messagein.cpp
@@ -32,9 +32,9 @@ MessageIn::MessageIn(const char *data, unsigned int length):
mId = readInt16();
}
-int MessageIn::readInt16()
+Uint16 MessageIn::readInt16()
{
- int value = -1;
+ Uint16 value = 0;
if (mPos + 2 <= mLength)
{
uint16_t t;
@@ -45,9 +45,9 @@ int MessageIn::readInt16()
return value;
}
-int MessageIn::readInt32()
+Uint32 MessageIn::readInt32()
{
- int value = -1;
+ Uint32 value = 0;
if (mPos + 4 <= mLength)
{
uint32_t t;
diff --git a/src/net/manaserv/messagein.h b/src/net/manaserv/messagein.h
index 6d0a4035..ed6510eb 100644
--- a/src/net/manaserv/messagein.h
+++ b/src/net/manaserv/messagein.h
@@ -36,8 +36,8 @@ class MessageIn : public Net::MessageIn
public:
MessageIn(const char *data, unsigned int length);
- int readInt16(); /**< Reads a short. */
- int readInt32(); /**< Reads a long. */
+ Uint16 readInt16(); /**< Reads a short. */
+ Uint32 readInt32(); /**< Reads a "long". */
};
}
diff --git a/src/net/manaserv/messageout.cpp b/src/net/manaserv/messageout.cpp
index 8779c5f6..695b1b39 100644
--- a/src/net/manaserv/messageout.cpp
+++ b/src/net/manaserv/messageout.cpp
@@ -28,7 +28,7 @@
namespace ManaServ {
-MessageOut::MessageOut(short id):
+MessageOut::MessageOut(Uint16 id):
Net::MessageOut(id)
{
writeInt16(id);
@@ -45,7 +45,7 @@ void MessageOut::expand(size_t bytes)
mDataSize = mPos + bytes;
}
-void MessageOut::writeInt16(Sint16 value)
+void MessageOut::writeInt16(Uint16 value)
{
expand(2);
uint16_t t = ENET_HOST_TO_NET_16(value);
@@ -53,7 +53,7 @@ void MessageOut::writeInt16(Sint16 value)
mPos += 2;
}
-void MessageOut::writeInt32(Sint32 value)
+void MessageOut::writeInt32(Uint32 value)
{
expand(4);
uint32_t t = ENET_HOST_TO_NET_32(value);
diff --git a/src/net/manaserv/messageout.h b/src/net/manaserv/messageout.h
index 451bc0aa..a22ea824 100644
--- a/src/net/manaserv/messageout.h
+++ b/src/net/manaserv/messageout.h
@@ -29,12 +29,12 @@ namespace ManaServ {
class MessageOut : public Net::MessageOut
{
public:
- MessageOut(short id);
+ MessageOut(Uint16 id);
~MessageOut();
- void writeInt16(Sint16 value); /**< Writes a short. */
- void writeInt32(Sint32 value); /**< Writes a long. */
+ void writeInt16(Uint16 value); /**< Writes a short. */
+ void writeInt32(Uint32 value); /**< Writes a "long". */
protected:
/**
diff --git a/src/net/messagein.cpp b/src/net/messagein.cpp
index 3c3e9edf..d9392379 100644
--- a/src/net/messagein.cpp
+++ b/src/net/messagein.cpp
@@ -34,14 +34,14 @@ MessageIn::MessageIn(const char *data, unsigned int length):
{
}
-int MessageIn::readInt8()
+Uint8 MessageIn::readInt8()
{
- int value = -1;
+ Uint8 value = 0;
if (mPos < mLength)
{
- value = (unsigned char) mData[mPos];
+ value = mData[mPos];
}
- mPos += 1;
+ mPos++;
return value;
}
diff --git a/src/net/messagein.h b/src/net/messagein.h
index 726c3bab..1313b8f7 100644
--- a/src/net/messagein.h
+++ b/src/net/messagein.h
@@ -39,7 +39,7 @@ class MessageIn
/**
* Returns the message ID.
*/
- int getId() const { return mId; }
+ Uint16 getId() const { return mId; }
/**
* Returns the message length.
@@ -51,9 +51,9 @@ class MessageIn
*/
unsigned int getUnreadLength() const { return mLength - mPos; }
- virtual int readInt8(); /**< Reads a byte. */
- virtual int readInt16() = 0; /**< Reads a short. */
- virtual int readInt32() = 0; /**< Reads a long. */
+ virtual Uint8 readInt8(); /**< Reads a byte. */
+ virtual Uint16 readInt16() = 0; /**< Reads a short. */
+ virtual Uint32 readInt32() = 0; /**< Reads a "long". */
/**
* Reads a 3-byte block containing tile-based coordinates. Used by
diff --git a/src/net/messageout.cpp b/src/net/messageout.cpp
index 0ac63935..5663e1df 100644
--- a/src/net/messageout.cpp
+++ b/src/net/messageout.cpp
@@ -33,7 +33,7 @@ MessageOut::MessageOut(short id):
{
}
-void MessageOut::writeInt8(Sint8 value)
+void MessageOut::writeInt8(Uint8 value)
{
expand(1);
mData[mPos] = value;
@@ -57,7 +57,7 @@ void MessageOut::writeString(const std::string &string, int length)
expand(length);
// Write the actual string
- memcpy(mData + mPos, string.c_str(), stringLength);
+ memcpy(mData + mPos, string.data(), stringLength);
// Pad remaining space with zeros
if (length > stringLength)
diff --git a/src/net/messageout.h b/src/net/messageout.h
index 05a55422..e05d3bd7 100644
--- a/src/net/messageout.h
+++ b/src/net/messageout.h
@@ -36,9 +36,9 @@ namespace Net {
class MessageOut
{
public:
- virtual void writeInt8(Sint8 value); /**< Writes a byte. */
- virtual void writeInt16(Sint16 value) = 0; /**< Writes a short. */
- virtual void writeInt32(Sint32 value) = 0; /**< Writes a long. */
+ virtual void writeInt8(Uint8 value); /**< Writes a byte. */
+ virtual void writeInt16(Uint16 value) = 0; /**< Writes a short. */
+ virtual void writeInt32(Uint32 value) = 0; /**< Writes a "long". */
/**
* Writes a string. If a fixed length is not given (-1), it is stored
diff --git a/src/net/tmwa/messagein.cpp b/src/net/tmwa/messagein.cpp
index 0074cfdd..1a860a25 100644
--- a/src/net/tmwa/messagein.cpp
+++ b/src/net/tmwa/messagein.cpp
@@ -24,10 +24,6 @@
#include <SDL.h>
#include <SDL_endian.h>
-#define MAKEWORD(low,high) \
- ((unsigned short)(((unsigned char)(low)) | \
- ((unsigned short)((unsigned char)(high))) << 8))
-
namespace TmwAthena {
MessageIn::MessageIn(const char *data, unsigned int length):
@@ -37,35 +33,23 @@ MessageIn::MessageIn(const char *data, unsigned int length):
mId = readInt16();
}
-int MessageIn::readInt16()
+Uint16 MessageIn::readInt16()
{
- Sint16 value = -1;
+ Uint16 value = 0;
if (mPos + 2 <= mLength)
{
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- Sint16 swap;
- memcpy(&swap, mData + mPos, sizeof(Sint16));
- value = SDL_Swap16(swap);
-#else
- memcpy(&value, mData + mPos, sizeof(Sint16));
-#endif
+ value = (mData[mPos + 1] << 8) | mData[mPos];
}
mPos += 2;
return value;
}
-int MessageIn::readInt32()
+Uint32 MessageIn::readInt32()
{
- Sint32 value = -1;
+ Uint32 value = 0;
if (mPos + 4 <= mLength)
{
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- Sint32 swap;
- memcpy(&swap, mData + mPos, sizeof(Sint32));
- value = SDL_Swap32(swap);
-#else
- memcpy(&value, mData + mPos, sizeof(Sint32));
-#endif
+ value = (mData[mPos + 3] << 24) | (mData[mPos + 2] << 16) | (mData[mPos + 1] << 8) | mData[mPos];
}
mPos += 4;
return value;
diff --git a/src/net/tmwa/messagein.h b/src/net/tmwa/messagein.h
index 13d6adf5..21375257 100644
--- a/src/net/tmwa/messagein.h
+++ b/src/net/tmwa/messagein.h
@@ -39,8 +39,8 @@ namespace TmwAthena {
public:
MessageIn(const char *data, unsigned int length);
- int readInt16(); /**< Reads a short. */
- int readInt32(); /**< Reads a long. */
+ Uint16 readInt16(); /**< Reads a short. */
+ Uint32 readInt32(); /**< Reads a "long". */
};
}
diff --git a/src/net/tmwa/messageout.cpp b/src/net/tmwa/messageout.cpp
index 8b407c47..71b8f756 100644
--- a/src/net/tmwa/messageout.cpp
+++ b/src/net/tmwa/messageout.cpp
@@ -31,7 +31,7 @@
namespace TmwAthena {
-MessageOut::MessageOut(short id):
+MessageOut::MessageOut(Uint16 id):
Net::MessageOut(id)
{
mNetwork = TmwAthena::Network::instance();
@@ -44,52 +44,39 @@ void MessageOut::expand(size_t bytes)
mNetwork->mOutSize += bytes;
}
-void MessageOut::writeInt16(Sint16 value)
+void MessageOut::writeInt16(Uint16 value)
{
expand(2);
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- Sint16 swap=SDL_Swap16(value);
- memcpy(mData + mPos, &swap, sizeof(Sint16));
-#else
- memcpy(mData + mPos, &value, sizeof(Sint16));
-#endif
+ mData[mPos] = value;
+ mData[mPos + 1] = value >> 8;
mPos += 2;
}
-void MessageOut::writeInt32(Sint32 value)
+void MessageOut::writeInt32(Uint32 value)
{
expand(4);
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- Sint32 swap=SDL_Swap32(value);
- memcpy(mData + mPos, &swap, sizeof(Sint32));
-#else
- memcpy(mData + mPos, &value, sizeof(Sint32));
-#endif
+ mData[mPos] = value;
+ mData[mPos + 1] = value >> 8;
+ mData[mPos + 2] = value >> 16;
+ mData[mPos + 3] = value >> 24;
mPos += 4;
}
-#define LOBYTE(w) ((unsigned char)(w))
-#define HIBYTE(w) ((unsigned char)(((unsigned short)(w)) >> 8))
-
-void MessageOut::writeCoordinates(unsigned short x, unsigned short y,
- unsigned char direction)
+void MessageOut::writeCoordinates(Uint16 x, Uint16 y, Uint8 direction)
{
char *data = mData + mPos;
mNetwork->mOutSize += 3;
mPos += 3;
- short temp;
- temp = x;
+ Uint16 temp = x;
temp <<= 6;
- data[0] = 0;
- data[1] = 1;
- data[2] = 2;
- data[0] = HIBYTE(temp);
- data[1] = (unsigned char) temp;
+ data[0] = temp >> 8;
+ data[1] = temp;
+
temp = y;
temp <<= 4;
- data[1] |= HIBYTE(temp);
- data[2] = LOBYTE(temp);
+ data[1] |= temp << 8;
+ data[2] = temp;
// Translate direction to eAthena format
switch (direction)
@@ -120,7 +107,7 @@ void MessageOut::writeCoordinates(unsigned short x, unsigned short y,
break;
default:
// OOPSIE! Impossible or unknown
- direction = (unsigned char) -1;
+ direction = 15;
}
data[2] |= direction;
}
diff --git a/src/net/tmwa/messageout.h b/src/net/tmwa/messageout.h
index 34958e82..3a6f34a9 100644
--- a/src/net/tmwa/messageout.h
+++ b/src/net/tmwa/messageout.h
@@ -39,16 +39,16 @@ class Network;
class MessageOut : public Net::MessageOut
{
public:
- MessageOut(short id);
+ MessageOut(Uint16 id);
- void writeInt16(Sint16 value); /**< Writes a short. */
- void writeInt32(Sint32 value); /**< Writes a long. */
+ void writeInt16(Uint16 value); /**< Writes a short. */
+ void writeInt32(Uint32 value); /**< Writes a "long". */
/**
* Encodes coordinates and direction in 3 bytes.
*/
- void writeCoordinates(unsigned short x, unsigned short y,
- unsigned char direction);
+ void writeCoordinates(Uint16 x, Uint16 y,
+ Uint8 direction);
private:
void expand(size_t size);