protocol buffers c语言的使用
来源:互联网 发布:算法导论第二版答案 编辑:程序博客网 时间:2024/06/05 12:04
以下源码示例来源于https://github.com/protobuf-c/protobuf-c/wiki/Examples
定义消息
message AMessage { required int32 a=1; optional int32 b=2;}
安装protobuf-c
我们需要把proto文件编译成c源码,才能在代码中使用,所以需要用到官方的编译工具
sudo apt-get install protobuf-c-compiler
编译
protoc-c --c_out=. amessage.proto //将会产生amessage.pb-c.c amessage.pb-c.h两个文件
代码中使用
序列化:
//serialize_main.c#include <stdio.h>#include <stdlib.h>#include "amessage.pb-c.h"int main (int argc, const char * argv[]) { AMessage msg = AMESSAGE__INIT; // AMessage void *buf; // Buffer to store serialized data unsigned len; // Length of serialized data if (argc != 2 && argc != 3) { // Allow one or two integers fprintf(stderr,"usage: amessage a [b]\n"); return 1; } msg.a = atoi(argv[1]); if (argc == 3) { msg.has_b = 1; msg.b = atoi(argv[2]); } len = amessage__get_packed_size(&msg); buf = malloc(len); amessage__pack(&msg,buf); fprintf(stderr,"Writing %d serialized bytes\n",len); // See the length of message fwrite(buf,len,1,stdout); // Write to stdout to allow direct command line piping free(buf); // Free the allocated serialized buffer return 0;}
反序列化:
//unserialize_main.c#include <stdio.h>#include <stdlib.h>#include "amessage.pb-c.h"#define MAX_MSG_SIZE 1024static size_tread_buffer (unsigned max_length, uint8_t *out){ size_t cur_len = 0; size_t nread; while ((nread=fread(out + cur_len, 1, max_length - cur_len, stdin)) != 0) { cur_len += nread; if (cur_len == max_length) { fprintf(stderr, "max message length exceeded\n"); exit(1); } } return cur_len;}int main (int argc, const char * argv[]) { AMessage *msg; // Read packed message from standard-input. uint8_t buf[MAX_MSG_SIZE]; size_t msg_len = read_buffer (MAX_MSG_SIZE, buf); // Unpack the message using protobuf-c. msg = amessage__unpack(NULL, msg_len, buf); if (msg == NULL) { fprintf(stderr, "error unpacking incoming message\n"); exit(1); } // display the message's fields. printf("Received: a=%d",msg->a); // required field if (msg->has_b) // handle optional field printf(" b=%d",msg->b); printf("\n"); // Free the unpacked message amessage__free_unpacked(msg, NULL); return 0;}
编译可执行程序
gcc -o serialize_main serialize_main.c amessage.pb-c.c -lprotobuf-cgcc -o unserialize_main unserialize_main.c amessage.pb-c.c -lprotobuf-c
运行
./serialize_main 10086 -123 | ./unserialize_main
阅读全文
0 0
- protocol buffers c语言的使用
- 使用Protocol Buffers的C语言拓展提速Python程序的示例
- Protocol Buffers 的使用 (一)
- protocol buffers的使用示例
- protocol buffers的使用示例
- Wire protocol buffers的使用
- Protocol Buffers在Objective-C上的使用
- Protocol Buffers的安装使用和C++/Python入门示例
- Protocol Buffers for C
- Protocol Buffers在mac下的使用
- Protocol Buffers的基础说明和使用
- android studio Protocol Buffers 的使用
- Protocol Buffers使用入门
- Java 使用 Protocol Buffers
- Protocol Buffers 使用
- Protocol Buffers使用教程
- Protocol Buffers使用教程
- Protocol Buffers for Object-C
- js中对象原型
- ZCMU 1540 第k大数 (二分)
- 程序员如何打造属于自己的云笔记服务
- web.xml文件报错
- Eclipse的.properties文件输出中文成unicode编码
- protocol buffers c语言的使用
- Android HttpURLconnection使用POST请求参数是json
- MySQL主从(一):基本安装部署
- windows xftp 无法向Ubuntu传输文件
- springboot 笔记(简单整理)
- Android Studio使用心得与技巧
- shell里的getopts
- ThreadPoolExecutor线程池参数设置
- 即时通讯:用户注册保存自定义昵称带表情后台SQL异常