Improved network encoding conversion functions
Now uses optimised/inbuilt functions if available.
This commit is contained in:
parent
4207468ef0
commit
c0d103da14
@ -21,10 +21,14 @@
|
||||
#include <ws2tcpip.h>
|
||||
#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 <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) __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();
|
||||
|
||||
@ -14,7 +14,7 @@ constexpr bool is_little_endian()
|
||||
|
||||
TEST(NetworkEncodingTest, test_htonf)
|
||||
{
|
||||
float input = std::numeric_limits<float>::max();
|
||||
float input = std::numeric_limits<float>::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<float>::max();
|
||||
float input = std::numeric_limits<float>::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<double>::max();
|
||||
double input = std::numeric_limits<double>::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<uint64_t>::max();
|
||||
uint64_t input = std::numeric_limits<uint64_t>::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<uint64_t>::max();
|
||||
uint64_t input = std::numeric_limits<uint64_t>::max() - 50;
|
||||
uint64_t encoded = htonll(input);
|
||||
uint64_t decoded = ntohll(encoded);
|
||||
ASSERT_EQ(input, decoded);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user