diff --git a/test/utility/frame.cpp b/test/utility/frame.cpp index 662e3c03b3..b570230681 100644 --- a/test/utility/frame.cpp +++ b/test/utility/frame.cpp @@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE( prepare_masking_key ) { if (sizeof(size_t) == 8) { BOOST_CHECK( - frame::prepare_masking_key(key) == utility::htonll(0x1234567812345678) + frame::prepare_masking_key(key) == lib::net::htonll(0x1234567812345678) ); } else { BOOST_CHECK( frame::prepare_masking_key(key) == htonl(0x12345678) ); @@ -255,7 +255,7 @@ BOOST_AUTO_TEST_CASE( prepare_masking_key2 ) { // One call if (sizeof(size_t) == 8) { BOOST_CHECK( - frame::prepare_masking_key(key) == utility::htonll(0xD5FB70EED5FB70EE) + frame::prepare_masking_key(key) == lib::net::htonll(0xD5FB70EED5FB70EE) ); } else { BOOST_CHECK( frame::prepare_masking_key(key) == htonl(0xD5FB70EE) ); diff --git a/websocketpp/common/network.hpp b/websocketpp/common/network.hpp index cf0cf32f9b..dbe455151e 100644 --- a/websocketpp/common/network.hpp +++ b/websocketpp/common/network.hpp @@ -28,8 +28,49 @@ #ifndef WEBSOCKETPP_COMMON_NETWORK_HPP #define WEBSOCKETPP_COMMON_NETWORK_HPP -// Required on linux for htonl, htons, etc -// this may need some additional changes for Windows support -#include +// For ntohs and htons +#if defined(WIN32) + #include +#else + //#include + #include +#endif + +namespace websocketpp { +namespace lib { +namespace net { + +#define TYP_INIT 0 +#define TYP_SMLE 1 +#define TYP_BIGE 2 + +inline uint64_t htonll(uint64_t src) { + static int typ = TYP_INIT; + unsigned char c; + union { + uint64_t ull; + unsigned char c[8]; + } x; + if (typ == TYP_INIT) { + x.ull = 0x01; + typ = (x.c[7] == 0x01ULL) ? TYP_BIGE : TYP_SMLE; + } + if (typ == TYP_BIGE) + return src; + x.ull = src; + c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c; + c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c; + c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c; + c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c; + return x.ull; +} + +inline uint64_t ntohll(uint64_t src) { + return htonll(src); +} + +} // net +} // lib +} // websocketpp #endif // WEBSOCKETPP_COMMON_NETWORK_HPP diff --git a/websocketpp/utilities.hpp b/websocketpp/utilities.hpp index 54e442712b..efee348f4d 100644 --- a/websocketpp/utilities.hpp +++ b/websocketpp/utilities.hpp @@ -70,14 +70,12 @@ typename T::const_iterator ci_find_substr(const T& str1, str2, str2+size, my_equal(loc) ); } -#define TYP_INIT 0 -#define TYP_SMLE 1 -#define TYP_BIGE 2 + /// Host to network long long -uint64_t htonll(uint64_t src); +//uint64_t htonll(uint64_t src); /// Network to host long long -uint64_t ntohll(uint64_t src); +//uint64_t ntohll(uint64_t src); /// Convert std::string to ascii printed string of hex digits std::string to_hex(const std::string& input);