diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-05-12 16:27:55 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-05-12 16:27:55 +0200 |
commit | 38f40f5413ac3cd096fd1db84b25a3c9bc9f50db (patch) | |
tree | 2a1e5073b913d93a0a1f30dbf0d4fb0b85250654 /src/server/shared/Packets/ByteBuffer.cpp | |
parent | 1501843154157933304a3954205c3b1972bf4ed8 (diff) |
Core/PacketIO: Improved bit functions - no longer reads each bit separately
Diffstat (limited to 'src/server/shared/Packets/ByteBuffer.cpp')
-rw-r--r-- | src/server/shared/Packets/ByteBuffer.cpp | 82 |
1 files changed, 43 insertions, 39 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.cpp b/src/server/shared/Packets/ByteBuffer.cpp index 994cd371968..8811a2a6c0c 100644 --- a/src/server/shared/Packets/ByteBuffer.cpp +++ b/src/server/shared/Packets/ByteBuffer.cpp @@ -17,18 +17,14 @@ #include "ByteBuffer.h" #include "Errors.h" -#include "MessageBuffer.h" #include "Log.h" +#include "MessageBuffer.h" #include "Util.h" #include <utf8.h> #include <algorithm> #include <sstream> #include <cmath> -ByteBuffer::ByteBuffer(MessageBuffer&& buffer) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0), _storage(buffer.Move()) -{ -} - ByteBufferPositionException::ByteBufferPositionException(size_t pos, size_t size, size_t valueSize) : ByteBufferException(Trinity::StringFormat("Attempted to get value with size: {} in ByteBuffer (pos: {} size: {})", valueSize, pos, size)) { @@ -39,6 +35,10 @@ ByteBufferInvalidValueException::ByteBufferInvalidValueException(char const* typ { } +ByteBuffer::ByteBuffer(MessageBuffer&& buffer) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0), _storage(buffer.Move()) +{ +} + ByteBuffer& ByteBuffer::operator>>(float& value) { read(&value, 1); @@ -145,63 +145,67 @@ void ByteBuffer::PutBits(std::size_t pos, std::size_t value, uint32 bitCount) void ByteBuffer::print_storage() const { - if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output + Logger const* networkLogger = sLog->GetEnabledLogger("network", LOG_LEVEL_TRACE); + if (!networkLogger) // optimize disabled trace output return; std::ostringstream o; - o << "STORAGE_SIZE: " << size(); for (uint32 i = 0; i < size(); ++i) - o << read<uint8>(i) << " - "; - o << ' '; + o << uint32(_storage[i]) << " - "; - TC_LOG_TRACE("network", "{}", o.str()); + TC_LOG_TRACE("network", "STORAGE_SIZE: {} {}", size(), o.view()); } void ByteBuffer::textlike() const { - if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output + Logger const* networkLogger = sLog->GetEnabledLogger("network", LOG_LEVEL_TRACE); + if (networkLogger) // optimize disabled trace output return; std::ostringstream o; - o << "STORAGE_SIZE: " << size(); for (uint32 i = 0; i < size(); ++i) - { - char buf[2]; - snprintf(buf, 2, "%c", read<uint8>(i)); - o << buf; - } - o << ' '; - TC_LOG_TRACE("network", "{}", o.str()); + o << char(_storage[i]); + + sLog->OutMessageTo(networkLogger, "network", LOG_LEVEL_TRACE, "STORAGE_SIZE: {} {}", size(), o.view()); } void ByteBuffer::hexlike() const { - if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output + Logger const* networkLogger = sLog->GetEnabledLogger("network", LOG_LEVEL_TRACE); + if (!networkLogger) // optimize disabled trace output return; - uint32 j = 1, k = 1; - std::ostringstream o; - o << "STORAGE_SIZE: " << size(); + o.setf(std::ios_base::hex, std::ios_base::basefield); + o.fill('0'); - for (uint32 i = 0; i < size(); ++i) + for (uint32 i = 0; i < size(); ) { - char buf[4]; - snprintf(buf, 4, "%02X", read<uint8>(i)); - if ((i == (j * 8)) && ((i != (k * 16)))) + char const* sep = " | "; + for (uint32 j = 0; j < 2; ++j) { - o << "| "; - ++j; + for (uint32 k = 0; k < 8; ++k) + { + o.width(2); + o << _storage[i]; + ++i; + } + + o << sep; + sep = "\n"; } - else if (i == (k * 16)) - { - o << '\n'; - ++k; - ++j; - } - - o << buf; } - o << ' '; - TC_LOG_TRACE("network", "{}", o.str()); + + sLog->OutMessageTo(networkLogger, "network", LOG_LEVEL_TRACE, "STORAGE_SIZE: {} {}", size(), o.view()); } + +template TC_SHARED_API uint8 ByteBuffer::read<uint8>(); +template TC_SHARED_API uint16 ByteBuffer::read<uint16>(); +template TC_SHARED_API uint32 ByteBuffer::read<uint32>(); +template TC_SHARED_API uint64 ByteBuffer::read<uint64>(); +template TC_SHARED_API int8 ByteBuffer::read<int8>(); +template TC_SHARED_API int16 ByteBuffer::read<int16>(); +template TC_SHARED_API int32 ByteBuffer::read<int32>(); +template TC_SHARED_API int64 ByteBuffer::read<int64>(); +template TC_SHARED_API float ByteBuffer::read<float>(); +template TC_SHARED_API double ByteBuffer::read<double>(); |