From: Charles McGarvey Date: Thu, 13 May 2010 20:11:45 +0000 (-0600) Subject: better string encoding in packets X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=26095329ef78e94929ad7ab14480dc1a0edecff8;p=chaz%2Fyoink better string encoding in packets --- diff --git a/src/Moof/Packet.hh b/src/Moof/Packet.hh index 096f112..0095612 100644 --- a/src/Moof/Packet.hh +++ b/src/Moof/Packet.hh @@ -97,17 +97,31 @@ inline Packet& operator<<(Packet& packet, const char* value) 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.c_str(), value.length()); + packet.write(value.data(), value.length() * sizeof(T)); + return packet; +} + +template +inline Packet& operator>>(Packet& packet, std::basic_string& 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 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) { @@ -116,26 +130,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; @@ -145,7 +147,6 @@ inline Packet& operator>>(Packet& packet, std::vector& value) } - } // namespace Mf #endif // _MOOF_PACKET_HH_ diff --git a/src/Moof/Socket.hh b/src/Moof/Socket.hh index a0f2a7c..9690a2e 100644 --- a/src/Moof/Socket.hh +++ b/src/Moof/Socket.hh @@ -213,6 +213,7 @@ public: else { Mf::logWarning(gai_strerror(status)); + return -1; } return 0;