-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsgpack-example.cpp
More file actions
93 lines (68 loc) · 1.92 KB
/
msgpack-example.cpp
File metadata and controls
93 lines (68 loc) · 1.92 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
This is a sample program that I use to learn msgpack with;
it is not a part of the main library.
*/
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
template <typename T>
struct ClockPayload {
std::string Pid;
T Payload;
std::map<std::string, uint64_t> VcMap;
};
void print_bytes(const void *object, size_t size)
{
// This is for C++; in C just drop the static_cast<>() and assign.
const unsigned char * const bytes = static_cast<const unsigned char *>(object);
size_t i;
printf("[ ");
for(i = 0; i < size; i++)
{
printf("%02x ", bytes[i]);
}
printf("]\n");
}
int main(void)
{
std::map<std::string, int> vc;
vc["client"] = 2;
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(buffer);
std::string c = "client";
int a = 0;
pk.pack(c);
pk.pack(a);
pk.pack(vc);
std::cout << "Size: " << buffer.size() << std::endl;
print_bytes(buffer.data(), buffer.size());
std::size_t len = buffer.size();
// copy the buffer to a char[] to send on the wire
char cstr[len];
const char* bufferdata = buffer.data();
memcpy(cstr, bufferdata, len);
print_bytes(bufferdata, len);
print_bytes(cstr, len);
// SEND, GET
std::string newPid;
int newA;
std::map<std::string, int> newVc;
std::size_t off = 0;
msgpack::object_handle result;
msgpack::unpack(result, cstr, len, off);
msgpack::object obj1(result.get());
obj1.convert(newPid);
std::cout << newPid << std::endl;
msgpack::unpack(result, cstr, len, off);
msgpack::object obj2(result.get());
obj2.convert(newA);
std::cout << newA << std::endl;
msgpack::unpack(result, cstr, len, off);
msgpack::object obj3(result.get());
obj3.convert(newVc);
std::cout << newVc["client"] << std::endl;
std::cout << newVc["server"] << std::endl;
return 0;
}