怎么给 pb 消息添加消息号?
目前论坛里的方案:
-
二次包装(消息体封装消息体)
-
拼接消息号(拼接固定字节在消息头用于消息号解析)
我的方案:
- 使用默认值
默认值是什么?
其实这是 protobuf 的语法特性,只需要在 ; 前面加上 [default = 默认值] 就可以定义一个属性的默认值
package test;
syntax = "proto3";
message test {
uint32 __id = 1 [default = 100];
string data = 2;
}
使用默认值有什么好处?
-
更好的性能
-
节省传输字节
-
更方便的使用(发送/接收再也不需要关注消息号)
-
不需要再手动定义消息号
三种方式性能对比
1w 次

10w 次
测试代码在末尾
怎么省略消息号?
- 首先我们必须生成 static module 的 proto 协议文件
