Protobuf.网络使用

Protobuf.NET using

本文关键字:网络 Protobuf      更新时间:2023-10-16

需要在托管c#和非托管c++之间发送一些数据。经过一番研究我尝试使用Protobuf.NET。

我不确定我是否理解ProtoBuf的功能…

  1. 在Proto中构建类型定义。我需要这个类型定义在两个项目c++和c#
  2. 使用命令行工具"protogen.exe"从类型定义中获取。cs和。cpp, .h文件
  3. 将。cs文件复制到我的c#项目中,将。cpp, .h文件复制到我的c++解决方案中。

看来我太笨了,无法解决这个问题。这是我的问题和疑问。

  1. 是否可以在c#中定义类型来生成c++中的文件?
  2. 尝试使用命令行工具protogen.exe处理以下文件

test1.proto

using ProtoBuf;
namespace ProtocolBuffers
{
    [ProtoContract]
    class Person
    {
        [ProtoMember(1)]
        public int Id {get;set;}
        [ProtoMember(2)]
        public string Name { get; set; }
    }
}

test2.proto

message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}

什么都不适合我。真的什么都试过了。我把原型文件放进命令行目录,尝试了每个选项来设置目录。如何轻松地构建它们?第二个文件是用c++的标准原型命令行工具工作,但我也需要它用于c#。真的很需要你的帮助。

首先,请注意protobuf-net只是。net的一个可用实现;无论如何…

" test1。"proto"不是。proto——它是c#;使用protobuf-net时,.proto不是必需的,但在您的互操作场景中,这是一个非常好的主意。有VS2010插件,或者在protobuf-net zip中的protogen工具:

 protogen -i:test2.proto -o:test2.cs

这将生成test2.cs,内容为:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from: test2.proto
namespace test2
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Person")]
  public partial class Person : global::ProtoBuf.IExtensible
  {
    public Person() {}
    private int _id;
    [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
    public int id
    {
      get { return _id; }
      set { _id = value; }
    }
    private string _name;
    [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"name", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public string name
    {
      get { return _name; }
      set { _name = value; }
    }
    private string _email = "";
    [global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)]
    [global::System.ComponentModel.DefaultValue("")]
    public string email
    {
      get { return _email; }
      set { _email = value; }
    }
    private global::ProtoBuf.IExtension extensionObject;
    global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
      { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
  }
}

请注意,如果您想要,例如,它尝试大小写规范化(因此您得到Id, Name, Email等),或者希望它包含额外的序列化器支持(BinaryFormatter, DataContractSerializer等),则有额外的开关

test1.proto不是有效的.proto文件。

test2.proto看起来很好。请注意,为了生成c++代码,您将需要来自protobuf主项目的编译器,而为了生成c#代码,您将需要来自protobuf-net的编译器。我猜你的大多数问题来自没有意识到你需要两个不同的编译器(但你提供相同的.proto文件给两者)。

虽然我对ProtoBuf了解不多,但我知道有一种更简单的方法可以做到这一点。如果上面的类表示您要发送到c++的数据,只需使用序列化并序列化您的类,然后您就可以将类和数据输出到XML文件。然后可以使用XmlLite或其他XML阅读器读取类和数据。您甚至可以使用Boost来读取序列化的类。