MessagePack 学习笔记

简介: 封装和解析类似json的  key-value 示例 {"ID" = 333,"name"="zds","3333"="ende"} msgpack::sbuffer sBuf; msgpack::packer pker(&sBuf); pker.

封装和解析类似json的  key-value 示例

{"ID" = 333,"name"="zds","3333"="ende"}

    msgpack::sbuffer sBuf;
    msgpack::packer<msgpack::sbuffer> pker(&sBuf);

    pker.pack_map(3);
    pker.pack(std::string("ID"));
    pker.pack(333);
    pker.pack(std::string("name"));
    pker.pack(std::string("zds"));
    pker.pack(std::string("333"));
    pker.pack(std::string("ende"));


    //unserilized
    msgpack::unpacked unpack;
    msgpack::unpack(unpack, sBuf.data(), sBuf.size());

    msgpack::object obj = unpack.get();
    std::cout << obj << std::endl;


    if (obj.type == msgpack::type::ARRAY)
        std::cout << "是array" << std::endl;
    else if (obj.type == msgpack::type::MAP)
        std::cout << "是map" << std::endl;




    if(obj.via.map.size > 0)
    {
        auto pkv = obj.via.map.ptr;
        auto pkv_end = obj.via.map.ptr + obj.via.map.size;

        do 
        {
            auto key = pkv->key;
            auto val = pkv->val;
            std::cout << "key:" << key << " value:" << val << std::endl;
            ++pkv;
        } while (pkv < pkv_end);
    }

解析Socket示例

各类数据结构:

msgpack::object 他是一个引用,拷贝他的代价少,因为他是浅拷贝
msgpack::object_handle  他管理了一个对象的生命周期。他如果释放了,所有从他生成的object都是无效的引用

 

解析Socket示例

下列代码解析socke收包数据

unpacker.reserve_buffer 分配要收的数据的内存字节数
unpacker..buffer() 返回数据地址
unpacker.buffer_consumed() 设置实际收到的数据
unpacker.next(object_handle& oh) 循环解析数据

