【问题标题】:How to define and (de)serialize an IPAddress in protobuf / C#?如何在 protobuf / C# 中定义和(反)序列化 IPAddress?
【发布时间】:2021-01-13 23:53:52
【问题描述】:
public class Example
{
    public DateTime Date { get; set; }
    public IPAddress Ip { get; set; }
    public string ExampleDescription { get; set; }
}

还有.proto:

syntax = "proto3";
package example;

import "google/protobuf/timestamp.proto";

message Register {
    google.protobuf.Timestamp Date = 1;
    ??? Ip = 2;
    string ExampleDescription = 3;
}

我想使用 Protobuf 通过 UDP 从客户端 服务器发送上述示例消息,但我不确定如何正确处理 IPAddress。我可以将它序列化为字符串(这可能会改善与其他语言的互操作性),但我也看到了 .proto 之类的:

oneof ip_addr {
    fixed32 v4 = 1;
    bytes v6 = 2;
}

我宁愿保持我的(上面的“示例”)类完好无损。我还需要同时支持 IPv4 和 IPv6。我更喜欢小型、优化的消息和(反)序列化,因为我每秒处理数千个,我希望这个数字在未来会上升。

无论哪种方式((反)序列化为字符串或使用oneof 方法),我都需要为此实现我自己的自定义(反)序列化器吗?最好的方法是什么?或者有人有关于如何在 C# 中实现它的好的教程/文档的指针吗?

将来我可能还想使用IpEndpoint;我可能会重新使用“ip proto”并在上面贴上int32 Port?

【问题讨论】:

  • 它使用 http 连接。 HTTP 可以使用机器名 https:\\servername 或 IP https\\IP。请参阅:docs.microsoft.com/en-us/aspnet/core/grpc/…
  • @jdweng 你确定你在回答正确的问题吗?这是关于序列化 IPAddress 类型,而不是 gRPC

标签: c# protocol-buffers ip-address


【解决方案1】:

编辑:随着 v3 中的 API 添加,现在有 3 种不同的方式来实现这一点;他们都记录在案here。


  • 如果您使用的是 Google 序列化程序;那是行不通的——你需要一个专用的 POCO 来使用 protobuf 原语进行序列化——大概是string。
  • 如果你使用protobuf-net,AllowParseableTypes是你的朋友;见this example。为了方便您找到,我也将其粘贴在这里。
using ProtoBuf.Meta;
using System;
using System.Net;
using Xunit;
using Xunit.Abstractions;

namespace ProtoBuf.Issues
{
    public class SO64101495
    {
        public SO64101495(ITestOutputHelper log)
            => Log = log;
        private readonly ITestOutputHelper Log;
        [Fact]
        public void RoundTripIPAddess()
        {
            var model = RuntimeTypeModel.Create();
            model.AllowParseableTypes = true;

            var schema = model.GetSchema(typeof(Example));
            Log?.WriteLine(schema);
            Assert.Equal(@"syntax = ""proto3"";
package ProtoBuf.Issues;
import ""google/protobuf/timestamp.proto"";
message Example {
   .google.protobuf.Timestamp Date = 1;
   string Ip = 2;
   string ExampleDescription = 3;
}
", schema);
        }



        [ProtoContract]
        [CompatibilityLevel(CompatibilityLevel.Level300)] // use timestamp.proto for Date
        public class Example
        {
            [ProtoMember(1)]
            public DateTime Date { get; set; }
            [ProtoMember(2)]
            public IPAddress Ip { get; set; }
            [ProtoMember(3)]
            public string ExampleDescription { get; set; }
        }
    }
}

Ver3 中还有一个即将发生的变化,应该会使其更具可扩展性。
这个新功能可能与您的oneof ip_addr 方法很好地配合使用,但它需要一个虚拟包装器类型。如果你感兴趣,我可以进一步扩展这个想法。

【讨论】:

  • 谢谢马克!我在哪里可以找到关于 AllowParseableTypes 的文档以及它是如何工作的?如果我要序列化为 Json(例如使用 System.Text.Json),我将实现 JsonConverter<IPAddress> 并使用 JsonSerializerOptions 将其传递给序列化程序;理想情况下,这种方法(或类似方法)将用于 protobuf-net。我很想了解更多,所以如果您想扩展:请这样做!
  • 嗯,我刚刚了解了“代理序列化”,也许这更接近我的预期。在我们讲话时对此进行调查;也许它很合适,也许不是。尚未确定。另外 - 回答你的第一个问题 - 我 am 使用 Google 序列化程序,但愿意切换到 protobuf-net。
  • @RobIII AllowParseableTypes 只是查找具有静态 Parse 方法的类型,使用该方法和 ToString() - 这非常简单;代理方法可能更相关 - 它允许使用您自己的转换逻辑注册复杂类型。请注意,在 v3 中使这个 API 更友好的更改还没有完全“成熟”,这就是为什么上面的提交是在一个分支中,而不是在主干/主干中,但没有什么能真正阻止我合并它。奇怪的是,当我尝试在这里使用代理 API(而不是全局标志)时,它并不喜欢它——我会找到那是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多