Protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多。除了比Json、XML有速度上的优势和使用上的方便外,protocolbuf还可以做到向前兼容和向后兼容。
protobuf 虽然只支持JAVA、C++和Pyton,但protobuf社区的protobuf.net组件让protobuf可以支持更多的语言,其中就包括了C#。
protobuf协议
定义protobuf协议必须创建一个以.proto为后缀的文件,以本篇 创建了一个名为myproto.proto的文件,如下:
- package ProtoTest;
-
- message TestInfo{
- required string test = 1;
- optional int32 num = 2;
- }
-
- message Msg{
- required int32 id = 1;
- optional TestInfo msg = 2;
- optional string str = 3 [defalut="Test String"];
- }
package在Java里面代表这个文件所在的包名,在c#里面代表该文件的命名空间,message代表一个类, required 代表该字段必填,optional 代表该字段可选,并可以为其设置默认值,string的默认值格式为[defalut="字符串"] 整型的默认值格式为[defalut=23333]
下面是protobuf在.proto文件中的字段类型转换表:
.proto Type | Notes | C++ Type | JavaType | Python Type[2] |
double | double | double | float |
float | 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 | 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 | int/long[3] |
uint32 | Uses variable-length encoding. | uint32 | int[1] | int/long[3] |
uint64 | Uses variable-length encoding. | uint64 | long[1] | int/long[3] |
sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int |
sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long[3] |
fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | int[1] | int |
fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long[1] | int/long[3] |
sfixed32 | Always four bytes. | int32 | int | int |
sfixed64 | Always eight bytes. | int64 | long | int/long[3] |
bool | bool | boolean | boolean |
string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String | str/unicode[4] |
bytes | May contain any arbitrary sequence of bytes. | string | ByteString | str
|
如何编译proto文件
在windows如何下载并编译protobuff,这部分参考博客:http://kuaile.in/archives/1214
Google下载protobuff下载地址:https://developers.google.com/protocol-buffers/docs/downloads
。
在解压后的文件夹中,打开vsprojects目录,目录中的文件如图所示:
分别依次右键编译生成libprotobuf.lib,libprotobuf-lite.lib,libprotoc.lib,protoc.exe 到这里就完成了protobuff的编译。
打开cmd,cd到protoc.exe所在的文件夹,即../vsproject/。假设E:\proto\存在myproto.proto文件。输入
- protoc -I=e:\proto --cpp_out=e:\proto e:\proto\myproto.proto
就会在e:\proto 下生成myproto.h 和myproto.cpp文件,测试成功就说明protobuff已经安装成功。
如何将proto文件编译成.cs文件
接下来要将proto文件编译成.cs文件。.net版的protobuf来源于proto社区,有两个版本:
这里使用的是protobuf-net,下载解压后,将
Precompile\precompile.exe 以及 ProtoGen\protogen.exe 两个文件加入到环境变量中,打开cmd,
使用方法如下:
- protogen -i:input.proto -o:output.cs
- protogen -i:input.proto -o:output.xml -t:xml
- protogen -i:input.proto -o:output.cs -p:datacontract -q
- protogen -i:input.proto -o:output.cs -p:observable=true
以E:\proto\myproto.proto为例,cmd下输入
- protogen -i:e:\proto\myproto.proto -o:myproto.cs
运行成功后就会在文件夹内生成myproto.cs文件
如何将myproto.cs加入你的项目中
有两种方法可以将.cs文件加入到你的项目中:第一种就是预编译成.dll文件,然后加入到你的项目中就可以了。第二种就是直接使用源码(这样的话编译速度会比较慢),将你的.cs文件直接加入到你的项目中,然后在项目中引用protobuf-net.dll的库,就可以使用了。
参考文章:
在ios android设备上使用 Protobuf (使用源码方式)
http://game.ceeger.com/forum/read.php?
tid=14359&fid=27&page=1
android与PC,C#与Java利用protobuf 进行无障碍通讯 http://www.cnblogs.com/TerryBlog/archive/2011/04/23/2025654.html
windows下Google Protocol Buffer 编译安装教程 http://kuaile.in/archives/1214