|
| 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 | +``` |
0 commit comments