简体   繁体   English

协议缓冲区,让C#与C ++通信:输入问题和架构问题

[英]Protocol buffers, getting C# to talk to C++ : type issues and schema issues

I am about to embark on a project to connect two programs, one in c#, and one in c++. 我即将开始一个项目来连接两个程序,一个在c#中,一个在c ++中。 I already have a working c# program, which is able to talk to other versions of itself. 我已经有一个有效的c#程序,可以与其他版本的自己交谈。 Before I start with the c++ version, I've thought of some issues: 在开始使用c ++版本之前,我已经想到了一些问题:

1) I'm using protobuf-net v1. 1)我正在使用protobuf-net v1。 I take it the .proto files from the serializer are exactly what are required as templates for the c++ version? 我认为来自序列化程序的.proto文件正是c ++版本的模板所需要的? A google search mentioned something about pascal casing, but I have no idea if that's important. 谷歌搜索提到了关于pascal外壳的一些内容,但我不知道这是否重要。

2) What do I do if one of the .NET types does not have a direct counterpart in c++? 2) 如果其中一种.NET类型在c ++中没有直接对应物,我该怎么办? What if I have a decimal or a Dictionary? 如果我有小数或字典怎么办? Do I have to modify the .proto files somehow and squish the data into a different shape? 我是否必须以某种方式修改.proto文件并将数据压缩成不同的形状? (I shall examine the files and see if I can figure it out) (我将检查文件,看看我是否可以搞清楚)

3) Are there any other gotchas that people can think of? 3)人们可以想到还有其他陷阱吗? Binary formats and things like that? 二进制格式和类似的东西?

EDIT I've had a look at one of the proto files now. 编辑我现在看了一个原型文件。 It seems .NET specific stuff is tagged eg bcl.DateTime or bcl.Decimal. 似乎.NET特定的东西被标记为例如bcl.DateTime或bcl.Decimal。 Subtypes are included in the proto definitions. 子类型包含在原型定义中。 I'm not sure what to do about bcl types, though. 不过,我不知道如何处理bcl类型。 If my c++ prog sees a decimal, what will it do? 如果我的c ++编程看到小数,它会怎么做?

  1. Yes, the proto files should be compatible. 是的,原型文件应该兼容。 The casing is about conventions, which shouldn't affect actual functionality - just the generated code etc. 外壳是关于约定的,它不应该影响实际的功能 - 只是生成的代码等。

  2. It's not whether or not there's a directly comparable type in .NET which is important - it's whether protocol buffers support the type which is important. 不管.NET中是否存在直接可比类型是重要的 - 协议缓冲区是否支持重要的类型。 Protocol buffers are mostly pretty primitive - if you want to build up anything bigger, you'll need to create your own messages. 协议缓冲区大多非常原始 - 如果你想要构建更大的东西,你需要创建自己的消息。

  3. The point of protocol buffers is to make it all binary compatible on the wire, so there really shouldn't be gotchas... read the documentation to find out about versioning policies etc. The only thing I can think of is that in the Java version at least, it's a good idea to make enum fields optional, and give the enum type itself a zero value of "unknown" which will be used if you try to deserialize a new value which isn't supported in deserializing code yet. 协议缓冲区的目的是使它在线上都是二进制兼容的,所以真的不应该有问题...阅读文档以了解版本控制策略等。我唯一能想到的是在Java中至少版本,最好让枚举字段可选,并给枚举类型本身一个“未知”的零值,如果你试图反序列化一个新的值,这个值在反序列化代码中尚未得到支持。

Some minor additions to Jon's points: Jon的一些小补充点:

  • protobuf-net v1 does have a Getaproto which may help with a starting point, however, for interop purposes I would recommend starting from a .proto; protobuf-net v1确实有一个Getaproto可能有助于起点,但是,为了互操作目的,我建议从.proto开始; protobuf-net can work this was around too, either via "protogen", or via the VS addin protobuf-net可以通过“protogen”或VS插件来解决这个问题
  • other than that, you shouldn't have my issues as long as you remember to treat all files as binary; 除此之外,只要您记得将所有文件视为二进制文件,就不应该有我的问题; opening files in text mode will cause grief 在文本模式下打开文件会导致悲伤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM