X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FPacket.cc;fp=src%2FMoof%2FPacket.cc;h=62da00b17f98141d307573232c2d23ca07444fa8;hp=4f3b7fd64f00def5b04ce2f9d7f74287f63e2b2c;hb=1a9061caa8fe73b4b34a37fe467e145bba7bd2f5;hpb=41f8dd670e963aad94527ce2be0486268993a477 diff --git a/src/Moof/Packet.cc b/src/Moof/Packet.cc index 4f3b7fd..62da00b 100644 --- a/src/Moof/Packet.cc +++ b/src/Moof/Packet.cc @@ -16,37 +16,79 @@ #include #endif +#if HAVE_ARPA_INET_H #include +#endif #include #include "Packet.hh" +#ifndef bswap_16 +#define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) +#endif + +#ifndef bswap_32 +#define bswap_32(x) ((((x) & 0xff000000) >> 24) | \ + (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | \ + (((x) & 0x000000ff) << 24)) +#endif + #ifndef bswap_64 -#define bswap_64(x) (((uint64_t)(x) << 56) | \ - (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \ - (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \ - (((uint64_t)(x) << 8) & 0xff00000000ULL) | \ - (((uint64_t)(x) >> 8) & 0xff000000ULL) | \ - (((uint64_t)(x) >> 24) & 0xff0000ULL) | \ - (((uint64_t)(x) >> 40) & 0xff00ULL) | \ - ((uint64_t)(x) >> 56)) +#define bswap_64(x) (((x) << 56) | \ + (((x) << 40) & 0xff000000000000ULL) | \ + (((x) << 24) & 0xff0000000000ULL) | \ + (((x) << 8) & 0xff00000000ULL) | \ + (((x) >> 8) & 0xff000000ULL) | \ + (((x) >> 24) & 0xff0000ULL) | \ + (((x) >> 40) & 0xff00ULL) | \ + ((x) >> 56)) #endif -static uint64_t htonll(uint64_t x) + +#if !HAVE_ARPA_INET_H +static uint16_t htons(uint16_t x) { #if SDL_BYTEORDER == SDL_LIL_ENDIAN - return bswap_64(x); + return bswap_16(x); +#else + return x; #endif } +static uint16_t ntohs(uint16_t x) +{ + return htons(x); +} -static uint64_t ntohll(uint64_t x) +static uint32_t htonl(uint32_t x) +{ +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + return bswap_32(x); +#else + return x; +#endif +} +static uint32_t ntohl(uint32_t x) +{ + return htonl(x); +} +#endif + + +static uint64_t htonll(uint64_t x) { #if SDL_BYTEORDER == SDL_LIL_ENDIAN return bswap_64(x); +#else + return x; #endif } +static uint64_t ntohll(uint64_t x) +{ + return htonll(x); +} namespace Mf { @@ -144,18 +186,23 @@ Packet& Packet::operator<<(uint64_t value) return *this; } -//Packet& Packet::operator<<(float value) -//{ -//} - -//Packet& Packet::operator<<(double value) -//{ -//} - -//Packet& Packet::operator<<(long double value) -//{ -//} +Packet& Packet::operator<<(float value) +{ + // XXX: assumes the ieee-754 + uint32_t* integer = reinterpret_cast(&value); + *integer = htonl(*integer); + write(integer, sizeof(value)); + return *this; +} +Packet& Packet::operator<<(double value) +{ + // XXX: assumes the ieee-754 + uint64_t* integer = reinterpret_cast(&value); + *integer = htonll(*integer); + write(integer, sizeof(value)); + return *this; +} size_t Packet::write(const void* bytes, size_t size) { @@ -230,6 +277,23 @@ Packet& Packet::operator>>(uint64_t& value) return *this; } +Packet& Packet::operator>>(float& value) +{ + // XXX: assumes the ieee-754 + uint32_t* integer = reinterpret_cast(&value); + read(integer, sizeof(value)); + *integer = htonl(*integer); + return *this; +} + +Packet& Packet::operator>>(double& value) +{ + // XXX: assumes the ieee-754 + uint64_t* integer = reinterpret_cast(&value); + read(integer, sizeof(value)); + *integer = htonll(*integer); + return *this; +} size_t Packet::read(void* bytes, size_t size) {