1
0

Updated to C# 12

This commit is contained in:
2024-01-14 13:10:33 +01:00
parent 9cd1344266
commit 27cd54fb30
51 changed files with 637 additions and 379 deletions

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using AMWD.Common.MessagePack.Utilities;
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPAddress"/> array to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPAddressArrayFormatter : IMessagePackFormatter<IPAddress[]>
{
/// <inheritdoc/>
public IPAddress[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return null;
int bytePos = 0;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
byte[] buffer = bytes.Skip(bytePos).Take(sizeof(int)).ToArray();
bytePos += sizeof(int);
NetworkHelper.SwapBigEndian(buffer);
int length = BitConverter.ToInt32(buffer, 0);
int arrayPos = 0;
var array = new IPAddress[length];
while (bytePos < bytes.Length)
{
byte len = bytes.Skip(bytePos).First();
bytePos++;
buffer = bytes.Skip(bytePos).Take(len).ToArray();
bytePos += len;
array[arrayPos] = new IPAddress(buffer);
arrayPos++;
}
return array;
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, IPAddress[] value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
var bytes = new List<byte>();
int length = value.Length;
byte[] buffer = BitConverter.GetBytes(length);
NetworkHelper.SwapBigEndian(buffer);
bytes.AddRange(buffer);
foreach (var ip in value)
{
buffer = ip.GetAddressBytes();
bytes.Add((byte)buffer.Length);
bytes.AddRange(buffer);
}
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, [.. bytes], options);
}
}
}

View File

@@ -0,0 +1,34 @@
using System.Net;
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPAddress"/> to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPAddressFormatter : IMessagePackFormatter<IPAddress>
{
/// <inheritdoc/>
public IPAddress Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return null;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
return new IPAddress(bytes);
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, IPAddress value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
byte[] bytes = value.GetAddressBytes();
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes, options);
}
}
}

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using AMWD.Common.MessagePack.Utilities;
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPAddress"/> list to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPAddressListFormatter : IMessagePackFormatter<List<IPAddress>>
{
/// <inheritdoc/>
public List<IPAddress> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return null;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
// skipping the length information
int bytePos = sizeof(int);
var list = new List<IPAddress>();
while (bytePos < bytes.Length)
{
byte len = bytes.Skip(bytePos).First();
bytePos++;
byte[] buffer = bytes.Skip(bytePos).Take(len).ToArray();
bytePos += len;
list.Add(new IPAddress(buffer));
}
return list;
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, List<IPAddress> value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
var bytes = new List<byte>();
int length = value.Count;
byte[] buffer = BitConverter.GetBytes(length);
NetworkHelper.SwapBigEndian(buffer);
bytes.AddRange(buffer);
foreach (var ip in value)
{
buffer = ip.GetAddressBytes();
bytes.Add((byte)buffer.Length);
bytes.AddRange(buffer);
}
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, [.. bytes], options);
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AMWD.Common.MessagePack.Utilities;
#if NET8_0_OR_GREATER
using IPNetwork = System.Net.IPNetwork;
#else
using Microsoft.AspNetCore.HttpOverrides;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
#endif
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPNetwork"/> array to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPNetworkArrayFormatter : IMessagePackFormatter<IPNetwork[]>
{
/// <inheritdoc/>
public IPNetwork[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return null;
int bytePos = 0;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
byte[] buffer = bytes.Skip(bytePos).Take(sizeof(int)).ToArray();
bytePos += sizeof(int);
NetworkHelper.SwapBigEndian(buffer);
int length = BitConverter.ToInt32(buffer, 0);
int arrayPos = 0;
var array = new IPNetwork[length];
while (bytePos < bytes.Length)
{
byte len = bytes.Skip(bytePos).First();
bytePos++;
buffer = bytes.Skip(bytePos).Take(len).ToArray();
bytePos += len;
array[arrayPos] = IPNetworkFormatter.DeserializeInternal(buffer);
arrayPos++;
}
return array;
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, IPNetwork[] value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
var bytes = new List<byte>();
int length = value.Length;
byte[] buffer = BitConverter.GetBytes(length);
NetworkHelper.SwapBigEndian(buffer);
bytes.AddRange(buffer);
foreach (var network in value)
{
buffer = IPNetworkFormatter.SerializeInternal(network);
bytes.Add((byte)buffer.Length);
bytes.AddRange(buffer);
}
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes.ToArray(), options);
}
}
}

View File

@@ -0,0 +1,68 @@
using System;
using System.Linq;
using System.Net;
#if NET8_0_OR_GREATER
using IPNetwork = System.Net.IPNetwork;
#else
using Microsoft.AspNetCore.HttpOverrides;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
#endif
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPNetwork"/> to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPNetworkFormatter : IMessagePackFormatter<IPNetwork>
{
/// <inheritdoc/>
public IPNetwork Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return default;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
return DeserializeInternal(bytes);
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, IPNetwork value, MessagePackSerializerOptions options)
{
if (value == default)
{
writer.WriteNil();
return;
}
byte[] bytes = SerializeInternal(value);
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes, options);
}
internal static byte[] SerializeInternal(IPNetwork network)
{
// IP network prefix has a maximum of 128 bit - therefore the length can be covered with a byte.
byte prefixLength = (byte)network.PrefixLength;
#if NET8_0_OR_GREATER
byte[] prefixBytes = network.BaseAddress.GetAddressBytes();
#else
byte[] prefixBytes = network.Prefix.GetAddressBytes();
#endif
byte[] bytes = new byte[prefixBytes.Length + 1];
bytes[0] = prefixLength;
Array.Copy(prefixBytes, 0, bytes, 1, prefixBytes.Length);
return bytes;
}
internal static IPNetwork DeserializeInternal(byte[] bytes)
{
byte prefixLength = bytes[0];
byte[] prefixBytes = bytes.Skip(1).ToArray();
var prefix = new IPAddress(prefixBytes);
return new IPNetwork(prefix, prefixLength);
}
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AMWD.Common.MessagePack.Utilities;
#if NET8_0_OR_GREATER
using IPNetwork = System.Net.IPNetwork;
#else
using Microsoft.AspNetCore.HttpOverrides;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
#endif
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPNetwork"/> list to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPNetworkListFormatter : IMessagePackFormatter<List<IPNetwork>>
{
/// <inheritdoc/>
public List<IPNetwork> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return null;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
// skipping the length information
int bytePos = sizeof(int);
var list = new List<IPNetwork>();
while (bytePos < bytes.Length)
{
byte len = bytes.Skip(bytePos).First();
bytePos++;
byte[] buffer = bytes.Skip(bytePos).Take(len).ToArray();
bytePos += len;
list.Add(IPNetworkFormatter.DeserializeInternal(buffer));
}
return list;
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, List<IPNetwork> value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
var bytes = new List<byte>();
int length = value.Count;
byte[] buffer = BitConverter.GetBytes(length);
NetworkHelper.SwapBigEndian(buffer);
bytes.AddRange(buffer);
foreach (var network in value)
{
buffer = IPNetworkFormatter.SerializeInternal(network);
bytes.Add((byte)buffer.Length);
bytes.AddRange(buffer);
}
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes.ToArray(), options);
}
}
}