C#下使用protobuf(Google Protocol Buffers)

  Protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多。除了比Json、XML有速度上的优势和使用上的方便外,protocolbuf还可以做到向前兼容和向后兼容。
        protobuf 虽然只支持JAVA、C++和Pyton,但protobuf社区的protobuf.net组件让protobuf可以支持更多的语言,其中就包括了C#。

protobuf协议
定义protobuf协议必须创建一个以.proto为后缀的文件,以本篇 创建了一个名为myproto.proto的文件,如下:

  1. package ProtoTest;  
  2.   
  3. message TestInfo{  
  4.  required string test = 1;  
  5.  optional int32 num = 2;  
  6. }  
  7.   
  8. message Msg{  
  9.  required int32 id = 1;  
  10.  optional TestInfo msg = 2;  
  11.  optional string str = 3 [defalut="Test String"];  
  12. }  
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

Java Type

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
github仓库地址:https://github.com/google/protobuf 
Google下载protobuff下载地址:https://developers.google.com/protocol-buffers/docs/downloads
在解压后的文件夹中,打开vsprojects目录,目录中的文件如图所示:



分别依次右键编译生成libprotobuf.liblibprotobuf-lite.liblibprotoc.libprotoc.exe  到这里就完成了protobuff的编译。
打开cmd,cd到protoc.exe所在的文件夹,即../vsproject/。假设E:\proto\存在myproto.proto文件。 输入
  1. protoc -I=e:\proto --cpp_out=e:\proto 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,下载地址为:http://code.google.com/p/protobuf-net/  写法上比较符合c#一贯的写法,而且效率更高。
另一个为protobuf-csharp-sport , 官方站点:http://code.google.com/p/protobuf-csharp-port/ 写法上跟java上的使用极其相似,比较遵循Google 的原生态写法,跨平台选择此版本比较好。
这里使用的是protobuf-net,下载解压后,将 Precompile\precompile.exe 以及 ProtoGen\protogen.exe 两个文件加入到环境变量中,打开cmd, 使用方法如下:
  1. //使用方法为:  
//使用方法为:
  1. protogen -i:input.proto -o:output.cs  
  2. protogen -i:input.proto -o:output.xml -t:xml  
  3. protogen -i:input.proto -o:output.cs -p:datacontract -q  
  4. protogen -i:input.proto -o:output.cs -p:observable=true  
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下输入
  1. protogen -i:e:\proto\myproto.proto -o:myproto.cs  
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

猜你喜欢

转载自blog.csdn.net/itliruochong/article/details/80139436