X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FPacket.hh;h=1a5e42fed31bc696cae6913194f535d268e0a99a;hp=198212c352646984e3a3352a6705a73e0de9c600;hb=264bdbb09bc86797f1f80d151ac408cb780b9355;hpb=41f8dd670e963aad94527ce2be0486268993a477 diff --git a/src/Moof/Packet.hh b/src/Moof/Packet.hh index 198212c..1a5e42f 100644 --- a/src/Moof/Packet.hh +++ b/src/Moof/Packet.hh @@ -16,7 +16,10 @@ #include #include -#include + +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif namespace Mf { @@ -26,9 +29,14 @@ class Packet { public: - Packet(size_t size = 1024); + Packet(size_t size = PAGE_SIZE); Packet(const char* data, size_t size); + Packet(const Packet& copy); + Packet& operator=(const Packet& copy); + + ~Packet(); + Packet& operator<<(bool value); Packet& operator<<(int8_t value); Packet& operator<<(int16_t value); @@ -38,9 +46,8 @@ public: Packet& operator<<(uint16_t value); Packet& operator<<(uint32_t value); Packet& operator<<(uint64_t value); - //Packet& operator<<(float value); - //Packet& operator<<(double value); - //Packet& operator<<(long double value); + Packet& operator<<(float value); + Packet& operator<<(double value); size_t write(const void* bytes, size_t size); @@ -53,13 +60,18 @@ public: Packet& operator>>(uint16_t& value); Packet& operator>>(uint32_t& value); Packet& operator>>(uint64_t& value); + Packet& operator>>(float& value); + Packet& operator>>(double& value); size_t read(void* bytes, size_t size); + void clear(); + + const char* bytes() const { - return &mBuffer.get()[mR]; + return mBuffer + mR; } size_t size() const @@ -70,38 +82,52 @@ public: private: - boost::shared_array mBuffer; - size_t mSize; + char* mBuffer; + size_t mSize; - size_t mR; - size_t mW; + size_t mR; + size_t mW; - size_t mBoolR; - size_t mBoolW; - size_t mBoolNumR; - size_t mBoolNumW; + size_t mBoolR; + size_t mBoolW; + size_t mBoolNumR; + size_t mBoolNumW; }; inline Packet& operator<<(Packet& packet, const char* value) { - uint8_t length = strnlen(value, 255); + uint16_t length = strlen(value); packet << length; packet.write(value, length); return packet; } -inline Packet& operator<<(Packet& packet, const std::string& value) +template +inline Packet& operator<<(Packet& packet, const std::basic_string& value) +{ + packet << (uint16_t)value.length(); + packet.write(value.data(), value.length() * sizeof(T)); + return packet; +} + +template +inline Packet& operator>>(Packet& packet, std::basic_string& value) { - packet << (uint8_t)value.length(); - packet.write(value.c_str(), value.length()); + uint16_t length = 0; + packet >> length; + + T str[length]; + size_t charsRead = packet.read(str, length * sizeof(T)); + value.assign(str, charsRead); return packet; } + template inline Packet& operator<<(Packet& packet, const std::vector& value) { - packet << (uint8_t)value.size(); + packet << (uint16_t)value.size(); typename std::vector::const_iterator it; for (it = value.begin(); it != value.end(); ++it) { @@ -110,26 +136,14 @@ inline Packet& operator<<(Packet& packet, const std::vector& value) return packet; } - -inline Packet& operator>>(Packet& packet, std::string& value) -{ - uint8_t length = 0; - packet >> length; - - char str[256]; - size_t charsRead = packet.read(str, length); - value.assign(str, charsRead); - return packet; -} - template inline Packet& operator>>(Packet& packet, std::vector& value) { - uint8_t size = 0; + uint16_t size = 0; packet >> size; value.clear(); - for (uint8_t i = 0; i < size; ++i) + for (uint16_t i = 0; i < size; ++i) { T item; packet >> item; @@ -139,7 +153,6 @@ inline Packet& operator>>(Packet& packet, std::vector& value) } - } // namespace Mf #endif // _MOOF_PACKET_HH_