MessagePack C API [英] MessagePack C API

查看:233
本文介绍了MessagePack C API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在看C API为MessagePack,还有一些功能适当序列化(包)中的数据按照类型: msgpack_pack_uint8 msgpack_pack_int32 ...

In looking at the C API for MessagePack, there are a number of functions to appropriately serialize (pack) the data according to type: msgpack_pack_uint8, msgpack_pack_int32, ...

似乎没有成为API中的等价的调用来解压数据。 msgpack_unpack_next 返回 msgpack_object 。这些对象仅具有类型粗粒度(最大的类型:int64类型,双,...),基于所述枚举包括在内。

There doesn't seem to be the equivalent call in the API to unpack the data. msgpack_unpack_next returns a msgpack_object. These objects only have coarse granularity of types (the largest of the type: int64, double, ...), based on the enums included.

我失去了一些东西在这里?是粗对象使用再投的期望?

Am I missing something here? Is the expectation that the coarse object be used and then cast?

应该如何拆包正确呢?

How should unpacking be done properly?

此外,有没有什么好的文档或使用的例子吗?网站上的那些都是小事。

Furthermore, is there any good documentation or usage examples? The ones on the website are trivial.

推荐答案

目前解包的时候,任何整数值总是存储在一个 msgpack_object 作为一个固定宽度64位整数(的int64_t 如果为负, uint64_t中其他)。

At unpack time, any integer value is always stored within a msgpack_object as a fixed-width 64-bit integer (int64_t if negative, uint64_t otherwise).

请参阅 CPP / src目录/ msgpack / object.h 的更多详细信息 msgpack_object 等,和 CPP / src目录/ msgpack / unpack.c 来看看msgpack如何处理拆包逻辑,例如:

See cpp/src/msgpack/object.h for more details on msgpack_object et al., and cpp/src/msgpack/unpack.c to see how msgpack handles the unpacking logic, e.g.:

static inline int template_callback_int8(unpack_user* u,
                                         int8_t d,
                                         msgpack_object* o) {
    if(d >= 0) {
        o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; o->via.u64 = d;
        return 0;
    }
    else {
        o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER; o->via.i64 = d;
        return 0;
    }
}

这是因为在包装的时候,msgpack根据其价值选择动态最优化的方式来连接code整数,例如如果你使用 msgpack_pack_uint16 来收拾你的整数,则:

This is because at pack time, msgpack chooses dynamically the most optimal way to encode an integer according to its value, e.g. if you use msgpack_pack_uint16 to pack your integer then:


  • ,将被保存在1个字节,如果值在[0,127],

  • 2个字节 的0xCC 作为第一个字节,如果值在[128,255]

  • 3个字节 0XCD 作为第一个字节,否则。

  • it will be saved in 1 byte if the value is in [0, 127],
  • 2 bytes with 0xcc as first byte if the value is in [128, 255],
  • 3 bytes with 0xcd as first byte otherwise.

请参阅 msgpack_pack_real_uint16 CPP / src目录/ msgpack / pack_template.h 了解更多信息。

See msgpack_pack_real_uint16 from cpp/src/msgpack/pack_template.h for more details.

在解压时的时间换句话说,msgpack使用一个足够大的正或负(测试,如果 obj.type MSGPACK_OBJECT_POSITIVE_INTEGER MSGPACK_OBJECT_NEGATIVE_INTEGER )举行任何整数值。所以这是由你来:

In other words at unpack time, msgpack uses a large enough positive or negative (test if obj.type is MSGPACK_OBJECT_POSITIVE_INTEGER or MSGPACK_OBJECT_NEGATIVE_INTEGER) to hold any integer value. So it's up to you to:


  • 投,如果你总是可以假设值将不会溢出,你投的类型,

  • 或,动态地检查(用掩模)如果该值不为您的接收器类型,
  • 足够大
  • 或总是使用的int64_t uint64_t中

  • cast if you can always assume that values will never overflow your cast type,
  • or, check dynamically (with a mask) if the value is not large enough for your receiver type,
  • or, always use an int64_t or uint64_t.

最后,C测试套件( msgpack / CPP /测试/ msgpackc_test.cpp )可能会有所帮助浏览code样品。

At last, the C test suite (msgpack/cpp/test/msgpackc_test.cpp) might be helpful to browse code samples.

这篇关于MessagePack C API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