blob: 4ddd543e4731b680fda09b97b3c97827b004e4c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include "message.h"
#include <cstring>
#include <iostream>
Message::Message() : deserialize_size(0) {
}
Message::p Message::create() {
return Message::p(new Message);
}
std::pair<uint8_t*, std::size_t> Message::serialize() {
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) {
if(deserialize_size == 0 && bytes == 0) {
return 4;
}
if(deserialize_size == 0 && bytes == 4) {
return (std::size_t)(*(int32_t*)data);
}
payload = std::string((char*)data, bytes);
return 0;
}
|