mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
Guid to UUID
This commit is contained in:
@ -64,11 +64,17 @@ public class BinaryList
|
||||
}
|
||||
|
||||
|
||||
public BinaryList AddGuid(Guid value)
|
||||
public BinaryList AddUUID(UUID value)
|
||||
{
|
||||
list.AddRange(DC.ToBytes(value));
|
||||
list.AddRange(value.Data);
|
||||
return this;
|
||||
|
||||
}
|
||||
//public BinaryList AddGuid(Guid value)
|
||||
//{
|
||||
// list.AddRange(DC.ToBytes(value));
|
||||
// return this;
|
||||
//}
|
||||
|
||||
public BinaryList InsertGuid(int position, Guid value)
|
||||
{
|
||||
|
@ -808,11 +808,16 @@ public static class DC // Data Converter
|
||||
return ar.ToArray();
|
||||
}
|
||||
|
||||
public static Guid GetGuid(this byte[] data, uint offset)
|
||||
public static UUID GetUUID(this byte[] data, uint offset)
|
||||
{
|
||||
return new Guid(Clip(data, offset, 16));
|
||||
return new UUID(data, offset);
|
||||
}
|
||||
|
||||
//public static Guid GetGuid(this byte[] data, uint offset)
|
||||
//{
|
||||
// return new Guid(Clip(data, offset, 16));
|
||||
//}
|
||||
|
||||
public static DateTime GetDateTime(this byte[] data, uint offset, Endian endian)
|
||||
{
|
||||
var ticks = GetInt64(data, offset, endian);
|
||||
|
@ -158,12 +158,12 @@ public static class DataDeserializer
|
||||
|
||||
var reply = new AsyncReply<IRecord>();
|
||||
|
||||
var classId = data.GetGuid(offset);
|
||||
var classId = data.GetUUID(offset);
|
||||
offset += 16;
|
||||
length -= 16;
|
||||
|
||||
|
||||
var template = Warehouse.GetTemplateByClassId((Guid)classId, TemplateType.Record);
|
||||
var template = Warehouse.GetTemplateByClassId(classId, TemplateType.Record);
|
||||
|
||||
var initRecord = (TypeTemplate template) =>
|
||||
{
|
||||
@ -216,7 +216,7 @@ public static class DataDeserializer
|
||||
else if (connection != null)
|
||||
{
|
||||
// try to get the template from the other end
|
||||
connection.GetTemplate((Guid)classId).Then(tmp =>
|
||||
connection.GetTemplate(classId).Then(tmp =>
|
||||
{
|
||||
initRecord(tmp);
|
||||
}).Error(x => reply.TriggerError(x));
|
||||
@ -237,11 +237,11 @@ public static class DataDeserializer
|
||||
public static unsafe AsyncReply EnumParser(byte[] data, uint offset, uint length, DistributedConnection connection, uint[] requestSequence)
|
||||
{
|
||||
|
||||
var classId = data.GetGuid(offset);
|
||||
var classId = data.GetUUID(offset);
|
||||
offset += 16;
|
||||
var index = data[offset++];
|
||||
|
||||
var template = Warehouse.GetTemplateByClassId((Guid)classId, TemplateType.Enum);
|
||||
var template = Warehouse.GetTemplateByClassId(classId, TemplateType.Enum);
|
||||
|
||||
if (template != null)
|
||||
{
|
||||
@ -251,7 +251,7 @@ public static class DataDeserializer
|
||||
{
|
||||
var reply = new AsyncReply();
|
||||
|
||||
connection.GetTemplate((Guid)classId).Then(tmp =>
|
||||
connection.GetTemplate(classId).Then(tmp =>
|
||||
{
|
||||
reply.Trigger(tmp.Constants[index].Value);
|
||||
}).Error(x => reply.TriggerError(x));
|
||||
|
@ -128,7 +128,7 @@ public static class DataSerializer
|
||||
|
||||
|
||||
var rt = new List<byte>();
|
||||
rt.AddRange(template.ClassId.ToByteArray());
|
||||
rt.AddRange(template.ClassId.Data);
|
||||
rt.Add(ct.Index);
|
||||
|
||||
return (TransmissionTypeIdentifier.Enum, rt.ToArray());
|
||||
@ -366,7 +366,7 @@ public static class DataSerializer
|
||||
var template = Warehouse.GetTemplateByType(record.GetType());
|
||||
|
||||
|
||||
rt.AddRange(template.ClassId.ToByteArray());
|
||||
rt.AddRange(template.ClassId.Data);
|
||||
|
||||
foreach (var pt in template.Properties)
|
||||
{
|
||||
|
@ -177,9 +177,9 @@ namespace Esiur.Data
|
||||
(RepresentationTypeIdentifier.DateTime) => Nullable ? typeof(DateTime?) : typeof(DateTime),
|
||||
(RepresentationTypeIdentifier.Resource) => typeof(IResource),
|
||||
(RepresentationTypeIdentifier.Record) => typeof(IRecord),
|
||||
(RepresentationTypeIdentifier.TypedRecord) => Warehouse.GetTemplateByClassId((Guid)GUID!, TemplateType.Record)?.DefinedType,
|
||||
(RepresentationTypeIdentifier.TypedResource) => Warehouse.GetTemplateByClassId((Guid)GUID!, TemplateType.Resource)?.DefinedType,
|
||||
(RepresentationTypeIdentifier.Enum) => Warehouse.GetTemplateByClassId((Guid)GUID!, TemplateType.Enum)?.DefinedType,
|
||||
(RepresentationTypeIdentifier.TypedRecord) => Warehouse.GetTemplateByClassId((UUID)UUID!, TemplateType.Record)?.DefinedType,
|
||||
(RepresentationTypeIdentifier.TypedResource) => Warehouse.GetTemplateByClassId((UUID)UUID!, TemplateType.Resource)?.DefinedType,
|
||||
(RepresentationTypeIdentifier.Enum) => Warehouse.GetTemplateByClassId((UUID)UUID!, TemplateType.Enum)?.DefinedType,
|
||||
|
||||
_ => null
|
||||
};
|
||||
@ -187,7 +187,7 @@ namespace Esiur.Data
|
||||
|
||||
public RepresentationTypeIdentifier Identifier;
|
||||
public bool Nullable;
|
||||
public Guid? GUID;
|
||||
public UUID? UUID;
|
||||
//public RepresentationType? SubType1; // List + Map
|
||||
//public RepresentationType? SubType2; // Map
|
||||
//public RepresentationType? SubType3; // No types yet
|
||||
@ -197,7 +197,7 @@ namespace Esiur.Data
|
||||
|
||||
public RepresentationType ToNullable()
|
||||
{
|
||||
return new RepresentationType(Identifier, true, GUID, SubTypes);
|
||||
return new RepresentationType(Identifier, true, UUID, SubTypes);
|
||||
}
|
||||
|
||||
public static RepresentationType? FromType(Type type)
|
||||
@ -226,7 +226,7 @@ namespace Esiur.Data
|
||||
return new RepresentationType(
|
||||
RepresentationTypeIdentifier.TypedResource,
|
||||
nullable,
|
||||
TypeTemplate.GetTypeGuid(type)
|
||||
TypeTemplate.GetTypeUUID(type)
|
||||
);
|
||||
}
|
||||
else if (Codec.ImplementsInterface(type, typeof(IRecord)))
|
||||
@ -234,7 +234,7 @@ namespace Esiur.Data
|
||||
return new RepresentationType(
|
||||
RepresentationTypeIdentifier.TypedRecord,
|
||||
nullable,
|
||||
TypeTemplate.GetTypeGuid(type)
|
||||
TypeTemplate.GetTypeUUID(type)
|
||||
);
|
||||
}
|
||||
else if (type.IsGenericType)
|
||||
@ -396,7 +396,7 @@ namespace Esiur.Data
|
||||
}
|
||||
else if (type.IsEnum)
|
||||
{
|
||||
return new RepresentationType(RepresentationTypeIdentifier.Enum, nullable, TypeTemplate.GetTypeGuid(type));
|
||||
return new RepresentationType(RepresentationTypeIdentifier.Enum, nullable, TypeTemplate.GetTypeUUID(type));
|
||||
}
|
||||
//else if (typeof(Structure).IsAssignableFrom(t) || t == typeof(ExpandoObject) => RepresentationTypeIdentifier.Structure)
|
||||
//{
|
||||
@ -427,11 +427,11 @@ namespace Esiur.Data
|
||||
|
||||
}
|
||||
|
||||
public RepresentationType(RepresentationTypeIdentifier identifier, bool nullable, Guid? guid = null, RepresentationType[]? subTypes = null)
|
||||
public RepresentationType(RepresentationTypeIdentifier identifier, bool nullable, UUID? uuid = null, RepresentationType[]? subTypes = null)
|
||||
{
|
||||
Nullable = nullable;
|
||||
Identifier = identifier;
|
||||
GUID = guid;
|
||||
UUID = uuid;
|
||||
SubTypes = subTypes;
|
||||
}
|
||||
|
||||
@ -444,8 +444,8 @@ namespace Esiur.Data
|
||||
else
|
||||
rt.AddUInt8((byte)Identifier);
|
||||
|
||||
if (GUID != null)
|
||||
rt.AddUInt8Array(DC.ToBytes((Guid)GUID));
|
||||
if (UUID != null)
|
||||
rt.AddUInt8Array(UUID.Value.Data);
|
||||
|
||||
if (SubTypes != null)
|
||||
for (var i = 0; i < SubTypes.Length; i++)
|
||||
@ -471,14 +471,14 @@ namespace Esiur.Data
|
||||
if ((header & 0x40) > 0)
|
||||
{
|
||||
|
||||
var hasGUID = (header & 0x4) > 0;
|
||||
var hasUUID = (header & 0x4) > 0;
|
||||
var subsCount = (header >> 3) & 0x7;
|
||||
|
||||
Guid? guid = null;
|
||||
UUID? uuid = null;
|
||||
|
||||
if (hasGUID)
|
||||
if (hasUUID)
|
||||
{
|
||||
guid = data.GetGuid(offset);
|
||||
uuid = data.GetUUID(offset);
|
||||
offset += 16;
|
||||
}
|
||||
|
||||
@ -490,7 +490,7 @@ namespace Esiur.Data
|
||||
offset += len;
|
||||
}
|
||||
|
||||
return (offset - oOffset, new RepresentationType(identifier, nullable, guid, subs));
|
||||
return (offset - oOffset, new RepresentationType(identifier, nullable, uuid, subs));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,65 +1,113 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.FlowAnalysis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Data
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public unsafe struct UUID
|
||||
//[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public struct UUID
|
||||
{
|
||||
//4e7db2d8-a785-1b99-1854-4b4018bc5677
|
||||
byte a1;
|
||||
byte a2;
|
||||
byte a3;
|
||||
byte a4;
|
||||
byte b1;
|
||||
byte b2;
|
||||
byte c1;
|
||||
byte c2;
|
||||
byte d1;
|
||||
byte d2;
|
||||
byte e1;
|
||||
byte e2;
|
||||
byte e3;
|
||||
byte e4;
|
||||
byte e5;
|
||||
byte e6;
|
||||
//byte a1;
|
||||
//byte a2;
|
||||
//byte a3;
|
||||
//byte a4;
|
||||
//byte b1;
|
||||
//byte b2;
|
||||
//byte c1;
|
||||
//byte c2;
|
||||
//byte d1;
|
||||
//byte d2;
|
||||
//byte e1;
|
||||
//byte e2;
|
||||
//byte e3;
|
||||
//byte e4;
|
||||
//byte e5;
|
||||
//byte e6;
|
||||
|
||||
public byte[] Data { get; private set; }
|
||||
|
||||
|
||||
public UUID(byte[] data)
|
||||
public UUID(byte[] data, uint offset)
|
||||
{
|
||||
if (data.Length < 16)
|
||||
if (offset + 16 < data.Length)
|
||||
throw new Exception("UUID data size must be at least 16 bytes");
|
||||
|
||||
for(var i = 0; i < 16; i++)
|
||||
Data[i] = data[i];
|
||||
Data = DC.Clip(data, offset, 16);
|
||||
|
||||
//a1 = data[offset++];
|
||||
//a2 = data[offset++];
|
||||
//a3 = data[offset++];
|
||||
//a4 = data[offset++];
|
||||
//b1 = data[offset++];
|
||||
//b2 = data[offset++];
|
||||
//c1 = data[offset++];
|
||||
//c2 = data[offset++];
|
||||
//d1 = data[offset++];
|
||||
//d2 = data[offset++];
|
||||
//e1 = data[offset++];
|
||||
//e2 = data[offset++];
|
||||
//e3 = data[offset++];
|
||||
//e4 = data[offset++];
|
||||
//e5 = data[offset++];
|
||||
//e6 = data[offset++];
|
||||
}
|
||||
|
||||
public UUID(byte[] data) {
|
||||
|
||||
if (data.Length != 16)
|
||||
throw new Exception("UUID data size must be 16 bytes");
|
||||
|
||||
Data = data;
|
||||
//a1 = data[0];
|
||||
//a2 = data[1];
|
||||
//a3 = data[2];
|
||||
//a4 = data[3];
|
||||
//b1 = data[4];
|
||||
//b2 = data[5];
|
||||
//c1 = data[6];
|
||||
//c2 = data[7];
|
||||
//d1 = data[8];
|
||||
//d2 = data[9];
|
||||
//e1 = data[10];
|
||||
//e2 = data[11];
|
||||
//e3 = data[12];
|
||||
//e4 = data[13];
|
||||
//e5 = data[14];
|
||||
//e6 = data[15];
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{a1.ToString("x2")}{a2.ToString("x2")}{a3.ToString("x2")}{a4.ToString("x2")}-{b1.ToString("x2")}{b2.ToString("x2")}-{c1.ToString("x2")}{c2.ToString("x2")}-{d1.ToString("x2")}{d2.ToString("x2")}-{e1.ToString("x2")}{e2.ToString("x2")}{e3.ToString("x2")}{e4.ToString("x2")}{e5.ToString("x2")}{e6.ToString("x2")}";
|
||||
|
||||
return $"{DC.ToHex(Data, 0, 4, null)}-{DC.ToHex(Data, 4, 2, null)}-{DC.ToHex(Data, 6, 2, null)}-{DC.ToHex(Data, 8, 2, null)}-{DC.ToHex(Data, 10, 6, null)}";
|
||||
|
||||
//return $"{a1.ToString("x2")}{a2.ToString("x2")}{a3.ToString("x2")}{a4.ToString("x2")}-{b1.ToString("x2")}{b2.ToString("x2")}-{c1.ToString("x2")}{c2.ToString("x2")}-{d1.ToString("x2")}{d2.ToString("x2")}-{e1.ToString("x2")}{e2.ToString("x2")}{e3.ToString("x2")}{e4.ToString("x2")}{e5.ToString("x2")}{e6.ToString("x2")}";
|
||||
}
|
||||
|
||||
public static bool operator == (UUID a, UUID b)
|
||||
{
|
||||
return a.a1 == b.a1
|
||||
&& a.a2 == b.a2
|
||||
&& a.a3 == b.a3
|
||||
&& a.a4 == b.a4
|
||||
&& a.b1 == b.b1
|
||||
&& a.b2 == b.b2
|
||||
&& a.c1 == b.c1
|
||||
&& a.c2 == b.c2
|
||||
&& a.d1 == b.d1
|
||||
&& a.d2 == b.d2
|
||||
&& a.e1 == b.e1
|
||||
&& a.e2 == b.e2
|
||||
&& a.e3 == b.e3
|
||||
&& a.e4 == b.e4
|
||||
&& a.e5 == b.e5
|
||||
&& a.e6 == b.e6;
|
||||
return a.Data.SequenceEqual(b.Data);
|
||||
|
||||
//return a.a1 == b.a1
|
||||
// && a.a2 == b.a2
|
||||
// && a.a3 == b.a3
|
||||
// && a.a4 == b.a4
|
||||
// && a.b1 == b.b1
|
||||
// && a.b2 == b.b2
|
||||
// && a.c1 == b.c1
|
||||
// && a.c2 == b.c2
|
||||
// && a.d1 == b.d1
|
||||
// && a.d2 == b.d2
|
||||
// && a.e1 == b.e1
|
||||
// && a.e2 == b.e2
|
||||
// && a.e3 == b.e3
|
||||
// && a.e4 == b.e4
|
||||
// && a.e5 == b.e5
|
||||
// && a.e6 == b.e6;
|
||||
}
|
||||
|
||||
public static bool operator !=(UUID a, UUID b)
|
||||
|
Reference in New Issue
Block a user