diff options
-rw-r--r-- | common/message.cpp | 20 | ||||
-rw-r--r-- | common/message.h | 5 |
2 files changed, 21 insertions, 4 deletions
diff --git a/common/message.cpp b/common/message.cpp index 78731e2..53209ed 100644 --- a/common/message.cpp +++ b/common/message.cpp @@ -12,15 +12,27 @@ Message::p Message::create() { } std::pair<uint8_t*, std::size_t> Message::serialize() { - return std::pair<uint8_t*, std::size_t>(0, 0); + std::size_t s = payload.size(); + + uint8_t* buf = new uint8_t[4 + s]; + + *(uint32_t*)buf = (uint32_t)s; + + memcpy(buf + 4, payload.c_str(), payload.size()); + + return std::pair<uint8_t*, std::size_t>(buf, 4 + s); } std::size_t Message::deserialize(uint8_t* data, std::size_t bytes) { - std::cout << "Fikk data (" << bytes << "): " << std::string((char*)data, bytes) << std::endl; - if(deserialize_size == 0 && bytes == 0) { - return 8; + return 4; + } + + if(deserialize_size == 0 && bytes == 4) { + return (std::size_t)(*(int32_t*)data); } + payload = std::string((char*)data, bytes); + return 0; } diff --git a/common/message.h b/common/message.h index 57258c1..eee5ac7 100644 --- a/common/message.h +++ b/common/message.h @@ -3,6 +3,8 @@ #include <boost/shared_ptr.hpp> +#include <string> + class Message { private: Message(); @@ -14,6 +16,9 @@ class Message { static p create(); + //! Temporary payload. + std::string payload; + //! Serialize message. std::pair<uint8_t*, std::size_t> serialize(); |