前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Protobuf协议初探(1)

Protobuf协议初探(1)

作者头像
何其不顾四月天
发布2023-03-10 13:25:26
6140
发布2023-03-10 13:25:26
举报
文章被收录于专栏:四月天的专栏

Protobuf讲解

Protobuf下载

Protobuf源码下载网址:源码地址 本人使用了 3.10 版本的 Protobuf,源码地址:ProtobufV3.10

Protobuf截图
Protobuf截图

Probuf使用Cmake构建工程,需要下载CMAKE cmake下载地址:cmake地址(作者系统是win10-64位,所以下载 在线安装工具)

cmake下载地址
cmake下载地址

Protobuf工程建立

1.将下载好的 protobuf-3.10.0.zip解压 2.安装cmake 3.打开cmake-gui.exe

cmake
cmake

where is the source code: cmake 文件夹路径 cmake文件夹路径:…\protobuf-3.10.0\protobuf-3.10.0\cmake Where to build the binaries : 工程生成路径 作者在 protobuf-3.10.0\cmake 下新建 build文件夹

4.configure配置 点击上图中的configure按钮

configure配置
configure配置

4.1 – 选择项目工程属性

项目工程
项目工程

4.2 – 项目平台属性

项目平台属性
项目平台属性

4.3 再次点击configure按钮进行配置生成

错误提示
错误提示

配置过程中,会有如下错误提示: Error in configuration process,project files may be invalid. 错误原因:

代码语言:javascript
复制
CMake Error at tests.cmake:2 (message):
  Cannot find third_party/googletest directory that's needed to build tests.
  If you use git, make sure you have cloned submodules:
    git submodule update --init --recursive
  If instead you want to skip tests, run cmake with:
    cmake -Dprotobuf_BUILD_TESTS=OFF
Call Stack (most recent call first):
  CMakeLists.txt:243 (include)

根据这句话来判断,skip tests。

代码语言:javascript
复制
If instead you want to skip tests, run cmake with:
configure配置图片
configure配置图片
代码语言:javascript
复制
CMAKE_CONFIGURATION_TYPES:配置生成类型:Debug/Release 根据个人需要配置
CMAKE_INSTALL_PREFIX:项目输出路径(自定义配置,作者在 build同级目录建立了 out 文件夹)
protobuf_BUILD_TESTS 根据错误提示,去掉 tests 配置
protobuf_WITH_ZLIB 根据个人需要,作者去掉了
protobuf_BUILD_SHARED_LIBS 配置编译动态库
protobuf_MSVC_STATIC_RUNTIME 配置使用MSVC静态运行时库
protobuf_BUILD_PROTOC_BINATIES 配置 build libprotoc and protoc complier
protobuf_BUILD_EXAMPLES 配置编译样例
protobuf_BUILD_CONFORMANCE 配置 编译 conformance rtests 

作者配置如下:

configure 配置
configure 配置

4.5 继续点击 configure 按钮,配置configure 5.点击 Generate 按钮,生成项目工程

生成图片
生成图片

输出如下,说明configure,generate 生成成功。 6.build工程目录如下:

生成目录如下
生成目录如下

protobuf 项目编译

打开项目工程

项目工程
项目工程

解决方案目录结构如下

目录结构
目录结构
代码语言:javascript
复制
ALL_BUILD 编译所有工程
INSTALL 将编译输出文件 配置到 输出目录
libprotobuf libprotobuf.lib 生成方案
libprotobuf-lite libprotobuf-lite.lib 生成方案
libprotoc libprotoc.lib 生成方案
protoc protoc.exe生成方案
ZERO_CHECK zero_check 生成方案
代码语言:javascript
复制
1.先生成 ALL_BUILD 项目
2.生成 INSTALL 项目

最后生成如下

输出目录结构
输出目录结构

protobuf 使用

1.proto文件介绍

代码语言:javascript
复制
syntax = "proto3";
package proto;

enum Type {
  UNKONWN = 0;
  WAKEUP  = 1; // weakup; service <-- guest
  MD5     = 2; // get file md5; service --> guest
  SAVE    = 3; // send file to agent; service --> guest
  CMD     = 4; // agent run command; service --> guest
}

message Packet {
  Type   type     = 1; // type
  uint64 code     = 2; // code
}

systax = “proto3” //语法 package proto; //包名 enum Type{ //枚举类型 } message pac{ //消息体 Type type =1; uint64 code =2; }

2.生成protobufC++ 文件 在out 目录中 bin文件夹下 protoc.exe 文件生成 protobuf c++文件 命令如下:

代码语言:javascript
复制
protoc.exe msg.proto --proto_path=E:\1-Projects\proto  --cpp_out=E:\1-Projects\proto
proto.exe filename --proto_path=filepath --cpp_out=outpath
--proto_path proto文件所在路径
--cpp_out c++ 文件输出路径

生成文件如下

生成文件如下
生成文件如下

3.项目配置 VS工程项目建立,配置这一块就不说了。 在out 文件夹下。 include 头文件目录,lib 链接库目录,配置到工程属性当中。 msg.pb.cc msg.pb.h 添加到工程文件当中 4.相关函数介绍

代码语言:javascript
复制
proto::Packet _packet; //proto 域名 Packet 包名
_packet.set_type(type);//设置类型
_packet.set_code();//设置code
std::string out;
_packet.SerializeToString(&out); //转为 二进制 string字符串
//可以将 out 作为普通string类型进行使用,网络传输,类之间参数传递,信号传输

proto::Packet _file;
_file.ParseFromString(out);
proto::Type  type = _file.type(); //获取序列化参数 type
uint64 code = _file.code(); //获取code值

将protobuf序列化函数

代码语言:javascript
复制
  // Write a protocol buffer of this message to the given output.  Returns
  // false on a write error.  If the message is missing required fields,
  // this may GOOGLE_CHECK-fail.
  bool SerializeToCodedStream(io::CodedOutputStream* output) const;
  // Like SerializeToCodedStream(), but allows missing required fields.
  bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;
  // Write the message to the given zero-copy output stream.  All required
  // fields must be set.
  bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
  // Like SerializeToZeroCopyStream(), but allows missing required fields.
  bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
  // Serialize the message and store it in the given string.  All required
  // fields must be set.
  bool SerializeToString(std::string* output) const;
  // Like SerializeToString(), but allows missing required fields.
  bool SerializePartialToString(std::string* output) const;
  // Serialize the message and store it in the given byte array.  All required
  // fields must be set.
  bool SerializeToArray(void* data, int size) const;
  // Like SerializeToArray(), but allows missing required fields.
  bool SerializePartialToArray(void* data, int size) const;

反序列化函数

代码语言:javascript
复制
 // These work exactly like the similarly-named methods of Message.

  bool MergeFromCodedStream(io::CodedInputStream* input);
  bool ParseFromCodedStream(io::CodedInputStream* input);
  bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
  bool ParseFromArray(const void* data, int size);
  inline bool ParseFromString(const std::string& data) {
    return ParseFromArray(data.data(), static_cast<int>(data.size()));
  }

5.注意事项 在编译 protobuf 库文件的时候,注意 项目工程属性

protobuf工程平台属性与项目平台属性一致性(X64,X86,Win32)等 protobuf工程运行库属性 与 项目工程运行库属性一致(MD/MT/MD_D/MT_D) 运行库设置 属性->C/C+±>代码生成->运行库 protobuf运行库设置,记得7个项目方案能够设置的全部设置

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Protobuf讲解
  • Protobuf下载
  • Protobuf工程建立
  • protobuf 项目编译
  • protobuf 使用
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档