]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Packet.hh
initial network stuff
[chaz/yoink] / src / Moof / Packet.hh
diff --git a/src/Moof/Packet.hh b/src/Moof/Packet.hh
new file mode 100644 (file)
index 0000000..198212c
--- /dev/null
@@ -0,0 +1,146 @@
+
+/*]  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.
+*
+**************************************************************************/
+
+#ifndef _MOOF_PACKET_HH_
+#define _MOOF_PACKET_HH_
+
+#include <cstring>
+#include <string>
+#include <vector>
+
+#include <boost/shared_array.hpp>
+
+
+namespace Mf {
+       
+
+class Packet
+{
+public:
+
+       Packet(size_t size = 1024);
+       Packet(const char* data, size_t size);
+
+       Packet& operator<<(bool value);
+       Packet& operator<<(int8_t  value);
+       Packet& operator<<(int16_t value);
+       Packet& operator<<(int32_t value);
+       Packet& operator<<(int64_t value);
+       Packet& operator<<(uint8_t  value);
+       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);
+
+       size_t write(const void* bytes, size_t size);
+
+       Packet& operator>>(bool& value);
+       Packet& operator>>(int8_t&  value);
+       Packet& operator>>(int16_t& value);
+       Packet& operator>>(int32_t& value);
+       Packet& operator>>(int64_t& value);
+       Packet& operator>>(uint8_t&  value);
+       Packet& operator>>(uint16_t& value);
+       Packet& operator>>(uint32_t& value);
+       Packet& operator>>(uint64_t& value);
+
+       size_t read(void* bytes, size_t size);
+
+
+       const char* bytes() const
+       {
+               return &mBuffer.get()[mR];
+       }
+
+       size_t size() const
+       {
+               return mW - mR;
+       }
+
+
+private:
+
+       boost::shared_array<char>       mBuffer;
+       size_t                                          mSize;
+
+       size_t                                          mR;
+       size_t                                          mW;
+
+       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);
+       packet << length;
+       packet.write(value, length);
+       return packet;
+}
+
+inline Packet& operator<<(Packet& packet, const std::string& value)
+{
+       packet << (uint8_t)value.length();
+       packet.write(value.c_str(), value.length());
+       return packet;
+}
+
+template <class T>
+inline Packet& operator<<(Packet& packet, const std::vector<T>& value)
+{
+       packet << (uint8_t)value.size();
+       typename std::vector<T>::const_iterator it;
+       for (it = value.begin(); it != value.end(); ++it)
+       {
+               packet << *it;
+       }
+       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;
+       packet >> size;
+
+       value.clear();
+       for (uint8_t i = 0; i < size; ++i)
+       {
+               T item;
+               packet >> item;
+               value.push_back(item);
+       }
+       return packet;
+}
+
+
+
+} // namespace Mf
+
+#endif // _MOOF_PACKET_HH_
+
This page took 0.022226 seconds and 4 git commands to generate.