diff options
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/messagein.cpp | 22 | ||||
-rw-r--r-- | src/net/messagein.hpp | 5 | ||||
-rw-r--r-- | src/net/messageout.cpp | 23 | ||||
-rw-r--r-- | src/net/messageout.hpp | 6 |
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); |