2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +00:00

ClassId Attribute

This commit is contained in:
Esiur Project 2022-09-03 22:00:26 +03:00
parent 2cf0d59e6b
commit 620dbaf653
8 changed files with 45 additions and 21 deletions

View File

@ -464,7 +464,7 @@ public static class DC // Data Converter
public static string ToHex(this byte[] ba) public static string ToHex(this byte[] ba)
{ {
if (ba == null) if (ba == null)
return "NULL"; return "";
return ToHex(ba, 0, (uint)ba.Length); return ToHex(ba, 0, (uint)ba.Length);
} }

View File

@ -6,7 +6,7 @@
<Copyright>Ahmed Kh. Zamil</Copyright> <Copyright>Ahmed Kh. Zamil</Copyright>
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl> <PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.3.4</Version> <Version>2.3.6</Version>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl> <RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors> <Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion></AssemblyVersion> <AssemblyVersion></AssemblyVersion>

View File

@ -1342,7 +1342,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
{ {
if (AutoReconnect) if (AutoReconnect)
{ {
Console.Write("Reconnecting socket..."); Global.Log("DistributedConnection", LogType.Debug, "Reconnecting socket...");
Task.Delay((int)ReconnectInterval).ContinueWith((x) => connectSocket(socket)); Task.Delay((int)ReconnectInterval).ContinueWith((x) => connectSocket(socket));
} }
else else

View File

@ -115,14 +115,14 @@ public class HTTPResponsePacket : Packet
} }
} }
public StringKeyList Headers = new StringKeyList(true); public StringKeyList Headers { get; } = new StringKeyList(true);
public string Version = "HTTP/1.1"; public string Version { get; set; } = "HTTP/1.1";
public byte[] Message; public byte[] Message;
public ResponseCode Number; public ResponseCode Number { get; set; } = ResponseCode.OK;
public string Text; public string Text;
public List<HTTPCookie> Cookies = new List<HTTPCookie>(); public List<HTTPCookie> Cookies { get; } = new List<HTTPCookie>();
public bool Handled; public bool Handled;
public override string ToString() public override string ToString()
@ -206,8 +206,6 @@ public class HTTPResponsePacket : Packet
if (headerSize == 0) if (headerSize == 0)
return -1; return -1;
//Cookies = new DStringDictionary();
//Headers = new DStringDictionary(true);
sMethod = sLines[0].Split(' '); sMethod = sLines[0].Split(' ');

View File

@ -74,6 +74,8 @@ public static class TemplateGenerator
if (template.Annotation != null) if (template.Annotation != null)
rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]");
rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]");
rt.AppendLine($"[Public] public class {className} : IRecord {{"); rt.AppendLine($"[Public] public class {className} : IRecord {{");
@ -104,6 +106,7 @@ public static class TemplateGenerator
if (template.Annotation != null) if (template.Annotation != null)
rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]");
rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]");
rt.AppendLine($"[Public] public enum {className} {{"); rt.AppendLine($"[Public] public enum {className} {{");
rt.AppendLine(String.Join(",\r\n", template.Constants.Select(x => $"{x.Name}={x.Value}"))); 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) if (template.Annotation != null)
rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]"); rt.AppendLine($"[Annotation({ToLiteral(template.Annotation)})]");
rt.AppendLine($"[ClassId(\"{template.ClassId.ToByteArray().ToHex(0, 16, null)}\")]");
// extends // extends
if (template.ParentId == null) if (template.ParentId == null)
rt.AppendLine($"public class {className} : DistributedResource {{"); rt.AppendLine($"public class {className} : DistributedResource {{");

View File

@ -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);
}
}
}

View File

@ -159,16 +159,18 @@ public class TypeTemplate
} }
public static Guid GetTypeGuid(Type type) => GetTypeGuid(GetTypeClassName(type)); public static Guid GetTypeGuid(Type type)
public static Guid GetTypeGuid(string typeName)
{ {
var tn = Encoding.UTF8.GetBytes(typeName); var attr = type.GetCustomAttribute<ClassIdAttribute>();
if (attr != null)
return attr.ClassId;
var tn = Encoding.UTF8.GetBytes(GetTypeClassName(type));
var hash = SHA256.Create().ComputeHash(tn).Clip(0, 16); var hash = SHA256.Create().ComputeHash(tn).Clip(0, 16);
var rt = new Guid(hash);
return new Guid(hash); return rt;
} }
static Type[] GetDistributedTypes(Type type) static Type[] GetDistributedTypes(Type type)
{ {
if (type.IsArray) if (type.IsArray)
@ -419,7 +421,7 @@ public class TypeTemplate
className = GetTypeClassName(type); className = GetTypeClassName(type);
// set guid // set guid
classId = GetTypeGuid(className); classId = GetTypeGuid(type);
if (addToWarehouse) if (addToWarehouse)
Warehouse.PutTemplate(this); Warehouse.PutTemplate(this);

View File

@ -43,17 +43,16 @@ using Esiur.Proxy;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace Test namespace Test
{ {
class Program class Program
{ {
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
// Create stores to keep objects. // Create stores to keep objects.
var system = await Warehouse.Put("sys", new MemoryStore()); var system = await Warehouse.Put("sys", new MemoryStore());