rcpp_framework/libs/brynet/base/endian/Endian.hpp

116 lines
2.9 KiB
C++
Raw Normal View History

2020-11-24 15:41:18 +01:00
#pragma once
2021-04-30 16:10:14 +02:00
#include <brynet/net/SocketLibTypes.hpp>
2020-11-24 15:41:18 +01:00
#include <cassert>
#include <cstdbool>
2021-04-30 16:10:14 +02:00
#include <cstdint>
2020-11-24 15:41:18 +01:00
#include <cstring>
#ifdef BRYNET_PLATFORM_LINUX
#include <endian.h>
#elif defined BRYNET_PLATFORM_DARWIN
#include <sys/_endian.h>
#endif
2021-05-14 17:16:45 +02:00
inline uint64_t hl64ton(uint64_t hostValue) {
uint64_t ret = 0;
uint32_t high, low;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
low = hostValue & 0xFFFFFFFF;
high = (hostValue >> 32) & 0xFFFFFFFF;
low = htonl(low);
high = htonl(high);
ret = low;
ret <<= 32;
ret |= high;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
return ret;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint64_t ntohl64(uint64_t netValue) {
uint64_t ret = 0;
uint32_t high, low;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
low = netValue & 0xFFFFFFFF;
high = (netValue >> 32) & 0xFFFFFFFF;
low = ntohl(low);
high = ntohl(high);
ret = low;
ret <<= 32;
ret |= high;
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
return ret;
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#ifdef BRYNET_PLATFORM_WINDOWS
2021-05-14 17:16:45 +02:00
inline uint64_t hostToNetwork64(uint64_t host64, bool convert = true) {
return convert ? hl64ton(host64) : host64;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint32_t hostToNetwork32(uint32_t host32, bool convert = true) {
return convert ? htonl(host32) : host32;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint16_t hostToNetwork16(uint16_t host16, bool convert = true) {
return convert ? htons(host16) : host16;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint64_t networkToHost64(uint64_t net64, bool convert = true) {
return convert ? ntohl64(net64) : net64;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint32_t networkToHost32(uint32_t net32, bool convert = true) {
return convert ? ntohl(net32) : net32;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint16_t networkToHost16(uint16_t net16, bool convert = true) {
return convert ? ntohs(net16) : net16;
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_LINUX
2021-05-14 17:16:45 +02:00
inline uint64_t hostToNetwork64(uint64_t host64, bool convert = true) {
return convert ? htobe64(host64) : host64;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint32_t hostToNetwork32(uint32_t host32, bool convert = true) {
return convert ? htobe32(host32) : host32;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint16_t hostToNetwork16(uint16_t host16, bool convert = true) {
return convert ? htobe16(host16) : host16;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint64_t networkToHost64(uint64_t net64, bool convert = true) {
return convert ? be64toh(net64) : net64;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint32_t networkToHost32(uint32_t net32, bool convert = true) {
return convert ? be32toh(net32) : net32;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint16_t networkToHost16(uint16_t net16, bool convert = true) {
return convert ? be16toh(net16) : net16;
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#elif defined BRYNET_PLATFORM_DARWIN
2021-05-14 17:16:45 +02:00
inline uint64_t hostToNetwork64(uint64_t host64, bool convert = true) {
return convert ? hl64ton(host64) : host64;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint32_t hostToNetwork32(uint32_t host32, bool convert = true) {
return convert ? htonl(host32) : host32;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint16_t hostToNetwork16(uint16_t host16, bool convert = true) {
return convert ? htons(host16) : host16;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint64_t networkToHost64(uint64_t net64, bool convert = true) {
return convert ? ntohl64(net64) : net64;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint32_t networkToHost32(uint32_t net32, bool convert = true) {
return convert ? ntohl(net32) : net32;
2021-04-30 16:10:14 +02:00
}
2021-05-14 17:16:45 +02:00
inline uint16_t networkToHost16(uint16_t net16, bool convert = true) {
return convert ? ntohs(net16) : net16;
2021-04-30 16:10:14 +02:00
}
2020-11-24 15:41:18 +01:00
#endif