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=4f3b7fd64f00def5b04ce2f9d7f74287f63e2b2c;hp=0000000000000000000000000000000000000000;hb=41f8dd670e963aad94527ce2be0486268993a477;hpb=40755d4c6251206c18ce4784967d3a910cee096f diff --git a/src/Moof/Packet.cc b/src/Moof/Packet.cc new file mode 100644 index 0000000..4f3b7fd --- /dev/null +++ b/src/Moof/Packet.cc @@ -0,0 +1,244 @@ + +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include "../config.h" + +#include +#if HAVE_BYTESWAP_H +#include +#endif + +#include + +#include + +#include "Packet.hh" + + +#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)) +#endif + +static uint64_t htonll(uint64_t x) +{ +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + return bswap_64(x); +#endif +} + +static uint64_t ntohll(uint64_t x) +{ +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + return bswap_64(x); +#endif +} + + +namespace Mf { + + +Packet::Packet(size_t size) : + mR(0), + mW(0), + mBoolR(0), + mBoolW(0), + mBoolNumR(0), + mBoolNumW(0) +{ + mBuffer = boost::shared_array(new char[size]); + mSize = size; +} + +Packet::Packet(const char* data, size_t size) : + mR(0), + mBoolR(0), + mBoolW(0), + mBoolNumR(0), + mBoolNumW(0) +{ + mBuffer = boost::shared_array(new char[size]); + mSize = size; + mW = size; + memcpy(mBuffer.get(), data, mSize); +} + + +Packet& Packet::operator<<(bool value) +{ + int bit = mBoolNumW % 8; + if (bit == 0) + { + mBoolW = mW; + + unsigned char byte = 0; + if (write(&byte, 1) == 0) return *this; + } + + if (value) mBuffer[mBoolW] |= (1 << bit); + ++mBoolNumW; + + return *this; +} + + +Packet& Packet::operator<<(int8_t value) +{ + return *this << (uint8_t)value; +} + +Packet& Packet::operator<<(int16_t value) +{ + return *this << (uint16_t)value; +} + +Packet& Packet::operator<<(int32_t value) +{ + return *this << (uint32_t)value; +} + +Packet& Packet::operator<<(int64_t value) +{ + return *this << (uint64_t)value; +} + + +Packet& Packet::operator<<(uint8_t value) +{ + write(&value, sizeof(value)); + return *this; +} + +Packet& Packet::operator<<(uint16_t value) +{ + value = htons(value); + write(&value, sizeof(value)); + return *this; +} + +Packet& Packet::operator<<(uint32_t value) +{ + value = htonl(value); + write(&value, sizeof(value)); + return *this; +} + +Packet& Packet::operator<<(uint64_t value) +{ + value = htonll(value); + write(&value, sizeof(value)); + return *this; +} + +//Packet& Packet::operator<<(float value) +//{ +//} + +//Packet& Packet::operator<<(double value) +//{ +//} + +//Packet& Packet::operator<<(long double value) +//{ +//} + + +size_t Packet::write(const void* bytes, size_t size) +{ + size_t nBytes = std::min(size, mSize - mW); + memcpy(&mBuffer[mW], bytes, nBytes); + mW += nBytes; + return nBytes; +} + + +Packet& Packet::operator>>(bool& value) +{ + int bit = mBoolNumR % 8; + if (bit == 0) + { + mBoolR = mR; + + unsigned char byte = 0; + if (read(&byte, 1) == 0) return *this; + } + + value = 1 & (mBuffer[mBoolR] >> bit); + ++mBoolNumR; + + return *this; +} + +Packet& Packet::operator>>(int8_t& value) +{ + return *this >> (uint8_t&)value; +} + +Packet& Packet::operator>>(int16_t& value) +{ + return *this >> (uint16_t&)value; +} + +Packet& Packet::operator>>(int32_t& value) +{ + return *this >> (uint32_t&)value; +} + +Packet& Packet::operator>>(int64_t& value) +{ + return *this >> (uint64_t&)value; +} + +Packet& Packet::operator>>(uint8_t& value) +{ + read(&value, sizeof(value)); + return *this; +} + +Packet& Packet::operator>>(uint16_t& value) +{ + read(&value, sizeof(value)); + value = ntohs(value); + return *this; +} + +Packet& Packet::operator>>(uint32_t& value) +{ + read(&value, sizeof(value)); + value = ntohl(value); + return *this; +} + +Packet& Packet::operator>>(uint64_t& value) +{ + read(&value, sizeof(value)); + value = ntohll(value); + return *this; +} + + +size_t Packet::read(void* bytes, size_t size) +{ + size_t nBytes = std::min(size, mW - mR); + memcpy(bytes, &mBuffer[mR], nBytes); + mR += nBytes; + return nBytes; +} + + +} // namespace Mf +