summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorFreeyorp <Freeyorp101@hotmail.com>2010-08-29 19:47:25 +1200
committerFreeyorp <Freeyorp101@hotmail.com>2010-08-29 19:47:25 +1200
commit7fc50c2d31e1d289e9d2a950271c6d399fe0896a (patch)
tree1ebff71f7b1526425cc57e2e3b2681297e540f90 /src/net
parent853cbb6efdb79f879fabc2133acb8c11d9d4f7b1 (diff)
parent7db9f6fe36b737d2eec7c6070497035b0834def2 (diff)
downloadmanaserv-7fc50c2d31e1d289e9d2a950271c6d399fe0896a.tar.gz
manaserv-7fc50c2d31e1d289e9d2a950271c6d399fe0896a.tar.bz2
manaserv-7fc50c2d31e1d289e9d2a950271c6d399fe0896a.tar.xz
manaserv-7fc50c2d31e1d289e9d2a950271c6d399fe0896a.zip
Merge branch 'testing'
Conflicts: src/account-server/storage.cpp src/game-server/being.cpp src/game-server/being.hpp src/game-server/character.cpp src/game-server/character.hpp src/game-server/gamehandler.cpp src/game-server/inventory.cpp src/scripting/lua.cpp src/sql/mysql/createTables.sql src/sql/sqlite/createTables.sql
Diffstat (limited to 'src/net')
-rw-r--r--src/net/messagein.cpp22
-rw-r--r--src/net/messagein.hpp5
-rw-r--r--src/net/messageout.cpp23
-rw-r--r--src/net/messageout.hpp6
4 files changed, 56 insertions, 0 deletions
diff --git a/src/net/messagein.cpp b/src/net/messagein.cpp
index 45d1b21c..63fd8778 100644
--- a/src/net/messagein.cpp
+++ b/src/net/messagein.cpp
@@ -23,8 +23,12 @@
#include <iostream>
#include <string>
#include <enet/enet.h>
+#ifndef USE_NATIVE_DOUBLE
+#include <sstream>
+#endif
#include "net/messagein.hpp"
+#include "utils/logger.h"
MessageIn::MessageIn(const char *data, int length):
mData(data),
@@ -42,6 +46,7 @@ int MessageIn::readByte()
{
value = (unsigned char) mData[mPos];
}
+ else LOG_DEBUG("Unable to read 1 byte in " << this->getId() << "!");
mPos += 1;
return value;
}
@@ -55,6 +60,7 @@ int MessageIn::readShort()
memcpy(&t, mData + mPos, 2);
value = (unsigned short) ENET_NET_TO_HOST_16(t);
}
+ else LOG_DEBUG("Unable to read 2 bytes in " << this->getId() << "!");
mPos += 2;
return value;
}
@@ -68,10 +74,26 @@ int MessageIn::readLong()
memcpy(&t, mData + mPos, 4);
value = ENET_NET_TO_HOST_32(t);
}
+ else LOG_DEBUG("Unable to read 4 bytes in " << this->getId() << "!");
mPos += 4;
return value;
}
+double MessageIn::readDouble()
+{
+ double value;
+#ifdef USE_NATIVE_DOUBLE
+ if (mPos + sizeof(double) <= mLength)
+ memcpy(&value, mData + mPos, sizeof(double));
+ mPos += sizeof(double);
+#else
+ int length = readByte();
+ std::istringstream i (readString(length));
+ i >> value;
+#endif
+ return value;
+}
+
std::string MessageIn::readString(int length)
{
// Get string length
diff --git a/src/net/messagein.hpp b/src/net/messagein.hpp
index 71884190..c6e49ab5 100644
--- a/src/net/messagein.hpp
+++ b/src/net/messagein.hpp
@@ -47,6 +47,11 @@ class MessageIn
int readByte(); /**< Reads a byte. */
int readShort(); /**< Reads a short. */
int readLong(); /**< Reads a long. */
+ /**
+ * Reads a double. HACKY and should *not* be used for client
+ * communication!
+ */
+ double readDouble();
/**
* Reads a string. If a length is not given (-1), it is assumed
diff --git a/src/net/messageout.cpp b/src/net/messageout.cpp
index 8e6a4a7a..950da5f3 100644
--- a/src/net/messageout.cpp
+++ b/src/net/messageout.cpp
@@ -21,6 +21,10 @@
#include <cstring>
#include <iomanip>
#include <iostream>
+#ifndef USE_NATIVE_DOUBLE
+#include <limits>
+#include <sstream>
+#endif
#include <string>
#include <enet/enet.h>
@@ -98,6 +102,25 @@ void MessageOut::writeLong(int value)
mPos += 4;
}
+void MessageOut::writeDouble(double value)
+{
+#ifdef USE_NATIVE_DOUBLE
+ expand(mPos + sizeof(double));
+ memcpy(mData + mPos, &value, sizeof(double));
+ mPos += sizeof(double);
+#else
+// Rather inefficient, but I don't have a lot of time.
+// If anyone wants to implement a custom double you are more than welcome to.
+ std::ostringstream o;
+ // Highest precision for double
+ o.precision(std::numeric_limits< double >::digits10);
+ o << value;
+ std::string str = o.str();
+ writeByte(str.size());
+ writeString(str, str.size());
+#endif
+}
+
void MessageOut::writeCoordinates(int x, int y)
{
expand(mPos + 3);
diff --git a/src/net/messageout.hpp b/src/net/messageout.hpp
index cd7befa7..270b7963 100644
--- a/src/net/messageout.hpp
+++ b/src/net/messageout.hpp
@@ -56,6 +56,12 @@ class MessageOut
void writeLong(int value); /**< Writes an integer on four bytes. */
/**
+ * Writes a double. HACKY and should *not* be used for client
+ * communication!
+ */
+ void writeDouble(double value);
+
+ /**
* Writes a 3-byte block containing tile-based coordinates.
*/
void writeCoordinates(int x, int y);