diff --git a/include/frnetlib/NetworkEncoding.h b/include/frnetlib/NetworkEncoding.h index 9f57cc6..59f43c8 100644 --- a/include/frnetlib/NetworkEncoding.h +++ b/include/frnetlib/NetworkEncoding.h @@ -41,34 +41,35 @@ #include #endif -#undef htonll -#undef ntohll -#undef htonf -#undef ntohf -#undef htond -#undef ntohd #if defined(__GNUC__) # if __BYTE_ORDER == __LITTLE_ENDIAN -# define htonll(x) __builtin_bswap64 (x) -# define ntohll(x) __builtin_bswap64 (x) +# define fr_htonll(x) __builtin_bswap64 (x) +# define fr_ntohll(x) __builtin_bswap64 (x) # else -# define htonll(x) (x) -# define ntohll(x) (x) +# define fr_htonll(x) (x) +# define fr_ntohll(x) (x) # endif #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)) +# define fr_htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) +# define fr_ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) #endif -inline float htonf(float val) +#undef htonf +#undef htond +#undef ntohd +#undef ntonf + +namespace fr { - uint32_t ret; - memcpy(&ret, &val, sizeof(ret)); - ret = htonl(ret); - memcpy(&val, &ret, sizeof(val)); - return val; -} + inline float htonf(float val) + { + uint32_t ret; + memcpy(&ret, &val, sizeof(ret)); + ret = htonl(ret); + memcpy(&val, &ret, sizeof(val)); + return val; + } inline float ntohf(float val) { @@ -83,7 +84,7 @@ inline double htond(double val) { uint64_t ret; memcpy(&ret, &val, sizeof(ret)); - ret = htonll(ret); + ret = fr_htonll(ret); memcpy(&val, &ret, sizeof(val)); return val; } @@ -92,7 +93,7 @@ inline double ntohd(double val) { uint64_t ret; memcpy(&ret, &val, sizeof(ret)); - ret = ntohll(ret); + ret = fr_ntohll(ret); memcpy(&val, &ret, sizeof(val)); return val; } @@ -137,6 +138,7 @@ static void init_wsa() signal(SIGPIPE, SIG_IGN); #endif } +} #endif //FRNETLIB_NETWORKENCODING_H diff --git a/include/frnetlib/Packet.h b/include/frnetlib/Packet.h index 693ba42..22eb03a 100644 --- a/include/frnetlib/Packet.h +++ b/include/frnetlib/Packet.h @@ -257,7 +257,7 @@ namespace fr */ inline Packet &operator<<(uint64_t var) { - var = htonll(var); + var = fr_htonll(var); buffer.append((char*)&var, sizeof(var)); return *this; } @@ -271,7 +271,7 @@ namespace fr memcpy(&var, &buffer[buffer_read_index], sizeof(var)); buffer_read_index += sizeof(var); - var = ntohll(var); + var = fr_ntohll(var); return *this; } @@ -326,7 +326,7 @@ namespace fr */ inline Packet &operator<<(int64_t var) { - var = htonll((uint64_t)var); + var = fr_htonll((uint64_t)var); buffer.append((char*)&var, sizeof(var)); return *this; } @@ -340,7 +340,7 @@ namespace fr memcpy(&var, &buffer[buffer_read_index], sizeof(var)); buffer_read_index += sizeof(var); - var = ntohll((uint64_t)var); + var = fr_ntohll((uint64_t)var); return *this; } diff --git a/src/WebFrame.cpp b/src/WebFrame.cpp index e6ac2c2..4862420 100644 --- a/src/WebFrame.cpp +++ b/src/WebFrame.cpp @@ -52,7 +52,7 @@ namespace fr } else //64bit length { - uint64_t len = htonll(payload.size()); + uint64_t len = fr_htonll(payload.size()); buffer.append((char*)&len, sizeof(len)); } } @@ -122,7 +122,7 @@ namespace fr else if(payload_length == 127) //Length is longer than 16 bit, so read 64bit length { status = socket->receive_all(&payload_length, sizeof(payload_length)); - payload_length = ntohll(payload_length); + payload_length = fr_ntohll(payload_length); if(status != fr::Socket::Success) return status; } diff --git a/tests/NetworkEncodingTest.cpp b/tests/NetworkEncodingTest.cpp index 6430b7c..55addaf 100644 --- a/tests/NetworkEncodingTest.cpp +++ b/tests/NetworkEncodingTest.cpp @@ -3,6 +3,8 @@ // #include +#include +#include #include "frnetlib/NetworkEncoding.h" constexpr bool is_little_endian() @@ -15,7 +17,7 @@ constexpr bool is_little_endian() TEST(NetworkEncodingTest, test_htonf) { float input = std::numeric_limits::max() - 50; - float result = htonf(input); + float result = fr::htonf(input); if(is_little_endian()) { @@ -32,15 +34,15 @@ TEST(NetworkEncodingTest, test_htonf) TEST(NetworkEncodingTest, test_ntohf) { float input = std::numeric_limits::max() - 50; - float encoded = htonf(input); - float decoded = ntohf(encoded); + float encoded = fr::htonf(input); + float decoded = fr::ntohf(encoded); ASSERT_EQ(input, decoded); } TEST(NetworkEncodingTest, test_htond) { double input = std::numeric_limits::max() - 50; - double result = htond(input); + double result = fr::htond(input); if(is_little_endian()) { @@ -57,15 +59,15 @@ TEST(NetworkEncodingTest, test_htond) TEST(NetworkEncodingTest, test_ntohd) { double input = std::numeric_limits::max(); - double encoded = htond(input); - double decoded = ntohd(encoded); + double encoded = fr::htond(input); + double decoded = fr::ntohd(encoded); ASSERT_EQ(input, decoded); } TEST(NetworkEncodingTest, test_htonll) { uint64_t input = std::numeric_limits::max() - 50; - uint64_t result = htonll(input); + uint64_t result = fr_htonll(input); if(is_little_endian()) { @@ -82,7 +84,7 @@ TEST(NetworkEncodingTest, test_htonll) TEST(NetworkEncodingTest, test_ntohll) { uint64_t input = std::numeric_limits::max() - 50; - uint64_t encoded = htonll(input); - uint64_t decoded = ntohll(encoded); + uint64_t encoded = fr_htonll(input); + uint64_t decoded = fr_ntohll(encoded); ASSERT_EQ(input, decoded); } \ No newline at end of file