So we can to configure pubsub routes by hostname or ipv4 (ipv6 hasn't been tested yet...so we should assume it doesn't work).
40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
namespace Bts.Net;
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
/// <summary>
|
|
/// This class seriailzes IpHostEntry to json (string). A common buffer is used to reduce memory
|
|
/// allocations on repeated calls to <see cref="Serialize"/>. For that reason, this class is NOT
|
|
/// thread safe.
|
|
/// </summary>
|
|
public class IpHostEntryJsonSerializer {
|
|
readonly MemoryStream buffer = new();
|
|
readonly IpHostEntryJsonConverter converter;
|
|
|
|
public IpHostEntryJsonSerializer(IpHostEntryJsonConverter? converter = null) =>
|
|
this.converter = converter ?? new() { };
|
|
|
|
public string? Serialize(IPHostEntry dp, JsonSerializerOptions? opts = null) {
|
|
opts ??= JsonSerializerOptions.Default;
|
|
buffer.Position = 0;
|
|
buffer.SetLength(0);
|
|
using (var jsonWriter = new Utf8JsonWriter(buffer)) {
|
|
converter.Write(jsonWriter, dp, opts);
|
|
}
|
|
var bytes = buffer.GetBuffer();
|
|
var span = new ArraySegment<byte>(bytes, 0, (int)buffer.Length);
|
|
return Encoding.UTF8.GetString(span);
|
|
}
|
|
|
|
public void Serialize(IPHostEntry dp, Utf8JsonWriter jsonWriter, JsonSerializerOptions? opts = null) {
|
|
opts ??= JsonSerializerOptions.Default;
|
|
buffer.Position = 0;
|
|
buffer.SetLength(0);
|
|
converter.Write(jsonWriter, dp, opts);
|
|
}
|
|
}
|