diff options
| author | Shauren <shauren.trinity@gmail.com> | 2017-05-13 11:49:09 +0200 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2017-05-13 11:49:09 +0200 |
| commit | beb3316089b350a43e917d896e682298df8dcfc0 (patch) | |
| tree | 8006c52594009f226d834991d101c97057699594 /src/server/shared/Packets | |
| parent | c00316d3d4b57826cc8e38feed24faf84832c04b (diff) | |
Core/Shared: Include cleanup
Diffstat (limited to 'src/server/shared/Packets')
| -rw-r--r-- | src/server/shared/Packets/ByteBuffer.cpp | 78 | ||||
| -rw-r--r-- | src/server/shared/Packets/ByteBuffer.h | 96 |
2 files changed, 91 insertions, 83 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.cpp b/src/server/shared/Packets/ByteBuffer.cpp index efc5e587168..d06e18ff234 100644 --- a/src/server/shared/Packets/ByteBuffer.cpp +++ b/src/server/shared/Packets/ByteBuffer.cpp @@ -17,10 +17,12 @@ */ #include "ByteBuffer.h" +#include "Errors.h" #include "MessageBuffer.h" -#include "Common.h" #include "Log.h" +#include "Util.h" #include <sstream> +#include <ctime> ByteBuffer::ByteBuffer(MessageBuffer&& buffer) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0), _storage(buffer.Move()) { @@ -37,6 +39,80 @@ ByteBufferPositionException::ByteBufferPositionException(size_t pos, size_t size message().assign(ss.str()); } +ByteBuffer& ByteBuffer::operator>>(float& value) +{ + value = read<float>(); + if (!std::isfinite(value)) + throw ByteBufferException(); + return *this; +} + +ByteBuffer& ByteBuffer::operator>>(double& value) +{ + value = read<double>(); + if (!std::isfinite(value)) + throw ByteBufferException(); + return *this; +} + +uint32 ByteBuffer::ReadPackedTime() +{ + uint32 packedDate = read<uint32>(); + tm lt = tm(); + + lt.tm_min = packedDate & 0x3F; + lt.tm_hour = (packedDate >> 6) & 0x1F; + //lt.tm_wday = (packedDate >> 11) & 7; + lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1; + lt.tm_mon = (packedDate >> 20) & 0xF; + lt.tm_year = ((packedDate >> 24) & 0x1F) + 100; + + return uint32(mktime(<)); +} + +void ByteBuffer::append(const uint8 *src, size_t cnt) +{ + ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size()); + ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size()); + ASSERT(size() < 10000000); + + FlushBits(); + _storage.insert(_storage.begin() + _wpos, src, src + cnt); + _wpos += cnt; +} + +void ByteBuffer::AppendPackedTime(time_t time) +{ + tm lt; + localtime_r(&time, <); + append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min); +} + +void ByteBuffer::put(size_t pos, const uint8 *src, size_t cnt) +{ + ASSERT(pos + cnt <= size(), "Attempted to put value with size: " SZFMTD " in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", cnt, pos, size()); + ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size()); + ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size()); + + std::memcpy(&_storage[pos], src, cnt); +} + +void ByteBuffer::PutBits(std::size_t pos, std::size_t value, uint32 bitCount) +{ + ASSERT(pos + bitCount <= size() * 8, "Attempted to put %u bits in ByteBuffer (bitpos: " SZFMTD " size: " SZFMTD ")", bitCount, pos, size()); + ASSERT(bitCount, "Attempted to put a zero bits in ByteBuffer"); + + for (uint32 i = 0; i < bitCount; ++i) + { + std::size_t wp = (pos + i) / 8; + std::size_t bit = (pos + i) % 8; + if ((value >> (bitCount - i - 1)) & 1) + _storage[wp] |= 1 << (7 - bit); + else + _storage[wp] &= ~(1 << (7 - bit)); + } +} + void ByteBuffer::print_storage() const { if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h index 82cc00a0f4e..93c6aaf83f8 100644 --- a/src/server/shared/Packets/ByteBuffer.h +++ b/src/server/shared/Packets/ByteBuffer.h @@ -20,11 +20,10 @@ #define _BYTEBUFFER_H #include "Define.h" -#include "Errors.h" #include "ByteConverter.h" -#include "Util.h" +#include <string> +#include <vector> #include <cstring> -#include <ctime> class MessageBuffer; @@ -151,7 +150,7 @@ class TC_SHARED_API ByteBuffer _curbitval = 0; } - bool WriteBit(uint32 bit) + bool WriteBit(bool bit) { --_bitpos; if (bit) @@ -164,7 +163,7 @@ class TC_SHARED_API ByteBuffer _curbitval = 0; } - return (bit != 0); + return bit; } bool ReadBit() @@ -179,7 +178,7 @@ class TC_SHARED_API ByteBuffer return ((_curbitval >> (7-_bitpos)) & 1) != 0; } - template <typename T> void WriteBits(T value, int32 bits) + void WriteBits(std::size_t value, int32 bits) { for (int32 i = bits - 1; i >= 0; --i) WriteBit((value >> i) & 1); @@ -208,7 +207,8 @@ class TC_SHARED_API ByteBuffer append<uint8>(b ^ 1); } - template <typename T> void put(size_t pos, T value) + template <typename T> + void put(std::size_t pos, T value) { static_assert(std::is_fundamental<T>::value, "append(compound)"); EndianConvert(value); @@ -227,22 +227,7 @@ class TC_SHARED_API ByteBuffer * @param value Data to write. * @param bitCount Number of bits to store the value on. */ - template <typename T> - void PutBits(size_t pos, T value, uint32 bitCount) - { - ASSERT(pos + bitCount <= size() * 8, "Attempted to put " SZFMTD " bits in ByteBuffer (bitpos: " SZFMTD " size: " SZFMTD ")", bitCount, pos, size()); - ASSERT(bitCount, "Attempted to put a zero bits in ByteBuffer"); - - for (uint32 i = 0; i < bitCount; ++i) - { - size_t wp = (pos + i) / 8; - size_t bit = (pos + i) % 8; - if ((value >> (bitCount - i - 1)) & 1) - _storage[wp] |= 1 << (7 - bit); - else - _storage[wp] &= ~(1 << (7 - bit)); - } - } + void PutBits(std::size_t pos, std::size_t value, uint32 bitCount); ByteBuffer &operator<<(uint8 value) { @@ -377,21 +362,8 @@ class TC_SHARED_API ByteBuffer return *this; } - ByteBuffer &operator>>(float &value) - { - value = read<float>(); - if (!std::isfinite(value)) - throw ByteBufferException(); - return *this; - } - - ByteBuffer &operator>>(double &value) - { - value = read<double>(); - if (!std::isfinite(value)) - throw ByteBufferException(); - return *this; - } + ByteBuffer &operator>>(float &value); + ByteBuffer &operator>>(double &value); ByteBuffer &operator>>(std::string& value) { @@ -531,26 +503,7 @@ class TC_SHARED_API ByteBuffer append(str, len); } - uint32 ReadPackedTime() - { - uint32 packedDate = read<uint32>(); - tm lt = tm(); - - lt.tm_min = packedDate & 0x3F; - lt.tm_hour = (packedDate >> 6) & 0x1F; - //lt.tm_wday = (packedDate >> 11) & 7; - lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1; - lt.tm_mon = (packedDate >> 20) & 0xF; - lt.tm_year = ((packedDate >> 24) & 0x1F) + 100; - - return uint32(mktime(<)); - } - - ByteBuffer& ReadPackedTime(uint32& time) - { - time = ReadPackedTime(); - return *this; - } + uint32 ReadPackedTime(); uint8* contents() { @@ -592,16 +545,7 @@ class TC_SHARED_API ByteBuffer return append((const uint8 *)src, cnt * sizeof(T)); } - void append(const uint8 *src, size_t cnt) - { - ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size()); - ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size()); - ASSERT(size() < 10000000); - - FlushBits(); - _storage.insert(_storage.begin() + _wpos, src, src + cnt); - _wpos += cnt; - } + void append(const uint8 *src, size_t cnt); void append(const ByteBuffer& buffer) { @@ -652,21 +596,9 @@ class TC_SHARED_API ByteBuffer return resultSize; } - void AppendPackedTime(time_t time) - { - tm lt; - localtime_r(&time, <); - append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min); - } - - void put(size_t pos, const uint8 *src, size_t cnt) - { - ASSERT(pos + cnt <= size(), "Attempted to put value with size: " SZFMTD " in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", cnt, pos, size()); - ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size()); - ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size()); + void AppendPackedTime(time_t time); - std::memcpy(&_storage[pos], src, cnt); - } + void put(size_t pos, const uint8 *src, size_t cnt); void print_storage() const; |
