消息编解码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中取出字段,而不用完全的把整 ...
随机推荐
- zend Studio10.6.2 下载
http://www.th7.cn/Program/php/201409/286788.shtml
- javascript不可用的问题探究
昨天在Twitter上的一些有趣的讨论中, 我发现人们对于Web应用和站点对javascript的依赖普遍存在一种疑惑. 这种疑惑一直都存在, 而对我而言, 这个问题随着浏览技术的飞跃发展而集中爆发了 ...
- 条件随机场(Conditional random field,CRF)
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 【mysql-python】安装+基本使用
安装:从SourceForge.net上下载最新的MySQLdb,http://sourceforge.net/projects/mysql-python/ 运行exe文件 使用 From:http: ...
- ASP.NET MVC 随想录
http://www.cnblogs.com/OceanEyes/category/696137.html
- mac os x 记录 转载
转载:远景网友(手机锋友t5sd3sf):http://bbs.feng.com/read-htm-tid-10434256.html 一个命令制作 OS X 原版安装U盘 1.要保证下载的原版安装包 ...
- keyword static
1. 不能通过类名来调用类的非静态成员函数 2. 类的对象可以使用静态成员函数和非静态成员函数 3. 静态成员函数中不能引用非静态成员 因为静态成员函数属于整个类, 在类的实例化对象之前就已经分配了空 ...
- iOS开发之--改变系统导航的颜色,字体,还有返回样式的自定义
在写项目的工程中,我们可能会遇到各种各样的项目,写的方法也是各有不同,不喜欢自定义的小伙伴也很多, 下面我就记录下系统导航和barbuttonitem的修改系统空间的方法: 1,添加rightbarb ...
- tinker
Ios前一段时间因为热更新被强制下架也算是最大闻了,但Android没关系,继续玩 首先tinker比Andfix好多了,版本现在都到1.7.11了,Andfix不支持yunos, 现在项目中没有用到 ...