在网上查了一下,虽然有很多文章介绍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. 《浏览器工作原理与实践》<05>渲染流程(上):HTML、CSS和JavaScript,是如何变成页面的?

    在上一篇文章中我们介绍了导航相关的流程,那导航被提交后又会怎么样呢?就进入了渲染阶段.这个阶段很重要,了解其相关流程能让你“看透”页面是如何工作的,有了这些知识,你可以解决一系列相关的问题,比如能熟练 ...

  2. 5.安装bacula-web(监控页面)

    1.   安装bacula-web(监控页面) 用途:监控bacula状态. http://docs.bacula-web.org/en/master/index.html bacula-web-7. ...

  3. VS---《在VS2010中 使用C++创建和使用DLL》(002)

    VS---<在VS2010中 使用C++创建和使用DLL>(002) 前面初认识了创建和调用DLL,在VS工程下可以通过在 同一工程.不同工程 下创建和调用DLL.现在,同一工程下创建和调 ...

  4. eclipse新建jsp模版设置

    第一步:找到JSP模板 eclipse -- >perferences - >Web -> jsp files -Editor ->templates: 第二步:准备编辑JSP ...

  5. HashMap源码分析三

      HashMap的源码,在jdk1.5中相比jdk1.4,改动不大,有几个方面   1 jdk1.5中引入了范型,在HashMap中也有体现 2 引入了另一个hash值的计算方式,不过默认是关闭状态 ...

  6. python+Appium自动化:屏幕截图

    屏幕截图 主要是为了程序出现错误时,开发除了可以分析日志之外,还可以进行截图更好地去定位问题. 截图一般有两种方法: 第一种save_screenshot(self,filename) driver. ...

  7. 08—mybatis注解配置二

    动态sql mybatis的注解也支持动态sql.mybatis提供了各种注解,如@InsertProvider.@UpdateProvider.@DeleteProvider和@SelectProv ...

  8. 前端知识体系:JavaScript基础-变量和类型

    前端工程师自检清单 1. JavaScript规定了几种语言类型 2. JavaScript对象的底层数据结构是什么 3. Symbol类型在实际开发中的应用.可手动实现一个简单的 Symbo 4. ...

  9. Lighting Techinology of the Last Of Us (2013 SIGGRAPH)

    Lighting Techinology of the Last Of Us(2013 SIGGRAPH) or "Old Lightmaps - New Tricks" 原作:M ...

  10. ACM-ICPC 2016 沈阳赛区现场赛 I. The Elder && HDU 5956(斜率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5956 题意:一颗树上每条边有个权值,每个节点都有新闻要送到根节点就是1节点,运送过程中如果不换青蛙就是 ...