日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MessagePack 学习笔记

發布時間:2024/10/12 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MessagePack 学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

封裝和解析類似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;// Serverstd::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();// Clientauto 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.

轉載于:https://www.cnblogs.com/zhangdongsheng/p/7560928.html

總結

以上是生活随笔為你收集整理的MessagePack 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲图片一区 | 亚洲视频久久 | 精品在线视频一区二区 | 狠狠干夜夜骑 | 日本一区不卡视频 | 国产一级片免费视频 | 奇米影视在线播放 | 性色av网址 | 色诱av手机版 | 欧美色乱 | 成人黄色激情视频 | 五月天在线观看 | 图片区偷拍区小说区 | 欧美日韩女优 | 成人欧美一区 | 女人一级一片30分 | 精品国产一区二区三区四区阿崩 | 日韩成人av一区二区 | 日韩在线精品 | 国产91免费在线观看 | 日韩一区不卡 | 亚洲网站在线观看 | 国产在线一区视频 | 亚洲欧美色图片 | 亚洲啪av永久无码精品放毛片 | 伊人在线视频 | 蜜桃99视频一区二区三区 | 久久久久人妻一区精品色 | 国产精品视频一二三区 | 无码人妻精品一区二区三 | 久久亚洲av午夜福利精品一区 | 亚洲九九九九 | 快播色图 | 成人性生交大片免费看r链接 | 九九热视频免费观看 | 日本视频h | 高清日韩一区二区 | 97一级片 | 国产在线视频一区二区 | 亚洲视频1| 日本国产在线播放 | 色婷婷激情 | 亚洲男女在线 | 久久亚洲精品中文字幕 | 日本在线精品视频 | 饥渴的少妇和男按摩师 | www.麻豆av | 一区二区三区视频观看 | www.69视频| 精品黑人一区二区三区在线观看 | av资源天堂 | 精品九九九九九 | 久热只有精品 | 欧美日韩成人一区二区三区 | 国产精品成人网 | 97香蕉碰碰人妻国产欧美 | 香蕉久久夜色精品 | 精品黄色在线观看 | 特一级黄色 | 国产福利在线导航 | 亚洲激情视频在线播放 | 国产一二三在线观看 | 精品自拍一区 | 久久精品国产亚洲AV成人雅虎 | 美女操操操| 综合激情av | 热99在线| 免费黄色网页 | aaa一级黄色片 | 欧美精品日韩精品 | 在线观看精品一区 | 国产情侣一区二区三区 | 成年人在线观看视频免费 | 久久久久久久久久影视 | 日日操日日射 | 欧美大片在线看免费观看 | 国产视频xxxx | 亚洲天堂精品在线观看 | 69超碰| 另类小说婷婷 | 日韩中文字幕久久 | 麻豆国产一区二区三区 | 人人爽人人射 | 亚洲人午夜射精精品日韩 | 久久99精品久久久久子伦 | 免费观看一区二区三区 | 国产精品麻豆视频 | 嫩草影院永久入口 | 一二级毛片 | 涩涩视屏| 尤物视频免费在线观看 | 中文字幕欧美另类精品亚洲 | 日本女教师电影 | 欧美日韩一 | 国产在线成人 | 麻豆系列在线观看 | 人妻熟女一区二区aⅴ水 | 精品人妻一区二区三区日产乱码卜 | 午夜视频h |