From c0d103da14cb2147e84aa36bcc1beff7899a00dd Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Mon, 26 Mar 2018 17:34:00 +0100 Subject: [PATCH] Improved network encoding conversion functions Now uses optimised/inbuilt functions if available. --- include/frnetlib/NetworkEncoding.h | 26 +++++++++++++++++++++----- tests/NetworkEncodingTest.cpp | 10 +++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/include/frnetlib/NetworkEncoding.h b/include/frnetlib/NetworkEncoding.h index 1bceb28..9e55160 100644 --- a/include/frnetlib/NetworkEncoding.h +++ b/include/frnetlib/NetworkEncoding.h @@ -21,10 +21,14 @@ #include #define SOL_TCP SOL_SOCKET #define SHUT_RDWR SD_BOTH -#define UNUSED_VAR #else +#ifdef __GNUC__ #define UNUSED_VAR __attribute__ ((unused)) +#else +# define UNUSED_VAR +#endif + #define closesocket(x) close(x) #define INVALID_SOCKET 0 #define SOCKET_ERROR (-1) @@ -36,15 +40,27 @@ #include #endif - #undef htonll #undef ntohll #undef htonf #undef ntohf #undef htond #undef ntohd -#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) -#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) + +#if defined(__GNUC__) +# if __BYTE_ORDER == __LITTLE_ENDIAN +# define htonll(x) __bswap_64 (x) +# define ntohll(x) __bswap_64 (x) +# else +# define htonll(x) (x) +# define ntohll(x) (x) +# endif +#elif defined(_MSC_VER) +//MSVC has htonll and ntohll, no need to redefine +#else +# define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) +# define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) +#endif inline float htonf(float val) { @@ -105,7 +121,7 @@ inline bool set_unix_socket_blocking(int32_t socket_descriptor, bool is_blocking return true; } -static UNUSED_VAR void init_wsa() +static void init_wsa() { #ifdef _WIN32 static WSADATA wsaData = WSAData(); diff --git a/tests/NetworkEncodingTest.cpp b/tests/NetworkEncodingTest.cpp index 12f95bf..6430b7c 100644 --- a/tests/NetworkEncodingTest.cpp +++ b/tests/NetworkEncodingTest.cpp @@ -14,7 +14,7 @@ constexpr bool is_little_endian() TEST(NetworkEncodingTest, test_htonf) { - float input = std::numeric_limits::max(); + float input = std::numeric_limits::max() - 50; float result = htonf(input); if(is_little_endian()) @@ -31,7 +31,7 @@ TEST(NetworkEncodingTest, test_htonf) TEST(NetworkEncodingTest, test_ntohf) { - float input = std::numeric_limits::max(); + float input = std::numeric_limits::max() - 50; float encoded = htonf(input); float decoded = ntohf(encoded); ASSERT_EQ(input, decoded); @@ -39,7 +39,7 @@ TEST(NetworkEncodingTest, test_ntohf) TEST(NetworkEncodingTest, test_htond) { - double input = std::numeric_limits::max(); + double input = std::numeric_limits::max() - 50; double result = htond(input); if(is_little_endian()) @@ -64,7 +64,7 @@ TEST(NetworkEncodingTest, test_ntohd) TEST(NetworkEncodingTest, test_htonll) { - uint64_t input = std::numeric_limits::max(); + uint64_t input = std::numeric_limits::max() - 50; uint64_t result = htonll(input); if(is_little_endian()) @@ -81,7 +81,7 @@ TEST(NetworkEncodingTest, test_htonll) TEST(NetworkEncodingTest, test_ntohll) { - uint64_t input = std::numeric_limits::max(); + uint64_t input = std::numeric_limits::max() - 50; uint64_t encoded = htonll(input); uint64_t decoded = ntohll(encoded); ASSERT_EQ(input, decoded);