Protobuf协议初探(1)

Protobuf讲解

Protobuf下载

Protobuf源码下载网址:源码地址
本人使用了 3.10 版本的 Protobuf,源码地址:ProtobufV3.10
Protobuf截图
Probuf使用Cmake构建工程,需要下载CMAKE
cmake下载地址:cmake地址(作者系统是win10-64位,所以下载 在线安装工具)
cmake下载地址

Protobuf工程建立

1.将下载好的 protobuf-3.10.0.zip解压
2.安装cmake
3.打开cmake-gui.exe
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配置
4.1 – 选择项目工程属性
项目工程
4.2 – 项目平台属性
项目平台属性
4.3 再次点击configure按钮进行配置生成
错误提示
配置过程中,会有如下错误提示: Error in configuration process,project files may be invalid.
错误原因:

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。

If instead you want to skip tests, run cmake with:

configure配置图片

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 配置
4.5 继续点击 configure 按钮,配置configure
5.点击 Generate 按钮,生成项目工程

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

protobuf 项目编译

打开项目工程
项目工程
解决方案目录结构如下
目录结构

ALL_BUILD 编译所有工程
INSTALL 将编译输出文件 配置到 输出目录
libprotobuf libprotobuf.lib 生成方案
libprotobuf-lite libprotobuf-lite.lib 生成方案
libprotoc libprotoc.lib 生成方案
protoc protoc.exe生成方案
ZERO_CHECK zero_check 生成方案
1.先生成 ALL_BUILD 项目
2.生成 INSTALL 项目

最后生成如下
输出目录结构

protobuf 使用

1.proto文件介绍

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++文件
命令如下:

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.相关函数介绍

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序列化函数

  // 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;

反序列化函数

 // 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个项目方案能够设置的全部设置

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值