下面这个用法是参照protobuf-csharp-port的官方wiki,参见:

https://code.google.com/p/protobuf-csharp-port/wiki/GettingStarted

官方原文里的用法与代码已经有些不匹配了,需要做一些小的修改。

准备工作

1.首先从https://code.google.com/p/protobuf-csharp-port这个上面把源代码下载下来,我这个版本是protobuf-csharp-port-2.4.1.521-source(r523)

2.下载后是个压缩包,解压目录,如下图:

点开“Build”文件夹:

看见那个BuildAll.bat,点击它会自动执行编译操作,编译完成后将出现build_output和build_temp两个输出文件夹,其中build_output如下图所示例:

生成的Google.ProtocolBuffers.dll将被用作外部链接的DLL使用,这里有个问题,能不能不用做DLL使用呢?因为众所周知的AOT,JIT问题。

例子: an address book

这块将会用一个程序来演示如何使用这个protocBuffer

The .proto file

看看下面这个proto文件,分析下这个文件都描述了什么信息。此文件在 你下载的源代码文件夹的protos/tutorial。

package tutorial;

import "google/protobuf/csharp_options.proto";

option (google.protobuf.csharp_file_options).namespace = "Google.ProtocolBuffers.Examples.AddressBook";
option (google.protobuf.csharp_file_options).umbrella_classname = "AddressBookProtos"; option optimize_for = SPEED; message Person {
required string name = ;
required int32 id = ; // Unique ID number for this person.
optional string email = ; enum PhoneType {
MOBILE = ;
HOME = ;
WORK = ;
} message PhoneNumber {
required string number = ;
optional PhoneType type = [default = HOME];
} repeated PhoneNumber phone = ;
} // Our address book file is just one of these.
message AddressBook {
repeated Person person = ;
}

这个文件和谷歌官方提供的基本一致,但做了一些调整:

  • 为了简单期间去掉了输出Java代码的选项。这里如果需要就把Java选项加上即可,和C#没有冲突。
  • 导入项代码: google/protobuf/csharp_options.proto ,目的是使用C#扩展。
  • 指定了两项C#特有的选项:
    • The name of the class containing the descriptor representing the overall .proto file. (This is called the "umbrella" class.) The generated source file is also named after this class. In this case we're using AddressBookProtos.
    • 生成类的命名空间。这里用的是: Google.ProtocolBuffers.Examples.AddressBook.
  • 指定了一个优化的选项。会多生成一些代码,但能提高运行效率。
  • 还有其他的选项,可以看看code.google上的description里的说明。

生成源代码

转到你的源代码目录,执行以下操作

protogen ..\..\protos\tutorial\addressbook.proto
..\..\protos\google\protobuf\csharp_options.proto
..\..\protos\google\protobuf\descriptor.proto
--proto_path=..\..\protos

看看build_temp文件夹,如下图所示:

在GeneratedSource文件夹中,就能找到AddressBookProtos.cs文件了:

这个AddressBookProtos.cs文件是我们后边需要用到的,其他文件只是Google用到的,我们不用去管,或者能把这些代码作为源代码引入主工程?不得而知。

