Moving network encoding functions into the fr namespace

To prevent collision with msvc.
This commit is contained in:
Unknown 2018-04-11 19:36:18 +01:00
parent 0f14184bf6
commit 79d2037bc4
4 changed files with 40 additions and 36 deletions

View File

@ -41,34 +41,35 @@
#include <netinet/tcp.h>
#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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -3,6 +3,8 @@
//
#include <gtest/gtest.h>
#include <limits>
#include <algorithm>
#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<float>::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<float>::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<double>::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<double>::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<uint64_t>::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<uint64_t>::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);
}