]> Dogcows Code - chaz/yoink/commitdiff
better string encoding in packets
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 May 2010 20:11:45 +0000 (14:11 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 May 2010 20:11:45 +0000 (14:11 -0600)
src/Moof/Packet.hh
src/Moof/Socket.hh

index 096f112ca063289f502dc7b5f60395188a1f8d70..00956126ea1185fb64b5e8eb7f2e5cd17edb38f0 100644 (file)
@@ -97,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)
        {
@@ -116,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;
@@ -145,7 +147,6 @@ inline Packet& operator>>(Packet& packet, std::vector<T>& value)
 }
 
 
-
 } // namespace Mf
 
 #endif // _MOOF_PACKET_HH_
index a0f2a7cae65bf2da03ee8b57af64f6b754e06c19..9690a2e892df42cdc59257ec270083d9f8cb98d3 100644 (file)
@@ -213,6 +213,7 @@ public:
                else
                {
                        Mf::logWarning(gai_strerror(status));
+                       return -1;
                }
 
                return 0;
This page took 0.023578 seconds and 4 git commands to generate.