Google Protocol Buffer 用法 C#
2015-07-27 12:21
363 查看
在网上查了一下,虽然有很多文章介绍Protocol Buffer,但是实际使用起来,还是会遇到很多问题,所以我想应该有一个指南一样的东西,让新手很快就能使用它。
Protocol Buffer简写为Protobuf,是Google开发的一种储存数据的方式,功能与XML一样,但更方便,数据量更小,速度更快,在序列化和反序列化的时候使用,有很大的优势。比如,网络游戏的通讯协议编写。更重要的是,它是一个开源项目。
闲话不多说,下载地址:
http://code.google.com/p/protobuf/downloads/list
由于Protobuf不能生成C#代码,所以就要用到另外一个开源项目protobuf-net,下载地址:
http://code.google.com/p/protobuf-net/
@echo off
rem 查找文件
for /f "delims=" %%i in ('dir /b ".\*.proto"') do echo %%i
for /f "delims=" %%i in ('dir /b/a "*.proto"') do protoc -I=. --cpp_out=. %%i
for /f "delims=" %%i in ('dir /b/a "*.proto"') do protogen -i:%%i -o:%%~ni.cs
pause
用法是,在命令行用protoc.exe依据你自己定义的.proto文件,来生成.cpp文件
用protobuf-net的protogen.exe来生成.cs文件,可以写一个批处理,如下:
protoc -I=. --cpp_out=. Net.proto //在当前目录下,以cpp方式编译Net.proto
protogen -i:Net.proto -o:Net.cs //在当前目录下,用Net.proto生成Net.cs
其实两句功能是一样,只是分别生成了Net.proto的cpp文件和cs文件,这样就可以在两种语言编写的应用程序间通信了。
Net.proto文件内容如下:
message SearchRequest {
required string query = 1;
optional int32 page_number = 2;
optional int32 result_per_page = 3;
}
数据类型如下:
http://blog.csdn.net/wangpei421/article/details/8012295
Protocol Buffer简写为Protobuf,是Google开发的一种储存数据的方式,功能与XML一样,但更方便,数据量更小,速度更快,在序列化和反序列化的时候使用,有很大的优势。比如,网络游戏的通讯协议编写。更重要的是,它是一个开源项目。
闲话不多说,下载地址:
http://code.google.com/p/protobuf/downloads/list
由于Protobuf不能生成C#代码,所以就要用到另外一个开源项目protobuf-net,下载地址:
http://code.google.com/p/protobuf-net/
@echo off
rem 查找文件
for /f "delims=" %%i in ('dir /b ".\*.proto"') do echo %%i
for /f "delims=" %%i in ('dir /b/a "*.proto"') do protoc -I=. --cpp_out=. %%i
for /f "delims=" %%i in ('dir /b/a "*.proto"') do protogen -i:%%i -o:%%~ni.cs
pause
用法是,在命令行用protoc.exe依据你自己定义的.proto文件,来生成.cpp文件
用protobuf-net的protogen.exe来生成.cs文件,可以写一个批处理,如下:
protoc -I=. --cpp_out=. Net.proto //在当前目录下,以cpp方式编译Net.proto
protogen -i:Net.proto -o:Net.cs //在当前目录下,用Net.proto生成Net.cs
其实两句功能是一样,只是分别生成了Net.proto的cpp文件和cs文件,这样就可以在两种语言编写的应用程序间通信了。
Net.proto文件内容如下:
message SearchRequest {
required string query = 1;
optional int32 page_number = 2;
optional int32 result_per_page = 3;
}
数据类型如下:
.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[1] |
uint64 | Uses variable-length encoding. | uint64 | long[1] |
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[1] |
fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long[1] |
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 |
相关文章推荐
- jstl自定义函数的使用
- JavaScript中利用Array和Object实现Map的方法
- 在Ubuntu平台上创建Cordova Camera HTML5应用
- JavaScript数据库TaffyDB用法实例分析
- <input type = "submit"> 提交方式和用js的form.submit()有什么区别?
- HTML 表
- js 使用闭包实现单例
- html5之canvas基础1
- bzoj1030: [JSOI2007]文本生成器
- 多余字以省略号显示的css样式
- JQUERY 一些技巧在实际中的应用
- js正则验证
- 了解HTML5和“她”的 API (二)
- USACO 4.2.2The Perfect Stall 完美的牛栏(二分图最大匹配)
- 精通CSS+DIV简单小结(二)
- JS实现冒泡排序,插入排序和快速排序并排序输出
- Jsp中图片显示问题以及显示绝对路径图片问题
- H5移动前端完美布局之-margin百分比的使用
- javascript简单实现滑动菜单效果的方法
- jQuery中attr()的使用