打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Protobuf:一种更小、更快、更高效的协议

前言

本篇文章我们来分享一种更轻量的数据格式——protobuf。

protobuf的优点:

  • 更小、更快、更简单。
  • 支持多种编程语言 。
  • 解析速度快。
  • 可扩展性强。

什么是protobuf、protobuf-c?

Protocol Buffers,是Google公司开发的一种数据格式,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。它不依赖于语言和平台并且可扩展性极强。

protobuf仓库:

github:https://github.com/protocolbuffers/protobuf

下载速度比较慢,可以先导入到码云,再下载。

protobuf支持多种编程语言:

可以看到,protobuf支持一些主流的语言,唯独没有支持C。所以诞生了第三方的protobuf-c。

protobuf-c仓库:

https://github.com/protobuf-c/protobuf-c

安装protobuf、protobuf-c

我们要使用基于C语言的protobuf,首先需要安装protobuf与protobuf-c。

下面是在Ubuntu下安装的方法:

1、安装protobuf

安装protobuf需要依赖一些工具,需要先安装依赖:

sudo apt-get install autoconf automake libtool curl make g++ unzip

安装完依赖后一依次输入如下命令下载、编译、安装(下载速度慢的话可以先导入码云再下载)protobuf:

git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
./autogen.sh
./configure
make
sudo make install
sudo ldconfig

其中,执行./autogen.sh命令为了生成configure配置脚本,执行configure可生成Makefile文件,执行make进行编译,执行sudo make install命令进行安装,执行sudo ldconfig命令让动态链接库为系统所共享。

2、安装protobuf-c

同样的,protobuf-c也要依赖于 pkg-config ,输入以下命令进行安装:

sudo apt-get install pkg-config

然后输入如下命令下载、编译、安装protobuf-c:

git clone https://github.com/protobuf-c/protobuf-c.git
cd protobuf-c
./autogen.sh
./configure
make
sudo make install

按以上方式安装的话,protobuf与protobuf-c默认安装在/usr/local路径下:

温馨提示:安装过程可能会出现各种各样的错误,遇到错误的时候仔细看错误描述及看本篇文章安装步骤,看是否遗漏了哪一步。

实践demo

protobuf的核心是一个.proto文件,我们自定义一个.proto来创建我们的协议数据,然后使用protoc-c工具编译生成C代码,有两个文件:一个头文件、一个源文件。

例如我们创建一个student.proto文件:

syntax = "proto2";
 
message Student
{
    required string name    = 1;
    required uint32 num     = 2;
    required uint32 c_score = 3;
}

其中syntax为语法版本,有proto2、proto3两个版本,我们使用proto2。同C语言类似,.proto也规定了一些数据格式,如proto2的数据类型有:double 、 float、 int32 、 uint32 、 string 等。

本例中,message为关键字,修饰的Student会对应生成我们C中的Student结构体。其中required为前缀修饰,表明该字段是必填字段。还有其它两个修饰关键字:

  • optional:声明该字段是可选字段。

  • repeated:声明该字段是可重复字段,通常用数组表示,也可以是list。

使用protoc-c工具工具编译student.proto文件的命令:

protoc --c_out=. student.proto

此时编译会生成student.pb-c.c、student.pb-c.h两个文件。我们看看student.pb-c.h里面有什么:

可以看到,student.pb-c.h里生成了一个协议数据结构体与操作该结构体的一些接口,包括组包与解包接口,对应的student.pb-c.c里就是这些接口对应的实现。

编写我们的student.c测试demo:

左右滑动查看全部代码>>>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.pb-c.h"

int main(void)
{
    Student pack_stu = {0};
    uint8_t buffer[512] = {0};
    Student *unpack_stu = NULL;
    size_t len = 0;

    student__init(&pack_stu);

    /* 组包 */
    pack_stu.name = "ZhengN";
    pack_stu.num = 88;
    pack_stu.c_score = 90;
    len = student__pack(&pack_stu, buffer);
    printf("len = %ld\n",len);

    /* 解包 */
    unpack_stu = student__unpack(NULL, len, buffer);
    printf("unpack_stu.name = %s\n", unpack_stu->name);
    printf("unpack_stu.num = %d\n", unpack_stu->num);
    printf("unpack_stu.c_score = %d\n", unpack_stu->c_score);

    student__free_unpacked(unpack_stu, NULL);
    return 0;
}

demo很简单,组包就是构造一个协议数据结构体,调用pack组包接口往buffer中扔数据;解包正好是反过来,从buffer中拿数据放到结构体里。

编译命令:

gcc student.c student.pb-c.c -o student -lprotobuf-c
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
protobuf 数据序列化
通信协议之Protocol buffer(Java篇)
在Windows上一键编译各种版本的Protobuf
google Protocol buffer(JAVA接口) 学习
Google Protocol Buffer 的使用和原理
protobuf简单介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服