注意事项:

  1. Make sure that you do not use a non-ASCII encoding with your text file. The protoc.exe compiler will complain with the following message:

    tutorial/addressbook.proto::: Expected top-level statement (e.g. "message").

    The best way to fix this is with Visual Studio. Open the proto file and select "File" -> "Save as...". From the save dialog click the down arrow next to     the save button and select "Save with Encoding". Select the "US-ASCII" codepage (near the bottom) and click save.

  2. It's often easiest keep all your proto files in a single directory tree. This allows to omit the --proto_path option by running protoc.exe from the top of     the this directory tree. Always keep the 'google/protobuf/*.proto' files available so that you can import them to specify options, or use the options     through protogen.

  3.Unlike a programming language like C# it is usually expected that your proto file will specify numerous messages, not one. Name your proto files     based on the namespace, not the message. Create proto files only as often as you would create a new namespace to organize those classes.

使用生成的源代码

为了使用上面生成的cs文件,我们需要执行下面这些操作:

  1. 创建一个builder,后边将用它来构造Person这个消息体
  2. 设置builder的属性
  3. 使用这个builder来创建 Person 消息体
  4. 将 Person 写入内存流
  5. 使用上面写好数据的内存流,再创建一个新的Person消息体
  6. 创建一个AddressBook的builder ,然后把这个刚创建的Person消息体赋值给它
  7. 使用AddressBook的builder 和上面的数据流创建 AddressBook

将这个AddressBook里的Person和第三步创建的Person进行比较,看数据是否一致。

    static void Sample()
{
byte[] bytes;
//Create a builder to start building a message
Person.Builder newContact = Person.CreateBuilder();
//Set the primitive properties
newContact.SetId()
.SetName("Foo")
.SetEmail("foo@bar");
//Now add an item to a list (repeating) field
newContact.AddPhone(
//Create the child message inline
Person.Types.PhoneNumber.CreateBuilder().SetNumber("555-1212").Build()
);
//Now build the final message:
Person person = newContact.Build();
//The builder is no longer valid (at least not now, scheduled for 2.4):
newContact = null;
using(MemoryStream stream = new MemoryStream())
{
//Save the person to a stream
person.WriteTo(stream);
bytes = stream.ToArray();
}
//Create another builder, merge the byte[], and build the message:
Person copy = Person.CreateBuilder().MergeFrom(bytes).Build(); //A more streamlined approach might look like this:
bytes = AddressBook.CreateBuilder().AddPerson(copy).Build().ToByteArray();
//And read the address book back again
AddressBook restored = AddressBook.CreateBuilder().MergeFrom(bytes).Build();
//The message performs a deep-comparison on equality:
if(restored.PersonCount != || !person.Equals(restored.PersonList[]))
throw new ApplicationException("There is a bad person in here!");
}

  用VS2013,创建一个WinForm程序,拖一个button上去,双击出事件,调用Sample函数即可,这样就通了。里面还有很多细节没说清楚,不过好歹有整块的东西了。那么后边的工作,需要分成几步来执行:

1.将上述流程分析清楚,有一个初步的架构图和UML图。

2.C#客户端的二进制内存流,显示需要铺设一个二进制的内存流管理器,是否需要参考之前的那个二进制管理器呢。

3.如何集成到Unity里,首先要写一份关于Unity的代码规范和内存处理规范。如果集成到Unity里,那么势必两头都要写代码,加油啊。

4.如何搭建一个Java服务器,支持解析数据,并发送和接收。

5.连接Java服务器通信。客户端和服务器两头看看能否顺利工作。

初用protobuf-csharp-port的更多相关文章

  1. 在UnrealEngine4中使用Google Protobuf

    转自:https://blog.csdn.net/or_7r_ccl/article/details/54986393 在UnrealEngine4中使用Google Protobuf         ...

  2. Google Protocol Buffers 快速入门(带生成C#源码的方法)

    Google Protocol Buffers是google出品的一个协议生成工具,特点就是跨平台,效率高,速度快,对我们自己的程序定义和使用私有协议很有帮助. Protocol Buffers入门: ...

  3. [原创]首次SDN比赛的记录-部分

    SDN大赛环境搭建和第一大题实现 由于物理设备不足的原因,故用虚拟机实现Floodlight控制器,openvswitch(以下简称:OVS)和mininet各种要用到的SDN环境的搭建.下面将给出它 ...

  4. MAC 下 STF 的环境搭建和运行

    STF --WEB 端批量移动设备管理控制工具 安装各种包 (首先安装Macport,因为后面需要用到port:http://www.ccvita.com/434.html) linux的基本包安装, ...

  5. protobuf初体验

    概念介绍 Protocol buffers 是google公司的与语言无关.与平台无关的.可扩张的为序列化话结构数据,就像xml一样,办事更加的小巧.快速.简单.Protocol buffers 目前 ...

  6. protobuf(Protocol Buffers)java初体验

    因为项目须要所以简单的研究了下protobuf.我也是參照网上的博客,所以大部分内容我也就不反复造轮子了.首先protobuf介绍点击这里,使用介绍点击这里,使用demo看这里. 我个人的第一个样例也 ...

  7. google protobuf初体验

    最近在读别人代码的时候发现一个的东西,名字叫protobuf, 感觉挺好用的,写在这里,留个记录.那么什么是protobuf 呢?假如您在网上搜索,应该会得到类似这样的文字介绍: Google Pro ...

  8. 使用CSharp编写Google Protobuf插件

    什么是 Google Protocol Buffer? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 ...

  9. ASP.NET Core 3.0 上的gRPC服务模板初体验(多图)

    早就听说ASP.NET Core 3.0中引入了gRPC的服务模板,正好趁着家里电脑刚做了新系统,然后装了VS2019的功夫来体验一把.同时记录体验的过程.如果你也想按照本文的步骤体验的话,那你得先安 ...

  10. RPC框架基础概念理解以及使用初体验

    RPC:Remote Procedure Call(远程服务调用) RPC是做什么的 通过RPC框架机器A某个进程可以通过网络调用机器B上的进程方法,就像在本地上调用一样. RPC可以基于HTTP或者 ...

随机推荐

  1. MVC Ajax Helper或jQuery异步方式加载部分视图

    Model: namespace MvcApplication1.Models { public class Team { public string Preletter { get; set; } ...

  2. linux基础-第七单元 用户、群组及权限的深入讨论

    怎样查看文件的权限 ls命令详解 root用户及文件的安全控制 什么是root用户 用户分类 文件操作权限 文件操作权限的字符表示 chown chgrp 使用符号表示法设定文件或目录上的权限 chm ...

  3. corefile 设置

    程序运行的过程中,可能会因为一些隐藏的bug导致崩溃,为了在出问题时,及时记录所在环境的情况,所以要设置core文件的产生.其实其本质就是把进程的内存保存到文件中去. 1.core文件的生成开关和大小 ...

  4. @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml

    项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore ...

  5. 我的STL学习之路

    说起STL(标准模板库),相信了解C++的都不会陌生吧^_^.LZ是从大三开始学习C++(ps:不是科班出身),并慢慢接触使用STL的,在学校中使用STL比较多的情况是写数据结构代码,使用STL实现数 ...

  6. CETV面试总结

    在通过CETV的网上笔试之后,我收到了面试通知,我纠结片刻,就买了第二天去广州的高铁,虽然感觉自己硬件方面的知识已经快忘完了,但还是想去一下,起码是一种经历.对一个路痴和知识储备不足的我来讲,这一切都 ...

  7. DPM检测模型 训练自己的数据集 读取接口修改

    (转载请注明作者和出处 楼燚(yì)航的blog :http://www.cnblogs.com/louyihang-loves-baiyan/ 未经允许请勿用于商业用途) 本文主要是针对上一篇基于D ...

  8. 怎样在ZBrush中快速绘制人体躯干

    之前我们对人体骨点的雕刻,了解了人体骨骼比例结构特征.今天的ZBrush教程将通过ZBrush®遮罩显示的特点对模型的人体躯干进行细致雕刻.文章内容仅以fisker老师讲述为例,您也可以按照自己的想法 ...

  9. POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳

    题意: 给你两个凸包,求其最短距离. 解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序. 还是说数据水了,没出乱给点或给逆时针点的数据呢..我直接默认顺时针给的点居然A ...

  10. 平滑过渡的战争迷雾(一) 原理:Warcraft3地形拼接算法

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9611887 作者:七十一雾央 新浪微博:http:/ ...