Protobuf.网络使用
Protobuf.NET using
需要在托管c#和非托管c++之间发送一些数据。经过一番研究我尝试使用Protobuf.NET。
我不确定我是否理解ProtoBuf的功能…
- 在Proto中构建类型定义。我需要这个类型定义在两个项目c++和c#
- 使用命令行工具"protogen.exe"从类型定义中获取。cs和。cpp, .h文件
- 将。cs文件复制到我的c#项目中,将。cpp, .h文件复制到我的c++解决方案中。
看来我太笨了,无法解决这个问题。这是我的问题和疑问。
- 是否可以在c#中定义类型来生成c++中的文件?
- 尝试使用命令行工具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来读取序列化的类。
- 我的神经网络不起作用 [XOR 问题]
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- 通过网络、跨平台传递std::变体是否安全
- 如何在MNIST上提高网络运行的准确性
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 使用OpenCV和覆盆子上的多个网络摄像头拍摄延时摄影,出现多个V4L错误
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 神经网络不学习.卡在50%
- 如何绑定 C++ gRPC 客户端的网络接口
- 如何继承 Protobuf 生成的C++类
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 在不知道套接字的情况下关闭网络连接
- 是否有 Windows 用户空间函数来枚举连接的网络共享?
- 在没有管理员权限的情况下连接到同一网络中的命名管道
- 无法为 X.radio 创建中等可视化,因为找不到网络节点可视化 - Omnet++
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- Protobuf网络串行化/反序列化c#与Linux c++
- Protobuf.网络使用