From 620dbaf653744e6d05974b04cab33a4fe796d0f1 Mon Sep 17 00:00:00 2001 From: Esiur Project Date: Sat, 3 Sep 2022 22:00:26 +0300 Subject: [PATCH] ClassId Attribute --- Esiur/Data/DataConverter.cs | 2 +- Esiur/Esiur.csproj | 2 +- Esiur/Net/IIP/DistributedConnection.cs | 2 +- Esiur/Net/Packets/HTTPResponsePacket.cs | 10 ++++------ Esiur/Proxy/TemplateGenerator.cs | 6 ++++++ Esiur/Resource/ClassIdAttribute.cs | 19 +++++++++++++++++++ Esiur/Resource/Template/TypeTemplate.cs | 18 ++++++++++-------- Test/Program.cs | 7 +++---- 8 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 Esiur/Resource/ClassIdAttribute.cs diff --git a/Esiur/Data/DataConverter.cs b/Esiur/Data/DataConverter.cs index 234187d..c289a4f 100644 --- a/Esiur/Data/DataConverter.cs +++ b/Esiur/Data/DataConverter.cs @@ -464,7 +464,7 @@ public static class DC // Data Converter public static string ToHex(this byte[] ba) { if (ba == null) - return "NULL"; + return ""; return ToHex(ba, 0, (uint)ba.Length); } diff --git a/Esiur/Esiur.csproj b/Esiur/Esiur.csproj index c6ceb7b..9203a94 100644 --- a/Esiur/Esiur.csproj +++ b/Esiur/Esiur.csproj @@ -6,7 +6,7 @@ Ahmed Kh. Zamil http://www.esiur.com true - 2.3.4 + 2.3.6 https://github.com/esiur/esiur-dotnet Ahmed Kh. Zamil diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index 6f6c8d3..5ddb9b2 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -1342,7 +1342,7 @@ public partial class DistributedConnection : NetworkConnection, IStore { if (AutoReconnect) { - Console.Write("Reconnecting socket..."); + Global.Log("DistributedConnection", LogType.Debug, "Reconnecting socket..."); Task.Delay((int)ReconnectInterval).ContinueWith((x) => connectSocket(socket)); } else diff --git a/Esiur/Net/Packets/HTTPResponsePacket.cs b/Esiur/Net/Packets/HTTPResponsePacket.cs index 87a1ea8..1c167f5 100644 --- a/Esiur/Net/Packets/HTTPResponsePacket.cs +++ b/Esiur/Net/Packets/HTTPResponsePacket.cs @@ -115,14 +115,14 @@ public class HTTPResponsePacket : Packet } } - public StringKeyList Headers = new StringKeyList(true); - public string Version = "HTTP/1.1"; + public StringKeyList Headers { get; } = new StringKeyList(true); + public string Version { get; set; } = "HTTP/1.1"; public byte[] Message; - public ResponseCode Number; + public ResponseCode Number { get; set; } = ResponseCode.OK; public string Text; - public List Cookies = new List(); + public List Cookies { get; } = new List(); public bool Handled; public override string ToString() @@ -206,8 +206,6 @@ public class HTTPResponsePacket : Packet if (headerSize == 0) return -1; - //Cookies = new DStringDictionary(); - //Headers = new DStringDictionary(true); sMethod = sLines[0].Split(' '); diff --git a/Esiur/Proxy/TemplateGenerator.cs b/Esiur/Proxy/TemplateGenerator.cs index 5a00fab..a175e3d 100644 --- a/Esiur/Proxy/TemplateGenerator.cs +++ b/Esiur/Proxy/TemplateGenerator.cs @@ -74,6 +74,8 @@ 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($"[Public] public class {className} : IRecord {{"); @@ -104,6 +106,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($"[Public] public enum {className} {{"); rt.AppendLine(String.Join(",\r\n", template.Constants.Select(x => $"{x.Name}={x.Value}"))); @@ -268,6 +271,9 @@ public static class TemplateGenerator if (template.Annotation != null) rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); + + rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]"); + // extends if (template.ParentId == null) rt.AppendLine($"public class {className} : DistributedResource {{"); diff --git a/Esiur/Resource/ClassIdAttribute.cs b/Esiur/Resource/ClassIdAttribute.cs new file mode 100644 index 0000000..2acb685 --- /dev/null +++ b/Esiur/Resource/ClassIdAttribute.cs @@ -0,0 +1,19 @@ +using Esiur.Data; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Resource +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum)] + public class ClassIdAttribute : Attribute + { + public Guid ClassId { get; private set; } + + public ClassIdAttribute(string classId) + { + var data = DC.FromHex(classId, null); + ClassId = new Guid(data); + } + } +} diff --git a/Esiur/Resource/Template/TypeTemplate.cs b/Esiur/Resource/Template/TypeTemplate.cs index 2b01c67..827e377 100644 --- a/Esiur/Resource/Template/TypeTemplate.cs +++ b/Esiur/Resource/Template/TypeTemplate.cs @@ -159,16 +159,18 @@ public class TypeTemplate } - public static Guid GetTypeGuid(Type type) => GetTypeGuid(GetTypeClassName(type)); - - public static Guid GetTypeGuid(string typeName) + public static Guid GetTypeGuid(Type type) { - var tn = Encoding.UTF8.GetBytes(typeName); + var attr = type.GetCustomAttribute(); + if (attr != null) + return attr.ClassId; + + var tn = Encoding.UTF8.GetBytes(GetTypeClassName(type)); var hash = SHA256.Create().ComputeHash(tn).Clip(0, 16); - - return new Guid(hash); + var rt = new Guid(hash); + return rt; } - + static Type[] GetDistributedTypes(Type type) { if (type.IsArray) @@ -419,7 +421,7 @@ public class TypeTemplate className = GetTypeClassName(type); // set guid - classId = GetTypeGuid(className); + classId = GetTypeGuid(type); if (addToWarehouse) Warehouse.PutTemplate(this); diff --git a/Test/Program.cs b/Test/Program.cs index 32f8ecb..5d40579 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -43,17 +43,16 @@ using Esiur.Proxy; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Reflection; +using System.Security.Cryptography; +using System.Text; namespace Test { - - class Program { - static async Task Main(string[] args) - { + { // Create stores to keep objects. var system = await Warehouse.Put("sys", new MemoryStore());