Skip to content

Commit 79151f5

Browse files
committed
Imported quickstarts from wiki
1 parent e8ffe7e commit 79151f5

File tree

2 files changed

+353
-0
lines changed

2 files changed

+353
-0
lines changed

QUICKSTART-C.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# Implementation Status
2+
3+
The serialization library is production-ready.
4+
5+
Currently, no RPC implementation is not available.
6+
7+
# Install
8+
9+
10+
## Mac OS X with MacPorts
11+
12+
On Mac OS X, you can install MessagePack for C using MacPorts.
13+
14+
```
15+
$ sudo port install msgpack
16+
```
17+
18+
You might need to run `sudo port selfupdate` before installing to update the package repository.
19+
20+
You can also install via Homebrew.
21+
22+
```
23+
$ sudo brew install msgpack
24+
```
25+
26+
## FreeBSD with Ports Collection
27+
28+
On FreeBSD, you can use Ports Collection. Install [net/msgpack|http://www.freebsd.org/cgi/cvsweb.cgi/ports/devel/msgpack/] package.
29+
30+
## Gentoo Linux with Portage
31+
32+
On Gentoo Linux, you can use emerge. Install [dev-libs/msgpack|http://gentoo-portage.com/dev-libs/msgpack] package.
33+
34+
## Other UNIX-like platform with ./configure
35+
36+
On the other UNIX-like platforms, download source package from [Releases|http://msgpack.org/releases/cpp/] and run `./configure && make && make install`.
37+
38+
```
39+
$ wget http://msgpack.org/releases/cpp/msgpack-0.5.5.tar.gz
40+
$ tar zxvf msgpack-0.5.5.tar.gz
41+
$ cd msgpack-0.5.5
42+
$ ./configure
43+
$ make
44+
$ sudo make install
45+
```
46+
47+
## Windows
48+
49+
On Windows, download source package from [here|https://sourceforge.net/projects/msgpack/files/] and extract it.
50+
Then open `msgpack_vc8.vcproj` file and build it using batch build. It builds libraries on `lib/` folder and header files on `include/` folder.
51+
52+
You can build using command line as follows:
53+
54+
```
55+
> vcbuild msgpack_vc2008.vcproj
56+
> dir lib % DLL files are here
57+
> dir include % header files are here
58+
```
59+
60+
## Install from git repository
61+
62+
You need to install gcc (4.1.0 or higher), autotools.
63+
64+
```
65+
$ git clone [email protected]:msgpack/msgpack.git
66+
$ cd msgpack/cpp
67+
$ ./bootstrap
68+
$ ./configure
69+
$ make
70+
$ sudo make install
71+
```
72+
73+
# Serialization QuickStart for C
74+
75+
## First program
76+
77+
Include `msgpack.h` header and link `msgpack` library to use MessagePack on your program.
78+
79+
```c
80+
#include <msgpack.h>
81+
#include <stdio.h>
82+
83+
int main(void) {
84+
85+
/* creates buffer and serializer instance. */
86+
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
87+
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
88+
89+
/* serializes ["Hello", "MessagePack"]. */
90+
msgpack_pack_array(pk, 2);
91+
msgpack_pack_raw(pk, 5);
92+
msgpack_pack_raw_body(pk, "Hello", 5);
93+
msgpack_pack_raw(pk, 11);
94+
msgpack_pack_raw_body(pk, "MessagePack", 11);
95+
96+
/* deserializes it. */
97+
msgpack_unpacked msg;
98+
msgpack_unpacked_init(&msg);
99+
bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);
100+
101+
/* prints the deserialized object. */
102+
msgpack_object obj = msg.data;
103+
msgpack_object_print(stdout, obj); /*=> ["Hello", "MessagePack"] */
104+
105+
/* cleaning */
106+
msgpack_sbuffer_free(buffer);
107+
msgpack_packer_free(pk);
108+
}
109+
```
110+
111+
## Simple program with a loop
112+
113+
```c
114+
#include <msgpack.h>
115+
#include <stdio.h>
116+
117+
int main(void) {
118+
119+
/* creates buffer and serializer instance. */
120+
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
121+
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
122+
123+
int j;
124+
125+
for(j = 0; j<23; j++) {
126+
/* NB: the buffer needs to be cleared on each iteration */
127+
msgpack_sbuffer_clear(buffer);
128+
129+
/* serializes ["Hello", "MessagePack"]. */
130+
msgpack_pack_array(pk, 3);
131+
msgpack_pack_raw(pk, 5);
132+
msgpack_pack_raw_body(pk, "Hello", 5);
133+
msgpack_pack_raw(pk, 11);
134+
msgpack_pack_raw_body(pk, "MessagePack", 11);
135+
msgpack_pack_int(pk, j);
136+
137+
/* deserializes it. */
138+
msgpack_unpacked msg;
139+
msgpack_unpacked_init(&msg);
140+
bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);
141+
142+
/* prints the deserialized object. */
143+
msgpack_object obj = msg.data;
144+
msgpack_object_print(stdout, obj); /*=> ["Hello", "MessagePack"] */
145+
puts("");
146+
}
147+
148+
/* cleaning */
149+
msgpack_sbuffer_free(buffer);
150+
msgpack_packer_free(pk);
151+
}
152+
```
153+
154+
## Streaming feature
155+
156+
```c
157+
#include <msgpack.h>
158+
#include <stdio.h>
159+
160+
int main(void) {
161+
/* serializes multiple objects using msgpack_packer. */
162+
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
163+
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
164+
msgpack_pack_int(pk, 1);
165+
msgpack_pack_int(pk, 2);
166+
msgpack_pack_int(pk, 3);
167+
168+
/* deserializes these objects using msgpack_unpacker. */
169+
msgpack_unpacker pac;
170+
msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
171+
172+
/* feeds the buffer. */
173+
msgpack_unpacker_reserve_buffer(&pac, buffer->size);
174+
memcpy(msgpack_unpacker_buffer(&pac), buffer->data, buffer->size);
175+
msgpack_unpacker_buffer_consumed(&pac, buffer->size);
176+
177+
/* now starts streaming deserialization. */
178+
msgpack_unpacked result;
179+
msgpack_unpacked_init(&result);
180+
181+
while(msgpack_unpacker_next(&pac, &result)) {
182+
msgpack_object_print(stdout, result.data);
183+
puts("");
184+
}
185+
186+
/* results:
187+
* $ gcc stream.cc -lmsgpack -o stream
188+
* $ ./stream
189+
* 1
190+
* 2
191+
* 3
192+
*/
193+
}
194+
```

QUICKSTART-CPP.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Implementation Status
2+
3+
The serialization library is production-ready.
4+
5+
Currently, RPC implementation is testing phase. Requires newer kernel, not running on RHEL5/CentOS5.
6+
7+
# Install
8+
9+
Same as QuickStart for C Language.
10+
11+
# Serialization QuickStart for C+\+
12+
13+
## First program
14+
15+
Include `msgpack.hpp` header and link `msgpack` library to use MessagePack on your program.
16+
17+
```cpp
18+
#include <msgpack.hpp>
19+
#include <vector>
20+
#include <string>
21+
#include <iostream>
22+
23+
int main(void) {
24+
// serializes this object.
25+
std::vector<std::string> vec;
26+
vec.push_back("Hello");
27+
vec.push_back("MessagePack");
28+
29+
// serialize it into simple buffer.
30+
msgpack::sbuffer sbuf;
31+
msgpack::pack(sbuf, vec);
32+
33+
// deserialize it.
34+
msgpack::unpacked msg;
35+
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
36+
37+
// print the deserialized object.
38+
msgpack::object obj = msg.get();
39+
std::cout << obj << std::endl; //=> ["Hello", "MessagePack"]
40+
41+
// convert it into statically typed object.
42+
std::vector<std::string> rvec;
43+
obj.convert(&rvec);
44+
}
45+
```
46+
47+
Compile it as follows:
48+
49+
```
50+
$ g++ hello.cc -lmsgpack -o hello
51+
$ ./hello
52+
["Hello", "MessagePack"]
53+
```
54+
55+
## Streaming feature
56+
57+
```cpp
58+
#include <msgpack.hpp>
59+
#include <iostream>
60+
#include <string>
61+
62+
int main(void) {
63+
// serializes multiple objects using msgpack::packer.
64+
msgpack::sbuffer buffer;
65+
66+
msgpack::packer<msgpack::sbuffer> pk(&buffer);
67+
pk.pack(std::string("Log message ... 1"));
68+
pk.pack(std::string("Log message ... 2"));
69+
pk.pack(std::string("Log message ... 3"));
70+
71+
// deserializes these objects using msgpack::unpacker.
72+
msgpack::unpacker pac;
73+
74+
// feeds the buffer.
75+
pac.reserve_buffer(buffer.size());
76+
memcpy(pac.buffer(), buffer.data(), buffer.size());
77+
pac.buffer_consumed(buffer.size());
78+
79+
// now starts streaming deserialization.
80+
msgpack::unpacked result;
81+
while(pac.next(&result)) {
82+
std::cout << result.get() << std::endl;
83+
}
84+
85+
// results:
86+
// $ g++ stream.cc -lmsgpack -o stream
87+
// $ ./stream
88+
// "Log message ... 1"
89+
// "Log message ... 2"
90+
// "Log message ... 3"
91+
}
92+
```
93+
94+
### Streaming into an array or map
95+
96+
```cpp
97+
#include <msgpack.hpp>
98+
#include <iostream>
99+
#include <string>
100+
101+
int main(void) {
102+
// serializes multiple objects into one message containing an array using msgpack::packer.
103+
msgpack::sbuffer buffer;
104+
105+
msgpack::packer<msgpack::sbuffer> pk(&buffer);
106+
pk.pack_array(3)
107+
pk.pack(std::string("Log message ... 1"));
108+
pk.pack(std::string("Log message ... 2"));
109+
pk.pack(std::string("Log message ... 3"));
110+
111+
// serializes multiple objects into one message containing a map using msgpack::packer.
112+
msgpack::sbuffer buffer2;
113+
114+
msgpack::packer<msgpack::sbuffer> pk2(&buffer2);
115+
pk2.pack_map(2)
116+
pk2.pack(std::string("x"));
117+
pk2.pack(3);
118+
pk2.pack(std::string("y));
119+
pk2.pack(3.4321);
120+
121+
}
122+
```
123+
124+
125+
## User-defined classes
126+
127+
You can use serialize/deserializes user-defined classes using MSGPACK_DEFINE macro.
128+
129+
```cpp
130+
#include <msgpack.hpp>
131+
#include <vector>
132+
#include <string>
133+
134+
class myclass {
135+
private:
136+
std::string m_str;
137+
std::vector<int> m_vec;
138+
public:
139+
MSGPACK_DEFINE(m_str, m_vec);
140+
};
141+
142+
int main(void) {
143+
std::vector<myclass> vec;
144+
// add some elements into vec...
145+
146+
// you can serialize myclass directly
147+
msgpack::sbuffer sbuf;
148+
msgpack::pack(sbuf, vec);
149+
150+
msgpack::unpacked msg;
151+
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
152+
153+
msgpack::object obj = msg.get();
154+
155+
// you can convert object to myclass directly
156+
std::vector<myclass> rvec;
157+
obj.convert(&rvec);
158+
}
159+
```

0 commit comments

Comments
 (0)