From 86eff0b2307eb4c462d88d6aa2cbd915ffdfefcc Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 27 Jun 2025 19:52:11 -0500 Subject: [PATCH] improve buffer consistency --- lib/Buffer.hpp | 13 ++++---- lib/Utilities.hpp | 80 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 28 deletions(-) diff --git a/lib/Buffer.hpp b/lib/Buffer.hpp index c44b42a..6f4dbd3 100644 --- a/lib/Buffer.hpp +++ b/lib/Buffer.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace reactor { @@ -105,13 +106,13 @@ public: void appendInt16(uint16_t x) { - uint16_t be = htons(x); + uint16_t be = hton16(x); append(reinterpret_cast(&be), sizeof(be)); } void appendInt32(uint32_t x) { - uint32_t be = htonl(x); + uint32_t be = hton32(x); append(reinterpret_cast(&be), sizeof(be)); } @@ -161,14 +162,14 @@ public: { assert(readableBytes() >= sizeof(uint16_t)); uint16_t be = *reinterpret_cast(peek()); - return ntohs(be); + return ntoh16(be); } uint32_t peekInt32() const { assert(readableBytes() >= sizeof(uint32_t)); uint32_t be = *reinterpret_cast(peek()); - return ntohl(be); + return ntoh32(be); } uint64_t peekInt64() const @@ -193,13 +194,13 @@ public: void prependInt16(uint16_t x) { - uint16_t be = htons(x); + uint16_t be = hton16(x); prepend(reinterpret_cast(&be), sizeof(be)); } void prependInt32(uint32_t x) { - uint32_t be = htonl(x); + uint32_t be = hton32(x); prepend(reinterpret_cast(&be), sizeof(be)); } diff --git a/lib/Utilities.hpp b/lib/Utilities.hpp index 05ea62f..6083539 100644 --- a/lib/Utilities.hpp +++ b/lib/Utilities.hpp @@ -24,14 +24,23 @@ namespace reactor { -// Use compiler intrinsics for efficient byte swapping -#if defined(_MSC_VER) - #include - #define bswap_64(x) _byteswap_uint64(x) -#elif defined(__GNUC__) || defined(__clang__) +#if defined(__GNUC__) || defined(__clang__) + #define bswap_16(x) __builtin_bswap16(x) + #define bswap_32(x) __builtin_bswap32(x) #define bswap_64(x) __builtin_bswap64(x) #else - // Generic fallback implementation + // Generic fallback implementations + inline uint16_t bswap_16(uint16_t val) + { + return (val << 8) | (val >> 8); + } + + inline uint32_t bswap_32(uint32_t val) + { + val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); + return (val << 16) | (val >> 16); + } + inline uint64_t bswap_64(uint64_t val) { uint64_t temp = val; @@ -41,6 +50,49 @@ namespace reactor } #endif +// C++20 Network byte order utilities +inline uint16_t hton16(uint16_t host_uint16) +{ + if constexpr (std::endian::native == std::endian::little) { + return bswap_16(host_uint16); + } else { + return host_uint16; + } +} + +inline uint32_t hton32(uint32_t host_uint32) +{ + if constexpr (std::endian::native == std::endian::little) { + return bswap_32(host_uint32); + } else { + return host_uint32; + } +} + +inline uint64_t hton64(uint64_t host_uint64) +{ + if constexpr (std::endian::native == std::endian::little) { + return bswap_64(host_uint64); + } else { + return host_uint64; + } +} + +inline uint16_t ntoh16(uint16_t net_uint16) +{ + return hton16(net_uint16); +} + +inline uint32_t ntoh32(uint32_t net_uint32) +{ + return hton32(net_uint32); +} + +inline uint64_t ntoh64(uint64_t net_uint64) +{ + return hton64(net_uint64); +} + // NonCopyable base class (unchanged, follows modern practice) class NonCopyable { @@ -53,22 +105,6 @@ protected: NonCopyable& operator=(NonCopyable&&) noexcept = default; }; -// C++20 Network byte order utilities -inline uint64_t hton64(uint64_t host_uint64) -{ - if constexpr (std::endian::native == std::endian::little) { - return bswap_64(host_uint64); - } else { - return host_uint64; - } -} - -inline uint64_t ntoh64(uint64_t net_uint64) -{ - return hton64(net_uint64); -} - - // Object Pool (unchanged, this is a standard pattern) template class ObjectPool : public NonCopyable, public std::enable_shared_from_this>