消息编解码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中取出字段,而不用完全的把整 ...
随机推荐
- Memcached内存分配及使用问题
在启动memcached的时候可以加-f参数和-n参数.-f指定各slab里面chunk大小的变化比例,默认1.25,-n指定slab里面chunk大小从多少开始.使用memcache_add($me ...
- 设计模式 ( 十二 ) 职责链模式(Chain of Responsibility)(对象行为)
设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型) 1.概述 你去政府部门求人办事过吗?有时候你会遇到过官员踢球推责,你的问题在我这里能解决就解决.不能解决就 ...
- springMVC介绍
http://www.iteye.com/blogs/subjects/springMVC —————————————————————————————————————————————————————— ...
- ubuntu 按键替换 Control_R to Left
ubuntu 按键替换 Control_R to Left 1 查看当前键盘布局 $xmodmap -pke keycode 105 = Control_R NoSymbol Control_Rkey ...
- Swift AVFoundation 二维码扫描和生成
项目最终不须要支持iOS6了(泪崩),在二维码扫描这一块,可以全然的放弃ZXing库,改用系统的AVFoundation了,拿swift写了个Demo,效果例如以下: github地址:点这里 有关A ...
- gitolite 服务器搭建
网上大牛都说的是安装gitolite要建git用户,我看完全没必要,毕竟用户拿到私钥也未必能登录服务器吧...下面我们就用root用户来安装gitolite,并且配置版本库文件保存路径.. 1. gi ...
- thinkphp中ajaxReturn的用法
1.例子: if ($codeid = $model->addCustomer($this->admin["id"])) { $data["code" ...
- LoadRunner小技巧集锦
preftest 性能测试工作室,专注于性能测试技术研究(www.AutomationQA.com) LoadRunner小技巧集锦 1.录制脚本中包含中文,出现乱码怎么办? 把录制选项中的Suppo ...
- Linux系统下修改MySQL密码(忘记密码)
忘记原来的myql的root的密码: 首先,你必须要有操作系统的root权限了.要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤.类似于安全模式登录系统,有人建议说是pkill m ...
- python3----requests
import requests def get_html_text(url): try: r = requests.get(url, timeout=30) r.raise_for_status() ...