moves network related utility functions into network header

This commit is contained in:
Peter Thorson
2013-01-20 08:57:51 -06:00
parent c76771b323
commit 7b2ed75e46
3 changed files with 49 additions and 10 deletions

View File

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

View File

@@ -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 <netinet/in.h>
// For ntohs and htons
#if defined(WIN32)
#include <winsock2.h>
#else
//#include <arpa/inet.h>
#include <netinet/in.h>
#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

View File

@@ -70,14 +70,12 @@ typename T::const_iterator ci_find_substr(const T& str1,
str2, str2+size, my_equal<typename T::value_type>(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);