int main() {
    boost::asio::io_service ios;
    std::uint16_t const port = 12345;

    // Server
    std::size_t const window_size = 10;
    boost::asio::ip::tcp::acceptor ac(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
    boost::asio::ip::tcp::socket ss(ios);
    std::function<void()> do_accept;
    std::function<void()> do_async_read_some;

    msgpack::unpacker unp;

    do_accept = [&] {
        ac.async_accept(
            ss,
            [&]
            (boost::system::error_code const& e) {
                if (e) {
                    std::cout << __LINE__ << ":" << e.message() << std::endl;
                    return;
                }
                unp.reserve_buffer(window_size);
                do_async_read_some = [&] {
                    ss.async_read_some(
                        boost::asio::buffer(unp.buffer(), window_size),
                        [&](boost::system::error_code const& e, std::size_t bytes_transferred) {
                            if (e) {
                                std::cout << __LINE__ << ":" << e.message() << std::endl;
                                return;
                            }
                            std::cout << bytes_transferred << " bytes read." << std::endl;
                            unp.buffer_consumed(bytes_transferred);
                            msgpack::object_handle oh;
                            while (unp.next(oh)) {
                                std::cout << oh.get() << std::endl;
                                // In order to finish the program,
                                // return if one complete msgpack is processed.
                                // In actual server, don't return here.
                                return;
                            }
                            do_async_read_some();
                        }
                    );
                };
                do_async_read_some();
            }
        );
    };
    do_accept();

    // Client
    auto host = "localhost";
    boost::asio::ip::tcp::resolver r(ios);
    boost::asio::ip::tcp::resolver::query q(host, boost::lexical_cast<std::string>(port));
    auto it = r.resolve(q);
    boost::asio::ip::tcp::socket cs(ios);
    boost::asio::async_connect(
        cs,

        it,
        [&]
        (boost::system::error_code const& e, boost::asio::ip::tcp::resolver::iterator) {
            if (e) {
                std::cout << __LINE__ << ":" << e.message() << std::endl;
                return;
            }
            std::cout << __LINE__ << ":client connected" << std::endl;
            msgpack::sbuffer sb;
            msgpack::pack(sb, std::make_tuple(42, false, "hello world", 12.3456));
            write(cs, boost::asio::buffer(sb.data(), sb.size()));
        }
    );

    // Start
    ios.run();
}

详解:

msgpack controls a buffer

msgpack provides a buffer management functionality named msgpack::unpacker. msgpack::unpacker is sutable for the following motivations:

  • msgpack data is chopped, and the client doesn't know when it will complete. This is a typical situation when you develop streaming applications.
  • You want to minimize copy opperations without careful memory management.

Here is the basic (not all) interface of msgpack::unpacker:

#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
#endif #ifndef MSGPACK_UNPACKER_RESERVE_SIZE #define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024) #endif class unpacker { public: unpacker(unpack_reference_func f = &unpacker::default_reference_func, void* user_data = nullptr, std::size_t init_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE, unpack_limit const& limit = unpack_limit()); void reserve_buffer(std::size_t size = MSGPACK_UNPACKER_RESERVE_SIZE); char* buffer(); void buffer_consumed(std::size_t size); bool next(unpacked& result); };

Here is a basic pattern using msgpack::unpacker:

// The size may decided by receive performance, transmit layer's protocol and so on.
std::size_t const try_read_size = 100;

msgpack::unpacker unp;

// Message receive loop while (/* block until input becomes readable */) { unp.reserve_buffer(try_read_size); // unp has at least try_read_size buffer on this point. // input is a kind of I/O library object. // read message to msgpack::unpacker's internal buffer directly. std::size_t actual_read_size = input.readsome(unp.buffer(), try_read_size); // tell msgpack::unpacker actual consumed size. unp.buffer_consumed(actual_read_size); msgpack::unpacked result; // Message pack data loop while(unp.next(result)) { msgpack::object obj(result.get()); // Use obj } // All complete msgpack message is proccessed at this point, // then continue to read addtional message. }

msgpack::unpacker::next() returns true if one complete msgpack messege is proccessed. If msgpack message is correct but insufficient, it returns false. However, parsing proccess is proceeded and the context information is preserved in the msgpack::unpacker. It helps leveling the load of parse.

When msgpack message contains binary data, string data, or ext data, they are not copied but referenced from msgpack::object by default. See the following implementation:

inline bool unpacker::default_reference_func(type::object_type type, uint64_t len, void*)
{
    return true; }

You can also customize unpack_reference_func. Even if you use references, you don't need to control buffer's lifetime. The buffers' lifetime is controled by msgpack using msgpack::zone's finalizer_array and msgpack::unpacker's reference counting mechanism.

So, in most cases, the default behavior is enough. If you want to control the peak of memory consumption when receiving msgpack data patterns are predictable, customizing unpack_reference_func might be useful.

You can get a reference information from msgpack::unpacker::next() using the following function:

    bool next(unpacked& result, bool& referenced);

However, mostly you don't need to use that version of next() because referenced memories are managed by unpacker.

相关文章
|
缓存 网络协议 数据安全/隐私保护
[运维笔记] - (命令).Windows server常用网络相关命令总结
[运维笔记] - (命令).Windows server常用网络相关命令总结
970 0
|
JSON 网络协议 开发工具
对已有的docker容器添加新的目录映射, 端口映射,环境变量,dns等
docker容器已经建立并运行, 需要在已有容器上添加新的目录映射,端口映射,环境变量等
3911 0
|
存储 NoSQL Java
SpringBoot 中使用 MongoDB 基于 MongoRepository增删改查(基础篇)
SpringBoot 中使用 MongoDB 基于 MongoRepository增删改查(基础篇)
1537 0
|
7月前
|
JSON BI API
全网最全面介绍闲鱼API接口指南
闲鱼是阿里巴巴旗下的二手交易平台,提供RESTful API支持商品管理、订单处理、用户操作与数据统计。开发者可通过OAuth 2.0认证,使用App Key和access_token调用接口,实现自动化运营与第三方集成。本指南详解API核心功能、接入步骤、代码示例及最佳实践,助您高效开发。
7044 1
|
10月前
|
人工智能 安全 大数据
阿里云最新学生、开发者和企业优惠券种类、金额、领取和使用区别参考
2025年,阿里云推出为了助力更多用户降低上云门槛,推出了多种类型的优惠券,覆盖学生、初创企业、开发者及有迁移需求的企业用户,推动更多用户享受云服务的便捷与高效。本文将为大家解析目前阿里云最新优惠券的种类、金额、领取方式及使用规则,帮助不同群体精准匹配最优福利,实现资源利用最大化。
|
监控 算法 机器人
软件体系结构 - 调度算法(2) 最低松弛度优先
【4月更文挑战第19天】软件体系结构 - 调度算法(2) 最低松弛度优先
878 0
|
8月前
|
数据采集 机器学习/深度学习 人工智能
2026年具身智能商业化浪潮前瞻:开发者工具与软件生态的万亿级掘金图谱
在具身智能执行器和传感器等硬件成本持续下降的背景下,真正驱动其大规模商业化爆发的,并非单一的硬件产品,而是其背后的软件与生态系统。正如互联网的价值最终体现在软件应用与服务上,具身智能的商业价值正在从传统的“硬件产品销售”转向“软件与服务订阅”。RaaS等创新商业模式的兴起,正是这一趋势的直接体现。这里我们将深入剖析2026年具身智能领域,特别是围绕数据采集、训练和任务开发的软件工具与服务所蕴藏的万亿级商业机遇,为开发者与投资者描绘一幅详尽的掘金图谱。
1094 0
|
存储 人工智能 算法
RAG七十二式:2024年度RAG清单
作者遴选了2024年度典型的RAG系统和论文(含AI注解、来源、摘要信息),并于文末附上RAG综述和测试基准材料,希望阅读完本文可以帮助大家速通RAG。
|
编解码 人工智能 测试技术
CogView4开源发布!智谱AI文生图模型支持任意长度双语输入,汉字生成能力突出,可商用!
今天智谱AI正式发布并开源了最新的图像生成模型——CogView4。
1059 10
CogView4开源发布!智谱AI文生图模型支持任意长度双语输入,汉字生成能力突出,可商用!
|
机器学习/深度学习 存储 人工智能
【AI系统】为什么 GPU 适用于 AI
本文探讨了GPU在AI计算中的应用,从卷积计算的基本原理入手,解析了GPU线程分级与AI计算模式的关系,以及矩阵乘法如何通过GPU编程提升算力利用率。文章还介绍了计算强度的概念,分析了不同数据结构对计算强度的影响,以及GPU中Tensor Core的作用,强调了在不同存储位置下,找到计算强度与矩阵大小的最佳平衡点对AI计算系统优化的重要性。
1221 2