protubuf在服务器开发的使用

protobuf的编码方式:

Varints是一种紧凑表示数字的方法,它用一个或多个字节表示一个数字,值越小占用的字节数越少。相对传统的4字节表示int32类型,Varints对小于128的数值用1个字节表示,大于128的值用更多的字节来表示,对于很大的数据则用5个字节来表示。

protobuf的数据类型

在这里插入图片描述
type 0:可以进行压缩的数据类型;
type 2:长度非一的数据类型;

解析压缩后的数据的规则

warning:protubuf协议压缩后的一个字节是8位,要这么理解:
(1) 5+3:拆类型
前五个表示field;(见下面例子中的"optional int32 age = 1;",这个”1“就是field,也可以叫做字段)
后三个表示type:(即数据类型,见上表《protobuf的数据类型》)。

(2) 1+7:拆数值
1:第一个字节是表示该段能不能表示完:
case(1): need complete the value with next byte.next byte occupy the high address;
case(0):it is enough to repressent the value with this byte;
7:后面7个就是字节的值。

举例说明:

文件一:UserInfo.proto
// "UserInfo.proto"
package ssp; //protubuf的命名空间(包)

message User{
	optional int32 age = 1;
	optional string name = 2;
	optional string password = 3;
	optional int32 ma = 33;
}

message Users{
	repeated User groups = 1;
}

我们在命令行输入

$ protoc --cpp_out=./  userInfo.proto

得到文件userInfo.pb.ccuserInfo.pb.h

在h头文件中可以看到生成的源码文件;

文件二:main.cpp
//“main.cpp"
#include<stdio.h>
#include<string.h>
#include "./test.pb.h"

using namespace ssp;
int hex_print(char ch){
	int p1 = (ch>>0)&1;
	int p2 = (ch>>1)&1;
	int p3 = (ch>>2)&1;
	int p4 = (ch>>3)&1;
	int p5 = (ch>>4)&1;
	int p6 = (ch>>5)&1;
	int p7 = (ch>>6)&1;
	int p8 = (ch>>7)&1;
	
	printf("%c :: %d=%d%d%d%d %d%d%d%d",ch,ch,p8,p7,p6,p5,p4,p3,p2,p1);
	return 0;
}

int main(){
	User u1;
	u1.set_age(1);
	u1.set_name("hello");
	u1.set_password("123456");
	u1.set_ma(13);
	char buffer[1024] ;
	memest(buffer,0,sizeof(buffer));
	u1.SerializeToArray(buffer,1024);
	//序列化!!很重要,将数据序列化在网络上传输,大大的提高时间!
	//序列化:二进制文件在网络中传输
	//SerializeToArray 需要将packet命名空间定义进来。
	int len = strlen(buffer);
	for(int i = 0; i < len; ++i){
		hex_print(buffer[i]);
	}
	return 0;
}

在命令行输入:

$ g++ main.cpp test.pb.cc -lprotobuf
$ ./a.out

运行执行文件得到结果为:

:: 8=0000 1000  
//5+3: 5:(00001)->filed=1; 3:(000)->type = 0.
 :: 1=0000 0001  
 //1+7: 1:(0)->不用next byte(也标志该字段读完了); 7:(0000001)->value = 1;
 //"1":用了两个字节
 
 :: 18=0001 0010 
 //5+3: 5:(00010)->field=2; 3:(010)->type=2(变长),下一个字节表示长度.
 :: 5=0000 0101 
 //1+7: 1:(0)->->不用next byte(也标志该字段读完了); 7:(0000101)->value=5(type=2,本字节value表示长度,同样的,很长时一个字节表示不了会将(1)位变成1,继续高位存在下一个字节).
h :: 104=0110 1000 
e :: 101=0110 0101
l :: 108=0110 1100
l :: 108=0110 1100
o :: 111=0110 1111  
//"hello":用了7个字节

 :: 26=0001 1010 
 //5+3: 5:(00011)->filed=3; 3:(000)->type = 2.
 :: 6=0000 0110  //1+7原则:见上,长度为6;
1 :: 49=0011 0001
2 :: 50=0011 0010
3 :: 51=0011 0011
4 :: 52=0011 0100
5 :: 53=0011 0101
6 :: 54=0011 0110
//”123456“ :用了8个字节。

 :: -120=1000 1000 
 // error: 5+3: 5:(10001)->filed=17; 3:(000)->type = 0.(不对呀!field=33 呀,因为protobuf会压缩自己,首位为1,即需要读next byte);
 //5+3: s:(000)->type = 0;
 :: 2=0000 0010
 //1+7: 1(0)->不用next byte,此时field =(00000100001) =33 ;
 //5+3首位为1,说明5+3变成了(1+4+next byte(1+7)) + 3;而field为高7+低4,warning:若1+7的1为”1“,仍旧要next byte!!
 :: 13=0000 1101
 //1+7: 1(0)->不用next byte,value=(0001101)=13.
 //"13":用了3字节。

eg:有部分显示不出来是因为是空格之前的不可以打印的字符。

可以看到压缩后占用的字节数明显下降!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

四库全书的酷

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

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

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

打赏作者

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

抵扣说明:

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

余额充值