Google Protocol Buffer 用法 C#

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

时间: 2024-08-14 17:01:21

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

Google protocol buffer 使用和原理浅析 - 附带进阶使用方式

Protocol Buffer ??Google Protocol Buffer又简称Protobuf,它是一种很高效的结构化数据存储格式,一般用于结构化数据的串行化,简单说就是我们常说的数据序列化.这种序列化的协议非常轻便高效,而且是跨平台的,目前已支持多种主流语言(3.0版本支持C++, JAVA, C#, OC, GO, PYTHON等). ??通过这种方式序列化得到的二进制流数据比传统的XML, JSON等方式的结果都占用更小的空间,并且其解析效率也更高,用于通讯协议或数据存储领域是非常

Google Protocol Buffer 的使用和原理

Google Protocol Buffer 的使用和原理 1.https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ 2.参考资料:http://blog.csdn.net/educast/article/details/24764205 3.github源码:https://github.com/google/protobuf 4谷歌:https://developers.google.com/protocol-buffers/

【Google Protocol Buffer】Google Protocol Buffer

http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩展的序列化结构数据格式.目前提供了 C++.Java.Python 三种语言的 API. 17 评论: 刘 明, 软件工程师, 上海交大电

Google protocol buffer使用笔记

一 下载 Google下载地址:https://developers.google.com/protocol-buffers/docs/downloads?hl=zh-CN Github下载地址:https://github.com/google/protobuf 我这里下载版本:protobuf-2.6.1.tar.gz 二 编译 1 解压 将上面的压缩包解压到文件夹 protobuf-2.6.1 中. 2 编译 2.1 在protobuf-2.6.1中,找到vsprojects/protob

Google Protocol Buffer项目无法加载解决方案

http://blog.csdn.net/suixiangzhe/article/details/52171313 今天下载Google Protocol Buffer源码研究时发现打开工程后所有项目都提示无法加载,在输出中找到错误提示是未找到导入的项目"C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props",在网上找了资料是说缺少.NET Core 1.0 f

Google Protocol Buffer 协议

1. Protocol Buffers 简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可以使用该技术来持久化数据或者序列化成网络传输的数据.主要用于数据存储.通信协议等方面.现阶段支持C++.JAVA.Python.Objective-C.C#.Javascript等6种编程语言.Googel 公司 2015-12-31 更新了最新的版本Version 3.0.0-bet

Google Protocol Buffer安装编译及使用

最近玩了玩谷歌的Protocol Buffer,下面就简单介绍下 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过12,183 个.proto 文件,他们用于RPC 系统和持续数据存储系统.Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储

eclipse4.4的google protocol buffer的proto文件编辑器Protocol Buffer Editor安装

eclipse4.4的proto文件编辑器Protocol Buffer Editor安装 google protocol buffer 文件编辑器. 插件项目名称为protobuf-dt,是托管在google code上,国内现在已经连接不上google code了,在eclipse市场可以发现该插件,但是不能安装.在github上发现 有人已经做了相关的同步,可以在下面地址下载. 点击打开链接 其他地址也可以. 本人的为eclipse-jee-luna-SR2-win32-x86_64,安装

转Google Protocol Buffer 的使用和原理

Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩展的序列化结构数据格式.目前提供了 C++.Java.Python 三种语言的 API. 2010 年 11 月 18 日 内容 简介 一个简单的例子 和其他类似技术的比较 高级应用话题 Protobuf 的更多细节 结束语 参考资料 评论 在