某项目实战分析代码二

此次分析的是protobuf的使用

Protocol Buffer( 简称 Protobuf) 是Google公司内部的混合语言数据标准,它是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或RPC 数据交换格式。

Protobuf是一个纯粹的展示层协议,可以和各种传输层协议一起使用,Protobuf的文档也非常完善。google 提供了多种语言的实现:java、c#、c++、go 和 python,每一种实现都包含了相应语言的编译器以及库文件。

Protobuf支持的数据类型相对较少,不支持常量类型。由于其设计的理念是纯粹的展现层协议,目前并没有一个专门支持Protobuf的RPC框架。

更多资料可查看:https://developers.google.com/protocol-buffers/

操作流程

  • 准备数据
  • 复合类型: 结构体/ 类
  • 基础类型
  • 创建一个新文件 xxx.proto
  • 将我们要序列化的数据 -> 写入到proto文件
    • 有语法格式
  • 通过一个命令 protocxxx.proto文件生成一个c++的类
    • 对应一个头文件/ 源文件
    • 操作命令-> 在window终端中: protoc xxx.proto --cpp_out=./
  • 直接使用这个类
    • 里边有对数据操作的api
      • 读数据 api
        • 方法名字 变量名()
      • 写数据 api
        • 方法名字: set_变量名(arg)

具体实现

// 要序列化的数据
struct Persion
{
    int id;
    string name;
    string sex;	// man woman
    int age;
};

在.proto文件中定义消息格式

// protobuf的版本
syntax = "proto3";	// proto2
// 组织Persion结构体
// 语法格式
message 关键字(相当于被创建出的类的名字)
{
	// 成员变量
	数据类型 变量名 = 变量的编号;	// 编号从1开始, 不能重复
}

// .proto文件 生成 c++ 类的命令
protoc proto文件名 --cpp_out=生成目录
.proto类型**C++**类型备注
doubledouble64位浮点数
floatfloat32位浮点数
int32int3232位整数
int64int6464位整数
uint32uint3232位无符号整数
uint64uint6464位无符号整数
sint32sint3232位整数,处理负数效率比int32更高
sint64sint6464位整数,处理负数效率比int64更高
fixed32uint32总是4个字节。如果数值总是比总是比228大的话,这个类型会比uint32高效。
fixed64uint64总是8个字节。如果数值总是比总是比256大的话,这个类型会比uint64高效。
sfixed32int32总是4个字节
sfixed64int64总是8个字节
boolbool布尔类型
stringstring一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本
bytesstring处理多字节的语言字符、如中文
enumenum枚举
messageobject of class自定义的消息类型
  • repeated限定修饰符

    syntax = "proto3";
    message Persion
    {
        int32 id = 1;   // 编号从1开始
        bytes name = 2;
        string sex = 3;
        int32 age = 4;
    }
    // 要求name有多个 -> 数组
    syntax = "proto3";
    message Persion
    {
        int32 id = 1;   // 编号从1开始
        // vector<string> name;
        repeated bytes name = 2;	// name可以在程序中创建多个, 在程序中作为动态数组来使用
        string sex = 3;
        int32 age = 4;
    }
    
  • 枚举

    syntax = "proto3";
    // 定义枚举
    enum Color
    {
    	Red = 0;	// protbuf中第一个枚举值必须为0
    	Green = 6;
    	Blue = 9;
    }
    message Persion
    {
        int32 id = 1;   // 编号从1开始
        bytes name = 2;
        string sex = 3;
        int32 age = 4;
        Color color = 5;	// 枚举变量
    }
    
  • proto文件的导入

    // Persion.proto
    syntax = "proto3";
    // 导入另外一个proto文件
    import "Info.proto";
    
    enum Color
    {
    	Red = 0;	// protbuf中第一个枚举值必须为0
    	Green = 6;
    	Blue = 9;
    }
    
    message Persion
    {
        int32 id = 1;   // 编号从1开始
        repeated bytes name = 2;
        string sex = 3;
        int32 age = 4;
        Color color = 5;
        Info info = 6;	// Info对象, 导入的proto文件中的类
    }
    
    // Info.proto
    syntax = "proto3";
    
    message Info
    {
        bytes address = 1;  // 地址
        int32 number = 2;   // 门牌号
    }
    
  • 包 -> 命名空间

    // Persion.proto
    syntax = "proto3";
    // 导入另外一个proto文件
    import "Info.proto";
    // 添加命名空间
    package itcast;	// Persion类属于itcast这个命名空间
    
    enum Color
    {
    	Red = 0;	// protbuf中第一个枚举值必须为0
    	Green = 6;
    	Blue = 9;
    }
    
    message Persion
    {
        int32 id = 1;   // 编号从1开始
        repeated bytes name = 2;
        string sex = 3;
        int32 age = 4;
        Color color = 5;
        // 命名空间.类名
        itheima.Info info = 6;	// Info对象, 导入的proto文件中的类
    }
    
    // Info.proto
    syntax = "proto3";
    // Persion类属于itheima这个命名空间
    package itheima;
    
    message Persion
    {
        bytes address = 1;  // 地址
        int32 number = 2;   // 门牌号
    }
    
  • 使用protobuf编译器生成C++类

    # protobuf编译器, 编译源码得到的 protoc.exe
    # 语法
    # --cpp_out 生成的c++类的位置
    protoc.exe xxx.proto --cpp_out=目录
    
  • 使用C++ API来读写消息

3. 业务数据分析

3.1 客户端

// 准备要发送的数据
struct RequestMsg
{
    //1 密钥协商 //2 密钥校验; // 3 密钥注销
    int cmdType; // 报文类型
    string clientId; // 客户端编号
    string serverId; // 服务器端编号
    string sign;
    string data;
};
message RequestMsg
{
	int32 cmdType;
	bytes clientId;
}

3.2 服务器端

struct RespondMsg
{
    bool rv; // 返回值
    int seckeyid; 		// 对称密钥编号 keysn
    string clientId; 	// 客户端编号
    string serverId; 	// 服务器编号
    string data; 	// 服务器端随机数
};

简单案例

1.新建protobuf文件

  syntax = "proto3";
  message Persion
  {
      int32 id = 1;   // 编号从1开始
      bytes name = 2;
      string sex = 3;
      int32 age = 4;
  }

2.按住shift+左键,进入powerShell,执行命令生成文件
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

煎饼果子小鸢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值