消息编解码Nanopb - protocol buffers
Google Protocol Buffer 有各种版本的代码包,Python C/C++、JAVA、C、OBJ-C、.NET等,嵌入式设备中使用的protobuf版本,我们选择的是nanoprobuf。
1)Google Protocol Buffer 的使用和原理
https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
2)Nanopb - protocol buffers with small code size
To use the nanopb library, you need to do two things:
- Compile your .proto files for nanopb, using protoc.
- Include pb_encode.c, pb_decode.c and pb_common.c in your project.
The easiest way to get started is to study the project in "examples/simple". It contains a Makefile, which should work directly under most Linux systems. However, for any other kind of build system, see the manual steps in README.txt in that folder.
simple.c
message SimpleMessage {
required int32 lucky_number = ;
}
#include <stdio.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include "simple.pb.h" int main()
{
/* This is the buffer where we will store our message. */
uint8_t buffer[];
size_t message_length;
bool status; /* Encode our message */
{
/* Allocate space on the stack to store the message data.
*
* Nanopb generates simple struct definitions for all the messages.
* - check out the contents of simple.pb.h!
* It is a good idea to always initialize your structures
* so that you do not have garbage data from RAM in there.
*/
SimpleMessage message = SimpleMessage_init_zero; /* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Fill in the lucky number */
message.lucky_number = ; /* Now we are ready to encode the message! */
status = pb_encode(&stream, SimpleMessage_fields, &message);
message_length = stream.bytes_written; /* Then just check for any errors.. */
if (!status)
{
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return ;
}
} /* Now we could transmit the message over network, store it in a file or
* wrap it to a pigeon's leg.
*/ /* But because we are lazy, we will just decode it immediately. */ {
/* Allocate space for the decoded message. */
SimpleMessage message = SimpleMessage_init_zero; /* Create a stream that reads from the buffer. */
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); /* Now we are ready to decode the message. */
status = pb_decode(&stream, SimpleMessage_fields, &message); /* Check for errors... */
if (!status)
{
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return ;
} /* Print the data contained in the message. */
printf("Your lucky number was %d!\n", message.lucky_number);
} return ;
}
https://github.com/nanopb/nanopb
https://github.com/protocolbuffers/protobuf
消息编解码Nanopb - protocol buffers的更多相关文章
- 企业级工作流解决方案(七)--微服务Tcp消息传输模型之消息编解码
Tcp消息传输主要参照surging来做的,做了部分裁剪和改动,详细参见:https://github.com/dotnetcore/surging Json-rpc没有定义消息如何传输,因此,Jso ...
- (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-2
8.1.2 Protobuf编解码开发 Protobuf的类库使用比较简单,下面我们就通过对SubscrjbeReqProto进行编解码来介绍Protobuf的使用. 8-1 Protob ...
- Netty游戏服务器之四protobuf编解码和黏包处理
我们还没讲客户端怎么向服务器发送消息,服务器怎么接受消息. 在讲这个之前我们先要了解一点就是tcp底层存在粘包和拆包的机制,所以我们在进行消息传递的时候要考虑这个问题. 看了netty权威这里处理的办 ...
- 【笔记】直接使用protocol buffers的底层库,对特定场景的PB编解码进行处理,编码性能提升2.4倍,解码性能提升4.8倍
接上一篇文章:[笔记]golang中使用protocol buffers的底层库直接解码二进制数据 最近计划优化prometheus的remote write协议,因为业务需要,实现了一个remote ...
- 理解netty对protocol buffers的编码解码
一,netty+protocol buffers简要说明 Netty是业界最流行的NIO框架之一优点:1)API使用简单,开发门槛低:2)功能强大,预置了多种编解码功能,支持多种主流协议:3)定制能力 ...
- Protocol Buffers(2):编码与解码
目录 Message Structure 解码代码一窥 varint Protobuf中的整数和浮点数 Length-delimited相关类型 小结 参考 博客:blog.shinelee.me | ...
- 一个简单RPC框架是怎样炼成的(IV)——实现RPC消息的编解码
之前我们制定了一个非常easy的RPC消息 的格式,可是还遗留了两个问题,上一篇解决掉了一个.还留下一个 我们并没有实现对应的encode和decode方法,没有基于能够跨设备的字符串传输,而是直接的 ...
- Protocol Buffers学习(4):更多消息类型
介绍一下消息的不同类型和引用 使用复杂消息类型 您可以使用其他消息类型作为字段类型.例如,假设你想在每个SearchResponse消息中包含Result消息,您可以在同一个.proto中定义一个Re ...
- 【笔记】golang中使用protocol buffers的底层库直接解码二进制数据
背景 一个简单的代理程序,发现单核QPS达到2万/s左右就上不去了,40%的CPU消耗在pb的decode/encode上面. 于是我想,对于特定的场景,直接从[]byte中取出字段,而不用完全的把整 ...
随机推荐
- 树莓派安装centos 7系统
1,格式化 https://www.sdcard.org/downloads/formatter_4/eula_windows/ 2,烧录,Win32DiskImager https://source ...
- javascript 实现java中的Map
javascript实现java中的Map,代码是在国外的一个网站上看到的(http://stackoverflow.com/questions/368280/javascript-hashmap-e ...
- HttpServletRequest和HttpServletResponse简介
http://blog.csdn.net/tong_xinglong/article/details/12972819
- 让所有IE支持HTML5的解决方案
自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注,然而唯一能让IE识别那些新元素(如<article>)的途径是使用HTML5 shiv,感谢remy sharp为我们提 ...
- GCC命令基础
gcc命令使用GNU推出的基于C/C++的编译器,是开放源代码领域应用最广泛的编译器,具有功能强大,编译代码支持性能优化等特点, 现在很多程序员都应用GCC,怎样才能更好的应用GCC. 目前,GCC可 ...
- JavaScript------表单约束验证DOM方法
<input id="id1" type="number" min="100" max="300" require ...
- ios 的EditBox点击空白处不隐藏的解决方案
原因:参数少了前缀CC 解决方案:修改 cocos/platform/ios/CCEAGLView-ios.mm 中的 handleTouchesAfterKeyboardShow -(void) h ...
- 记一个在docker中运行多线程event_loop.run_forever()的bug
问题简介 我写爬虫,用到了asyncio相关的事件循环,新建了一个线程去run_forever(),在docker中运行.后来程序有异常,主线程挂了,但是竟然不报错.查了很久,才找出来. 如果你新建一 ...
- angular4 在页面跳转的时候传递多个参数到新页面
页面跳转 router.navigate //单一参数: this.router.navigate(['/detail',id]); //多个参数: this.router.navigate(['/d ...
- 利用jsPerf优化Web应用的性能
在前端开发的过程中,掌握好浏览器的特性进行有针对性的性能调优是一项基本工作,jsperf.com是一个用来发布基于HTML的针对性能比较的测试用例的网站,你可以在jsPerf上在线填写和运行测试用例, ...