protobuf在C#项目中的使用
在C#项目中,有时候会使用到使用到protobuf来作为通信时数据交换的格式。
protobuf(ProtocolBuffer,简称PB),是google 的一种数据交换的格式。这是一种二进制的格式,比使用xml更方便,数据量更小,数据交换速度更快。是一种兼容性比较好的数据传输格式,主要用于通信协议、数据存储等方面。
protobuf的C#版本(protobuf-net)下载地址:https://code.google.com/p/protobuf-net/,
将其保存为ProtoGen目录。
注意:在使用protobuf通信的时候,需要保证其版本号的一致性。
1、制定proto协议
创建一个后缀为“.proto”的文件,即为我们的协议文件。文件格式如下:
示例:TestParam.proto(协议具体规则见附录)
option optimize_for = SPEED;
message TestParam
{
required string string_param = 1;
optional double double_param = 2;
required int32 int_param = 3;
optional int32 default_param = 4 [default = 1];
repeated TestParam2 repeated_param = 5;
......
}
message Param2
{
required enum enum_param
{
flag_a = 0;
flag_b = 1;
flag_c = 2;
}
......
}
......
2、编译
“.proto”文件
将TestParam.proto文件复制到ProtoGen目录下,并在此目录下执行命令行语句:
protogen -i:TestParam.proto -o:TestParam.cs
则可以由
TestParam.proto得到
TestParam.cs的C#文件
为了方便,可以将该命令保存为一个批处理文件(如csharpPb.bat),使用时修改文件内容并执行此批处理文件即可。
如新的proto文件为
TestParam2.proto,则将
csharpPb.bat内容改为:
protogen -i:TestParam2.proto -o:TestParam2.cs
3、添加到项目中
将生成的C#文件TestParam.cs复制到C#项目中,并添加对protobuf-net.dll的引用,即可在项目中使用protobuf定义的TestParam结构
。
附录:proto协议规则
a、协议文件proto一般以“option optimize_for = SPEED;”为开头。
SPEED为文件级别,protobuf定义了3种optimize_for级别:
SPEED:速度优先级别,生成的代码运行效率更高;
CODE_SIZE:大小优先级别,生成的代码编译后所占的空间更少;
LIFT_RUNTIME:以牺牲protobuf提供的反射功能为代价,达到生成的代码既执行效率高,编译后所占的空间又少的目的。
注:文件级别缺省状态下默认为SPEED。
b、message是消息定义关键字,等同于class或struct。
c、TestParam、Param2等是消息名,等同于类名或结构体名。
d、required、optional、repeated是限定关键字
required:该字段为必要字段,必须在序列化、反序列化之前赋值(不能为空);
optional:一般字段;
repeated:数组结构字段(主结构中字段repeat_param即为节点是子结构Param2的数组);
一般来说主结构中至少有一个字段为required。
e、默认值
[default = 默认值]
f、数据类型
N:打包的字节不固定
.proto Type |
Notes |
C++ Type |
Java Type |
double |
|
double |
double |
float |
|
float |
float |
int32 |
Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. |
int32 |
int |
int64 |
Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. |
int64 |
long |
uint32 |
Uses variable-length encoding. |
uint32 |
int |
uint64 |
Uses variable-length encoding. |
uint64 |
long |
sint32 |
Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. |
int32 |
int |
sint64 |
Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. |
int64 |
long |
fixed32 |
Always four bytes. More efficient than uint32 if values are often greater than 228. |
uint32 |
int |
fixed64 |
Always eight bytes. More efficient than uint64 if values are often greater than 256. |
uint64 |
long |
sfixed32 |
Always four bytes. |
int32 |
int |
sfixed64 |
Always eight bytes. |
int64 |
long |
bool |
|
bool |
boolean |
string |
A string must always contain UTF-8 encoded or 7-bit ASCII text. |
string |
String |
bytes |
May contain any arbitrary sequence of bytes. |
string |
ByteString |
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)