improve buffer consistency
This commit is contained in:
parent
200ceccf5e
commit
86eff0b230
@ -6,6 +6,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace reactor {
|
namespace reactor {
|
||||||
|
|
||||||
@ -105,13 +106,13 @@ public:
|
|||||||
|
|
||||||
void appendInt16(uint16_t x)
|
void appendInt16(uint16_t x)
|
||||||
{
|
{
|
||||||
uint16_t be = htons(x);
|
uint16_t be = hton16(x);
|
||||||
append(reinterpret_cast<const char*>(&be), sizeof(be));
|
append(reinterpret_cast<const char*>(&be), sizeof(be));
|
||||||
}
|
}
|
||||||
|
|
||||||
void appendInt32(uint32_t x)
|
void appendInt32(uint32_t x)
|
||||||
{
|
{
|
||||||
uint32_t be = htonl(x);
|
uint32_t be = hton32(x);
|
||||||
append(reinterpret_cast<const char*>(&be), sizeof(be));
|
append(reinterpret_cast<const char*>(&be), sizeof(be));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,14 +162,14 @@ public:
|
|||||||
{
|
{
|
||||||
assert(readableBytes() >= sizeof(uint16_t));
|
assert(readableBytes() >= sizeof(uint16_t));
|
||||||
uint16_t be = *reinterpret_cast<const uint16_t*>(peek());
|
uint16_t be = *reinterpret_cast<const uint16_t*>(peek());
|
||||||
return ntohs(be);
|
return ntoh16(be);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t peekInt32() const
|
uint32_t peekInt32() const
|
||||||
{
|
{
|
||||||
assert(readableBytes() >= sizeof(uint32_t));
|
assert(readableBytes() >= sizeof(uint32_t));
|
||||||
uint32_t be = *reinterpret_cast<const uint32_t*>(peek());
|
uint32_t be = *reinterpret_cast<const uint32_t*>(peek());
|
||||||
return ntohl(be);
|
return ntoh32(be);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t peekInt64() const
|
uint64_t peekInt64() const
|
||||||
@ -193,13 +194,13 @@ public:
|
|||||||
|
|
||||||
void prependInt16(uint16_t x)
|
void prependInt16(uint16_t x)
|
||||||
{
|
{
|
||||||
uint16_t be = htons(x);
|
uint16_t be = hton16(x);
|
||||||
prepend(reinterpret_cast<const char*>(&be), sizeof(be));
|
prepend(reinterpret_cast<const char*>(&be), sizeof(be));
|
||||||
}
|
}
|
||||||
|
|
||||||
void prependInt32(uint32_t x)
|
void prependInt32(uint32_t x)
|
||||||
{
|
{
|
||||||
uint32_t be = htonl(x);
|
uint32_t be = hton32(x);
|
||||||
prepend(reinterpret_cast<const char*>(&be), sizeof(be));
|
prepend(reinterpret_cast<const char*>(&be), sizeof(be));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,14 +24,23 @@
|
|||||||
namespace reactor
|
namespace reactor
|
||||||
{
|
{
|
||||||
|
|
||||||
// Use compiler intrinsics for efficient byte swapping
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
#if defined(_MSC_VER)
|
#define bswap_16(x) __builtin_bswap16(x)
|
||||||
#include <stdlib.h>
|
#define bswap_32(x) __builtin_bswap32(x)
|
||||||
#define bswap_64(x) _byteswap_uint64(x)
|
|
||||||
#elif defined(__GNUC__) || defined(__clang__)
|
|
||||||
#define bswap_64(x) __builtin_bswap64(x)
|
#define bswap_64(x) __builtin_bswap64(x)
|
||||||
#else
|
#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)
|
inline uint64_t bswap_64(uint64_t val)
|
||||||
{
|
{
|
||||||
uint64_t temp = val;
|
uint64_t temp = val;
|
||||||
@ -41,6 +50,49 @@ namespace reactor
|
|||||||
}
|
}
|
||||||
#endif
|
#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)
|
// NonCopyable base class (unchanged, follows modern practice)
|
||||||
class NonCopyable
|
class NonCopyable
|
||||||
{
|
{
|
||||||
@ -53,22 +105,6 @@ protected:
|
|||||||
NonCopyable& operator=(NonCopyable&&) noexcept = default;
|
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)
|
// Object Pool (unchanged, this is a standard pattern)
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class ObjectPool : public NonCopyable, public std::enable_shared_from_this<ObjectPool<T>>
|
class ObjectPool : public NonCopyable, public std::enable_shared_from_this<ObjectPool<T>>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user