Skip to content

v1_1_c_overview

Takatoshi Kondo edited this page Oct 14, 2015 · 18 revisions

msgpack-c C version users guide

This document is under construction.

How to use

Include msgpack.h header file.

#include <msgpack.h>

Link msgpack library. When you use UNIX, the library name is libmsgpack.a. When you use Windows, the library name is msgpack.lib for static link and msgpack_import.lib for dynamic link.

Packing

When you pack your data, use buffers and a packer.

msgpack_sbuffer sbuf; /* buffer */
msgpack_packer pk;    /* packer */

msgpack_sbuffer_init(&sbuf); /* initialize buffer */
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); /* initialize packer */

Buffers

The buffers store packed byte data. The buffers are used by packer. msgpack-c provides four buffers. sbuffer, fbuffer, zbuffer, and vrefbuffer.

sbuffer is a simple memory buffer. It uses malloc, realloc, and free. fbuffer is a file buffer. It uses FILE structure in C standard library. zbuffer is a compressing buffer using zlib. So packed data is compressed. vrefbuffer is an iovec based buffer. So packed data is stored as iovec.

Initialize, Destroy, and Write

All buffers have at least three functions. Initialize, Destroy, and Write.

Buffer Initialize Destroy Write
sbuffer msgpack_sbuffer_init msgpack_sbuffer_destroy msgpack_sbuffer_write
zbuffer msgpack_zbuffer_init msgpack_zbuffer_destroy msgpack_zbuffer_write
vrefbuffer msgpack_vrefbuffer_init msgpack_vrefbuffer_destroy msgpack_vrefbuffer_write
fbuffer fopen fclose msgpack_fbuffer_write

When you initialize packer, you pass a pointer to the buffer as the second argument, and pass write function pointer as the third argument. Packer calls the write function through the pointer internally.

The buffers have additional buffer specific functions. Please check the source code.

Packer