在网上查了一下,虽然有很多文章介绍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文件内容如下:

  1. message SearchRequest {
  2. required string query = 1;
  3. optional int32 page_number = 2;
  4. optional int32 result_per_page = 3;
  5. }

数据类型如下:

.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
 

http://blog.csdn.net/wangpei421/article/details/8012295

Google Protocol Buffer 用法 C#的更多相关文章

  1. Google Protocol Buffer入门

    简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...

  2. Google Protocol Buffer 的使用和原理[转]

    本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...

  3. Google Protocol Buffer 的使用

    简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...

  4. 学习Google Protocol buffer之概述

    XML这种属于非常强大的一种格式,能存储任何你想存的数据,而且编辑起来还是比较方便的.致命的缺陷在于比较庞大,在某些情况下,序列化和解析都会成为瓶颈.这种对于实时性很强的应用来说,就不太适合了,想象下 ...

  5. Google Protocol Buffer的安装与.proto文件的定义

    什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...

  6. Google Protocol Buffer 的使用和原理

    Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...

  7. Google Protocol Buffer 的编码方式

    Google Protocol Buffer 使用到了两种编码方式:Varints 和 zigzag. 一 Varints 编码 每个 byte 只用 7bit 表示数字,最高位 bit作为标志位,如 ...

  8. Google Protocol Buffer 协议

    1. Protocol Buffers 简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化 ...

  9. Google Protocol Buffer

    Google Protocol Buffer(protobuf)是一种高效且格式可扩展的编码结构化数据的方法.和JSON不同,protobuf支持混合二进制数据,它还有先进的和可扩展的模式支持.pro ...

随机推荐

  1. idea自动在文件头中添加作者和创建时间

    设置路径 : File -> Settings -> Editor -> File and Code Templates 定制头模板: /** * @Author: chancy * ...

  2. QTP10破解步骤(亲试有效)

    QTP10破解步骤: 1.安装qtp,一路默认下来(一定要默认安装目录),一直到要求输入License的界面 2.拷贝mgn-mqt82.exe到C:\Program Files\Mercury In ...

  3. 微信小程序开发(五)数据绑定

    承接上篇博客. // index.js Page({ data: { time: (new Date()).toString(), addr: "北京" }, but: funct ...

  4. 异步函数Demo

    private static async Task<String> IssueClientRequestAsync(string serverName, string message) { ...

  5. ACM中值得注意/利用的C++语法特性

    C++ 的易踩坑点 随时补充 STL不能边循环边erase() //自己写的求交集RE了 for (set <int> ::iterator it = s.begin(); it != s ...

  6. shell脚本基础编写

    shell脚本的格式 名称:Shell 脚本文件的名称可以任意,但为了避免被误以为是普通文件,建议将 .sh 后缀加上,以表示是一个脚本文件. shell 脚本中一般会出现三种不同的元素: 第一行的脚 ...

  7. iOS 中通过kvc 获取数组的均值、求和、最大最小值等

    NSArray *values = @[@, @, @, @, @, @, @, @, @, @, @, @, @, @, @, @, @]; NSNumber *avg = [values valu ...

  8. Error creating bean with name 'documentationPluginsBootstrapper' defined in URL

    启动报错 Error starting ApplicationContext. To display the auto-configuration report re-run your applica ...

  9. GPU Debugger

    https://gpuopen.com/presentations/2019/digital-dragons-2019-make-your-game-friendly.pdf https://grap ...

  10. np中的温故知新

    1.一维数组中寻找与某个数最近的数 # 一维数组中寻找与某个数最近的数 Z=np.random.uniform(0,1,20) print("随机数组:\n",Z) z=0.5 m ...