From 276e7b17fde8b6da7e31f95ad1ea67c2f071b928 Mon Sep 17 00:00:00 2001 From: ahmed Date: Mon, 3 Mar 2025 04:48:53 +0300 Subject: [PATCH] Guid to UUID --- Esiur/Data/BinaryList.cs | 10 +- Esiur/Data/DataConverter.cs | 9 +- Esiur/Data/DataDeserializer.cs | 12 +- Esiur/Data/DataSerializer.cs | 4 +- Esiur/Data/RepresentationType.cs | 34 ++--- Esiur/Data/UUID.cs | 126 ++++++++++++------ .../Net/IIP/DistributedConnectionProtocol.cs | 24 ++-- Esiur/Net/Packets/IIPPacket.cs | 8 +- Esiur/Net/Packets/IIPPacketAttachInfo.cs | 7 +- Esiur/Proxy/TemplateGenerator.cs | 12 +- Esiur/Resource/ClassIdAttribute.cs | 4 +- Esiur/Resource/Template/TypeTemplate.cs | 24 ++-- Esiur/Resource/Warehouse.cs | 12 +- Test/Program.cs | 30 +++-- 14 files changed, 195 insertions(+), 121 deletions(-) diff --git a/Esiur/Data/BinaryList.cs b/Esiur/Data/BinaryList.cs index 55d2593..397df1a 100644 --- a/Esiur/Data/BinaryList.cs +++ b/Esiur/Data/BinaryList.cs @@ -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) { diff --git a/Esiur/Data/DataConverter.cs b/Esiur/Data/DataConverter.cs index dafc83f..9f7e884 100644 --- a/Esiur/Data/DataConverter.cs +++ b/Esiur/Data/DataConverter.cs @@ -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); diff --git a/Esiur/Data/DataDeserializer.cs b/Esiur/Data/DataDeserializer.cs index 1805125..b702a5c 100644 --- a/Esiur/Data/DataDeserializer.cs +++ b/Esiur/Data/DataDeserializer.cs @@ -158,12 +158,12 @@ public static class DataDeserializer var reply = new AsyncReply(); - 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)); diff --git a/Esiur/Data/DataSerializer.cs b/Esiur/Data/DataSerializer.cs index 7870bbc..2831db4 100644 --- a/Esiur/Data/DataSerializer.cs +++ b/Esiur/Data/DataSerializer.cs @@ -128,7 +128,7 @@ public static class DataSerializer var rt = new List(); - 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) { diff --git a/Esiur/Data/RepresentationType.cs b/Esiur/Data/RepresentationType.cs index b283b1d..e8e0446 100644 --- a/Esiur/Data/RepresentationType.cs +++ b/Esiur/Data/RepresentationType.cs @@ -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 { diff --git a/Esiur/Data/UUID.cs b/Esiur/Data/UUID.cs index ee3dda9..0b75029 100644 --- a/Esiur/Data/UUID.cs +++ b/Esiur/Data/UUID.cs @@ -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) diff --git a/Esiur/Net/IIP/DistributedConnectionProtocol.cs b/Esiur/Net/IIP/DistributedConnectionProtocol.cs index 2357c34..ae820b4 100644 --- a/Esiur/Net/IIP/DistributedConnectionProtocol.cs +++ b/Esiur/Net/IIP/DistributedConnectionProtocol.cs @@ -48,12 +48,12 @@ partial class DistributedConnection KeyList> suspendedResources = new KeyList>(); KeyList resourceRequests = new KeyList(); - KeyList> templateRequests = new KeyList>(); + KeyList> templateRequests = new KeyList>(); KeyList> templateByNameRequests = new KeyList>(); - Dictionary templates = new Dictionary(); + Dictionary templates = new Dictionary(); KeyList requests = new KeyList(); @@ -143,7 +143,7 @@ partial class DistributedConnection } - public AsyncReply StaticCall(Guid classId, byte index, Map parameters) + public AsyncReply StaticCall(UUID classId, byte index, Map parameters) { var pb = Codec.Compose(parameters, this);// Codec.ComposeVarArray(parameters, this, true); @@ -154,7 +154,7 @@ partial class DistributedConnection SendParams().AddUInt8((byte)(0x40 | (byte)IIPPacketAction.StaticCall)) .AddUInt32(c) - .AddGuid(classId) + .AddUUID(classId) .AddUInt8(index) .AddUInt8Array(pb) .Done(); @@ -577,7 +577,7 @@ partial class DistributedConnection { // reply ok SendReply(IIPPacketAction.AttachResource, callback) - .AddGuid(r.Instance.Template.ClassId) + .AddUUID(r.Instance.Template.ClassId) .AddUInt64(r.Instance.Age) .AddUInt16((ushort)link.Length) .AddUInt8Array(link) @@ -589,7 +589,7 @@ partial class DistributedConnection { // reply ok SendReply(IIPPacketAction.AttachResource, callback) - .AddGuid(r.Instance.Template.ClassId) + .AddUUID(r.Instance.Template.ClassId) .AddUInt64(r.Instance.Age) .AddUInt16((ushort)link.Length) .AddUInt8Array(link) @@ -1207,7 +1207,7 @@ partial class DistributedConnection } } - void IIPRequestTemplateFromClassId(uint callback, Guid classId) + void IIPRequestTemplateFromClassId(uint callback, UUID classId) { var t = Warehouse.GetTemplateByClassId(classId); @@ -1334,7 +1334,7 @@ partial class DistributedConnection }); } - void IIPRequestStaticCall(uint callback, Guid classId, byte index, TransmissionType transmissionType, byte[] content) + void IIPRequestStaticCall(uint callback, UUID classId, byte index, TransmissionType transmissionType, byte[] content) { var template = Warehouse.GetTemplateByClassId(classId); @@ -2097,7 +2097,7 @@ partial class DistributedConnection /// /// Class GUID. /// ResourceTemplate. - public AsyncReply GetTemplate(Guid classId) + public AsyncReply GetTemplate(UUID classId) { if (templates.ContainsKey(classId)) return new AsyncReply(templates[classId]); @@ -2108,7 +2108,7 @@ partial class DistributedConnection templateRequests.Add(classId, reply); SendRequest(IIPPacketAction.TemplateFromClassId) - .AddGuid(classId) + .AddUUID(classId) .Done() .Then((rt) => { @@ -2327,7 +2327,7 @@ partial class DistributedConnection DistributedResource dr; TypeTemplate template = null; - Guid classId = (Guid)rt[0]; + UUID classId = (UUID)rt[0]; if (resource == null) { @@ -2370,7 +2370,7 @@ partial class DistributedConnection if (template == null) { - GetTemplate((Guid)rt[0]).Then((tmp) => + GetTemplate((UUID)rt[0]).Then((tmp) => { // ClassId, ResourceAge, ResourceLink, Content if (resource == null) diff --git a/Esiur/Net/Packets/IIPPacket.cs b/Esiur/Net/Packets/IIPPacket.cs index 4e26bf3..6a6dfb8 100644 --- a/Esiur/Net/Packets/IIPPacket.cs +++ b/Esiur/Net/Packets/IIPPacket.cs @@ -119,7 +119,7 @@ class IIPPacket : Packet public string ResourceLink { get; set; } public string ResourceName { get; set; } - public Guid ClassId { get; set; } + public UUID ClassId { get; set; } public byte MethodIndex { get; set; } public string MethodName { get; set; } public uint CallbackId { get; set; } @@ -403,7 +403,7 @@ class IIPPacket : Packet if (NotEnough(offset, ends, 16)) return -dataLengthNeeded; - ClassId = data.GetGuid(offset); + ClassId = data.GetUUID(offset); offset += 16; } @@ -592,7 +592,7 @@ class IIPPacket : Packet if (NotEnough(offset, ends, 18)) return -dataLengthNeeded; - ClassId = data.GetGuid(offset);//, Endian.Little); + ClassId = data.GetUUID(offset);//, Endian.Little); offset += 16; MethodIndex = data[offset++]; @@ -615,7 +615,7 @@ class IIPPacket : Packet if (NotEnough(offset, ends, 26)) return -dataLengthNeeded; - ClassId = data.GetGuid(offset); + ClassId = data.GetUUID(offset); offset += 16; ResourceAge = data.GetUInt64(offset, Endian.Little); diff --git a/Esiur/Net/Packets/IIPPacketAttachInfo.cs b/Esiur/Net/Packets/IIPPacketAttachInfo.cs index 8b7d95e..d2a7770 100644 --- a/Esiur/Net/Packets/IIPPacketAttachInfo.cs +++ b/Esiur/Net/Packets/IIPPacketAttachInfo.cs @@ -1,4 +1,5 @@ -using System; +using Esiur.Data; +using System; using System.Collections.Generic; using System.Text; @@ -9,9 +10,9 @@ struct IIPPacketAttachInfo public string Link; public ulong Age; public byte[] Content; - public Guid ClassId; + public UUID ClassId; - public IIPPacketAttachInfo(Guid classId, ulong age, string link, byte[] content) + public IIPPacketAttachInfo(UUID classId, ulong age, string link, byte[] content) { ClassId = classId; Age = age; diff --git a/Esiur/Proxy/TemplateGenerator.cs b/Esiur/Proxy/TemplateGenerator.cs index 20fe6f9..a366f09 100644 --- a/Esiur/Proxy/TemplateGenerator.cs +++ b/Esiur/Proxy/TemplateGenerator.cs @@ -77,7 +77,7 @@ public static class TemplateGenerator rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); - rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]"); + rt.AppendLine($"[ClassId(\"{template.ClassId.Data.ToHex(0, 16, null)}\")]"); rt.AppendLine($"[Export] public class {className} : IRecord {{"); @@ -110,7 +110,7 @@ public static class TemplateGenerator if (template.Annotation != null) rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); - rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]"); + rt.AppendLine($"[ClassId(\"{template.ClassId.Data.ToHex(0, 16, null)}\")]"); rt.AppendLine($"[Export] public enum {className} {{"); rt.AppendLine(String.Join(",\r\n", template.Constants.Select(x => $"{x.Name}={x.Value}"))); @@ -126,11 +126,11 @@ public static class TemplateGenerator string name; if (representationType.Identifier == RepresentationTypeIdentifier.TypedResource)// == DataType.Resource) - name = templates.First(x => x.ClassId == representationType.GUID && (x.Type == TemplateType.Resource)).ClassName; + name = templates.First(x => x.ClassId == representationType.UUID && (x.Type == TemplateType.Resource)).ClassName; else if (representationType.Identifier == RepresentationTypeIdentifier.TypedRecord) - name = templates.First(x => x.ClassId == representationType.GUID && x.Type == TemplateType.Record).ClassName; + name = templates.First(x => x.ClassId == representationType.UUID && x.Type == TemplateType.Record).ClassName; else if (representationType.Identifier == RepresentationTypeIdentifier.Enum) - name = templates.First(x => x.ClassId == representationType.GUID && x.Type == TemplateType.Enum).ClassName; + name = templates.First(x => x.ClassId == representationType.UUID && x.Type == TemplateType.Enum).ClassName; else if (representationType.Identifier == RepresentationTypeIdentifier.TypedList) name = GetTypeName(representationType.SubTypes[0], templates) + "[]"; else if (representationType.Identifier == RepresentationTypeIdentifier.TypedMap) @@ -278,7 +278,7 @@ public static class TemplateGenerator rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); - rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]"); + rt.AppendLine($"[ClassId(\"{template.ClassId.Data.ToHex(0, 16, null)}\")]"); // extends if (template.ParentId == null) diff --git a/Esiur/Resource/ClassIdAttribute.cs b/Esiur/Resource/ClassIdAttribute.cs index 2acb685..86779e8 100644 --- a/Esiur/Resource/ClassIdAttribute.cs +++ b/Esiur/Resource/ClassIdAttribute.cs @@ -8,12 +8,12 @@ namespace Esiur.Resource [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum)] public class ClassIdAttribute : Attribute { - public Guid ClassId { get; private set; } + public UUID ClassId { get; private set; } public ClassIdAttribute(string classId) { var data = DC.FromHex(classId, null); - ClassId = new Guid(data); + ClassId = new UUID(data); } } } diff --git a/Esiur/Resource/Template/TypeTemplate.cs b/Esiur/Resource/Template/TypeTemplate.cs index 82b2e4f..f450a0d 100644 --- a/Esiur/Resource/Template/TypeTemplate.cs +++ b/Esiur/Resource/Template/TypeTemplate.cs @@ -22,8 +22,8 @@ namespace Esiur.Resource.Template; public class TypeTemplate { - protected Guid classId; - protected Guid? parentId; + protected UUID classId; + protected UUID? parentId; public string Annotation { get; set; } @@ -43,7 +43,7 @@ public class TypeTemplate protected byte[] content; - public Guid? ParentId => parentId; + public UUID? ParentId => parentId; public byte[] Content { @@ -123,7 +123,7 @@ public class TypeTemplate return null; } - public Guid ClassId + public UUID ClassId { get { return classId; } } @@ -159,7 +159,7 @@ public class TypeTemplate } - public static Guid GetTypeGuid(Type type) + public static UUID GetTypeUUID(Type type) { var attr = type.GetCustomAttribute(); if (attr != null) @@ -170,7 +170,7 @@ public class TypeTemplate hash[6] = (byte)((hash[6] & 0xF) | 0x80); hash[8] = (byte)((hash[8] & 0xF) | 0x80); - var rt = new Guid(hash); + var rt = new UUID(hash); return rt; } @@ -428,7 +428,7 @@ public class TypeTemplate className = GetTypeClassName(type); // set guid - classId = GetTypeGuid(type); + classId = GetTypeUUID(type); if (addToWarehouse) Warehouse.PutTemplate(this); @@ -525,7 +525,7 @@ public class TypeTemplate var classNameBytes = DC.ToBytes(className); b.AddUInt8((byte)((hasParent ? 0x80 : 0) | (hasClassAnnotation ? 0x40 : 0x0) | (byte)templateType)) - .AddGuid(classId) + .AddUUID(classId) .AddUInt8((byte)classNameBytes.Length) .AddUInt8Array(classNameBytes); @@ -533,8 +533,8 @@ public class TypeTemplate { // find the first parent type that implements IResource ParentDefinedType = ResourceProxy.GetBaseType(type.BaseType); - var parentId = GetTypeGuid(ParentDefinedType); - b.AddGuid(parentId); + var parentId = GetTypeUUID(ParentDefinedType); + b.AddUUID(parentId); } if (hasClassAnnotation) @@ -736,7 +736,7 @@ public class TypeTemplate od.templateType = (TemplateType)(data[offset++] & 0xF); - od.classId = data.GetGuid(offset); + od.classId = data.GetUUID(offset); offset += 16; od.className = data.GetString(offset + 1, data[offset]); offset += (uint)data[offset] + 1; @@ -744,7 +744,7 @@ public class TypeTemplate if (hasParent) { - od.parentId = data.GetGuid(offset); + od.parentId = data.GetUUID(offset); offset += 16; } diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index a3277ec..bac21df 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -54,14 +54,14 @@ public static class Warehouse static uint resourceCounter = 0; - static KeyList> templates - = new KeyList>() + static KeyList> templates + = new KeyList>() { //[TemplateType.Unspecified] = new KeyList(), - [TemplateType.Resource] = new KeyList(), - [TemplateType.Record] = new KeyList(), + [TemplateType.Resource] = new KeyList(), + [TemplateType.Record] = new KeyList(), //[TemplateType.Wrapper] = new KeyList(), - [TemplateType.Enum] = new KeyList(), + [TemplateType.Enum] = new KeyList(), }; static bool warehouseIsOpen = false; @@ -800,7 +800,7 @@ public static class Warehouse /// /// Class Id. /// Resource template. - public static TypeTemplate GetTemplateByClassId(Guid classId, TemplateType? templateType = null) + public static TypeTemplate GetTemplateByClassId(UUID classId, TemplateType? templateType = null) { if (templateType == null) { diff --git a/Test/Program.cs b/Test/Program.cs index a56063e..3585aca 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -56,10 +56,10 @@ namespace Test class Program { - static void TestSerialization(object x) + static void TestSerialization(object x, DistributedConnection connection = null) { - var y = Codec.Compose(x, null); + var y = Codec.Compose(x, connection); var rr = DC.ToHex(y); Console.WriteLine(x.GetType().Name + ": " + rr); @@ -67,15 +67,25 @@ namespace Test [Export] - public class StudentRecord:IRecord + public class StudentRecord : IRecord { public string Name { get; set; } public byte Grade { get; set; } } + public enum LogLevel : int + { + Debug, + Warning, + Error, + } static async Task Main(string[] args) { + var x = LogLevel.Warning; + + TestSerialization(LogLevel.Warning); + TestSerialization(new Map { ["C++"] = 1, @@ -83,14 +93,16 @@ namespace Test ["JS"] = null }); - TestSerialization(new StudentRecord() { Name = "Ali", Grade = 90}); + - var tn = Encoding.UTF8.GetBytes("MyProject.Tools.Logging.LogRecord"); + TestSerialization(new StudentRecord() { Name = "Ali", Grade = 90 }); + + var tn = Encoding.UTF8.GetBytes("Test.StudentRecord"); var hash = System.Security.Cryptography.SHA256.Create().ComputeHash(tn).Clip(0, 16); hash[6] = (byte)((hash[6] & 0xF) | 0x80); hash[8] = (byte)((hash[8] & 0xF) | 0x80); - var g = new Guid(hash); + var g = new UUID(hash); Console.WriteLine(g); @@ -136,6 +148,8 @@ namespace Test var res3 = await Warehouse.Put("sys/service/c1", new MyChildResource() { ChildName = "Child 1", Description = "Child Testing 3", CategoryId = 12 }); var res4 = await Warehouse.Put("sys/service/c2", new MyChildResource() { ChildName = "Child 2 Destroy", Description = "Testing Destroy Handler", CategoryId = 12 }); + TestSerialization(res1); + server.MapCall("Hello", (string msg, DateTime time, DistributedConnection sender) => { Console.WriteLine(msg); @@ -167,7 +181,7 @@ namespace Test } -// AuthorizationRequest, AsyncReply + // AuthorizationRequest, AsyncReply static AsyncReply Authenticator(AuthorizationRequest x) { Console.WriteLine($"Authenticator: {x.Clue}"); @@ -194,7 +208,7 @@ namespace Test Authenticator = Authenticator }); - + dynamic remote = await con.Get("sys/service"); var pcall = await con.Call("Hello", "whats up ?", DateTime.UtcNow);