feat: preserve grpc write admission and uncertain outcomes
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
|
||||
// Internal fingerprint framing only. This is not a transport or persisted format.
|
||||
internal static class DataFingerprintEncoding
|
||||
{
|
||||
internal static void AppendByte(IncrementalHash hash, byte value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[1];
|
||||
scratch[0] = value;
|
||||
hash.AppendData(scratch);
|
||||
}
|
||||
|
||||
internal static void AppendInt32(IncrementalHash hash, int value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[4];
|
||||
BinaryPrimitives.WriteInt32LittleEndian(scratch, value);
|
||||
hash.AppendData(scratch);
|
||||
}
|
||||
|
||||
internal static void AppendInt64(IncrementalHash hash, long value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[8];
|
||||
BinaryPrimitives.WriteInt64LittleEndian(scratch, value);
|
||||
hash.AppendData(scratch);
|
||||
}
|
||||
|
||||
internal static void AppendString(IncrementalHash hash, string value)
|
||||
{
|
||||
AppendInt32(hash, value.Length);
|
||||
Span<byte> scratch = stackalloc byte[256];
|
||||
var offset = 0;
|
||||
while (offset < value.Length)
|
||||
{
|
||||
var chars = Math.Min(scratch.Length / 2, value.Length - offset);
|
||||
for (var i = 0; i < chars; i++)
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(scratch.Slice(i * 2, 2), value[offset + i]);
|
||||
hash.AppendData(scratch[..(chars * 2)]);
|
||||
offset += chars;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void AppendScalar(IncrementalHash hash, DataValueKind kind, object value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[16];
|
||||
var count = 0;
|
||||
switch (kind)
|
||||
{
|
||||
case DataValueKind.Boolean: scratch[0] = (bool)value ? (byte)1 : (byte)0; count = 1; break;
|
||||
case DataValueKind.Int8: scratch[0] = unchecked((byte)(sbyte)value); count = 1; break;
|
||||
case DataValueKind.UInt8: scratch[0] = (byte)value; count = 1; break;
|
||||
case DataValueKind.Int16: BinaryPrimitives.WriteInt16LittleEndian(scratch, (short)value); count = 2; break;
|
||||
case DataValueKind.UInt16: BinaryPrimitives.WriteUInt16LittleEndian(scratch, (ushort)value); count = 2; break;
|
||||
case DataValueKind.Int32: BinaryPrimitives.WriteInt32LittleEndian(scratch, (int)value); count = 4; break;
|
||||
case DataValueKind.UInt32: BinaryPrimitives.WriteUInt32LittleEndian(scratch, (uint)value); count = 4; break;
|
||||
case DataValueKind.Int64: BinaryPrimitives.WriteInt64LittleEndian(scratch, (long)value); count = 8; break;
|
||||
case DataValueKind.UInt64: BinaryPrimitives.WriteUInt64LittleEndian(scratch, (ulong)value); count = 8; break;
|
||||
case DataValueKind.Float: BinaryPrimitives.WriteInt32LittleEndian(scratch, BitConverter.SingleToInt32Bits((float)value)); count = 4; break;
|
||||
case DataValueKind.Double: BinaryPrimitives.WriteInt64LittleEndian(scratch, BitConverter.DoubleToInt64Bits((double)value)); count = 8; break;
|
||||
case DataValueKind.Timestamp: BinaryPrimitives.WriteInt64LittleEndian(scratch, ((DateTimeOffset)value).UtcTicks); count = 8; break;
|
||||
case DataValueKind.Guid: ((Guid)value).TryWriteBytes(scratch); count = 16; break;
|
||||
case DataValueKind.String: AppendString(hash, (string)value); return;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(kind));
|
||||
}
|
||||
hash.AppendData(scratch[..count]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user