]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Packet.hh
better string encoding in packets
[chaz/yoink] / src / Moof / Packet.hh
index 208b22e9bc75fa51af739546708e7023206b6bdc..00956126ea1185fb64b5e8eb7f2e5cd17edb38f0 100644 (file)
 #include <string>
 #include <vector>
 
-#include <boost/shared_array.hpp>
+
+#ifndef PAGE_SIZE
+#define PAGE_SIZE 4096
+#endif
 
 
 namespace Mf {
@@ -26,9 +29,11 @@ class Packet
 {
 public:
 
-       Packet(size_t size = 1024);
+       Packet(size_t size = PAGE_SIZE);
        Packet(const char* data, size_t size);
 
+       ~Packet();
+
        Packet& operator<<(bool value);
        Packet& operator<<(int8_t  value);
        Packet& operator<<(int16_t value);
@@ -60,7 +65,7 @@ public:
 
        const char* bytes() const
        {
-               return &mBuffer.get()[mR];
+               return mBuffer + mR;
        }
 
        size_t size() const
@@ -71,16 +76,16 @@ public:
 
 private:
 
-       boost::shared_array<char>       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;
 };
 
 
@@ -92,17 +97,31 @@ inline Packet& operator<<(Packet& packet, const char* value)
        return packet;
 }
 
-inline Packet& operator<<(Packet& packet, const std::string& value)
+template <class T>
+inline Packet& operator<<(Packet& packet, const std::basic_string<T>& value)
 {
        packet << (uint16_t)value.length();
-       packet.write(value.c_str(), value.length());
+       packet.write(value.data(), value.length() * sizeof(T));
        return packet;
 }
 
+template <class T>
+inline Packet& operator>>(Packet& packet, std::basic_string<T>& value)
+{
+       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 <class T>
 inline Packet& operator<<(Packet& packet, const std::vector<T>& value)
 {
-       packet << (uint8_t)value.size();
+       packet << (uint16_t)value.size();
        typename std::vector<T>::const_iterator it;
        for (it = value.begin(); it != value.end(); ++it)
        {
@@ -111,26 +130,14 @@ inline Packet& operator<<(Packet& packet, const std::vector<T>& 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 <class T>
 inline Packet& operator>>(Packet& packet, std::vector<T>& 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;
@@ -140,7 +147,6 @@ inline Packet& operator>>(Packet& packet, std::vector<T>& value)
 }
 
 
-
 } // namespace Mf
 
 #endif // _MOOF_PACKET_HH_
This page took 0.022812 seconds and 4 git commands to generate.