From a1353a03ad84f417d0431acea829d2b5ca4dba37 Mon Sep 17 00:00:00 2001 From: ahmed Date: Sun, 24 Aug 2025 05:58:40 +0300 Subject: [PATCH] UUID --- Esiur/Data/Codec.cs | 16 ++-- Esiur/Data/DataSerializer.cs | 8 +- Esiur/Data/UUID.cs | 25 ++++++ Esiur/Net/IIP/DistributedConnection.cs | 6 +- .../Packets/IIPAuthPacketIAuthDestination.cs | 2 +- Esiur/Resource/Warehouse.cs | 88 ++++++++++--------- .../Membership/AuthorizationRequest.cs | 2 +- 7 files changed, 92 insertions(+), 55 deletions(-) diff --git a/Esiur/Data/Codec.cs b/Esiur/Data/Codec.cs index 7ccf231..73f7c23 100644 --- a/Esiur/Data/Codec.cs +++ b/Esiur/Data/Codec.cs @@ -122,30 +122,30 @@ public static class Codec DataDeserializer.LocalResourceParser8, DataDeserializer.ResourceParser8, }, - new SyncParser[]{ - DataDeserializer.Int16Parser, + new SyncParser[]{ DataDeserializer.UInt16Parser, + DataDeserializer.Int16Parser, DataDeserializer.Char16Parser, DataDeserializer.LocalResourceParser16, DataDeserializer.ResourceParser16, }, - new SyncParser[]{ - DataDeserializer.Int32Parser, + new SyncParser[]{ DataDeserializer.UInt32Parser, + DataDeserializer.Int32Parser, DataDeserializer.Float32Parser, DataDeserializer.LocalResourceParser32, DataDeserializer.ResourceParser32, }, - new SyncParser[]{ - DataDeserializer.Int64Parser, + new SyncParser[]{ DataDeserializer.UInt64Parser, + DataDeserializer.Int64Parser, DataDeserializer.Float64Parser, DataDeserializer.DateTimeParser, }, new SyncParser[] - { - DataDeserializer.Int128Parser, // int 128 + { DataDeserializer.UInt128Parser, // uint 128 + DataDeserializer.Int128Parser, // int 128 DataDeserializer.Float128Parser, } }; diff --git a/Esiur/Data/DataSerializer.cs b/Esiur/Data/DataSerializer.cs index 6f1c067..a6f6f0d 100644 --- a/Esiur/Data/DataSerializer.cs +++ b/Esiur/Data/DataSerializer.cs @@ -114,10 +114,16 @@ public static class DataSerializer public static (TransmissionTypeIdentifier, byte[]) EnumComposer(object value, DistributedConnection connection) { + Console.WriteLine(value.GetType().Name); + if (value == null) return (TransmissionTypeIdentifier.Null, new byte[0]); - var template = connection.Instance.Warehouse.GetTemplateByType(value.GetType()); + var warehouse = connection?.Instance?.Warehouse ?? connection?.Server?.Instance?.Warehouse; + if (warehouse == null) + throw new Exception("Warehouse not set."); + + var template = warehouse.GetTemplateByType(value.GetType()); var intVal = Convert.ChangeType(value, (value as Enum).GetTypeCode()); diff --git a/Esiur/Data/UUID.cs b/Esiur/Data/UUID.cs index 7458d50..f3ffbee 100644 --- a/Esiur/Data/UUID.cs +++ b/Esiur/Data/UUID.cs @@ -57,6 +57,23 @@ namespace Esiur.Data //e6 = data[offset++]; } + public unsafe override int GetHashCode() + { + unchecked + { + fixed (byte* p = Data) + { + ulong u0 = *(ulong*)p; + ulong u1 = *(ulong*)(p + 8); + + // simple mixing of two 64-bit halves + return ((int)u0 ^ (int)(u0 >> 32)) ^ + ((int)u1 ^ (int)(u1 >> 32)); + } + } + + } + public UUID(byte[] data) { if (data.Length != 16) @@ -88,6 +105,14 @@ namespace Esiur.Data //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 override bool Equals(object obj) + { + if (obj is UUID b) + return Data.SequenceEqual(b.Data); + + return false; + } + public static bool operator == (UUID a, UUID b) { return a.Data.SequenceEqual(b.Data); diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index 506408e..0c50ada 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -764,7 +764,7 @@ public partial class DistributedConnection : NetworkConnection, IStore if (this.Instance == null) { - Instance.Warehouse.Put(Server + "/" + session.AuthorizedAccount.Replace("/", "_"), this) + Server.Instance.Warehouse.Put(Server + "/" + session.AuthorizedAccount.Replace("/", "_"), this) .Then(x => { openReply?.Trigger(true); @@ -896,7 +896,7 @@ public partial class DistributedConnection : NetworkConnection, IStore var dataType = authPacket.DataType.Value; - var (_, parsed) = Codec.ParseSync(data, dataType.Offset, Instance.Warehouse, dataType); + var (_, parsed) = Codec.ParseSync(data, dataType.Offset, Server.Instance.Warehouse, dataType); var rt = (Map)parsed; @@ -1232,7 +1232,7 @@ public partial class DistributedConnection : NetworkConnection, IStore if (this.Instance == null) { - Instance.Warehouse.Put( + Server.Instance.Warehouse.Put( Server.Instance.Link + "/" + this.GetHashCode().ToString().Replace("/", "_"), this) .Then(x => { diff --git a/Esiur/Net/Packets/IIPAuthPacketIAuthDestination.cs b/Esiur/Net/Packets/IIPAuthPacketIAuthDestination.cs index 4817886..500abe3 100644 --- a/Esiur/Net/Packets/IIPAuthPacketIAuthDestination.cs +++ b/Esiur/Net/Packets/IIPAuthPacketIAuthDestination.cs @@ -11,6 +11,6 @@ namespace Esiur.Net.Packets Email = 2, SMS = 3, App = 4, // Authenticator app - ThirdParty = 5, // usualy a second person + ThirdParty = 5, // usually a second person } } diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index 581526c..c55f08f 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -22,22 +22,23 @@ SOFTWARE. */ -using Esiur.Data; using Esiur.Core; +using Esiur.Data; +using Esiur.Misc; +using Esiur.Net.IIP; +using Esiur.Net.Packets; using Esiur.Proxy; using Esiur.Resource.Template; using Esiur.Security.Permissions; using System; +using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Text; -using System.Threading.Tasks; -using Esiur.Net.IIP; using System.Text.RegularExpressions; -using Esiur.Misc; -using System.Collections.Concurrent; -using System.Collections; -using System.Data; +using System.Threading.Tasks; namespace Esiur.Resource; @@ -85,6 +86,11 @@ public class Warehouse Protocols.Add("iip", async (name, attributes) => await New(name, null, attributes)); + + new TypeTemplate(typeof(IIPAuthPacketIAuthHeader), this); + + new TypeTemplate(typeof(IIPAuthPacketIAuthDestination), this); + new TypeTemplate(typeof(IIPAuthPacketIAuthFormat), this); } @@ -351,45 +357,45 @@ public class Warehouse if (resource is IStore && store == null) store = (IStore)resource; - + if (store == null) throw new Exception("Resource store is not set."); - //if (store == null) - //{ - // // assign parent's store as a store - // if (parent != null) - // { - // // assign parent as a store - // if (parent is IStore) - // { - // store = (IStore)parent; - // List> list; - // if (stores.TryGetValue(store, out list)) - // lock (((ICollection)list).SyncRoot) - // list.Add(resourceReference); - // //stores[store].Add(resourceReference); - // } - // else - // { - // store = parent.Instance.Store; + //if (store == null) + //{ + // // assign parent's store as a store + // if (parent != null) + // { + // // assign parent as a store + // if (parent is IStore) + // { + // store = (IStore)parent; + // List> list; + // if (stores.TryGetValue(store, out list)) + // lock (((ICollection)list).SyncRoot) + // list.Add(resourceReference); + // //stores[store].Add(resourceReference); + // } + // else + // { + // store = parent.Instance.Store; - // List> list; - // if (stores.TryGetValue(store, out list)) - // lock (((ICollection)list).SyncRoot) - // list.Add(resourceReference); - // } - // } - // // assign self as a store (root store) - // else if (resource is IStore) - // { - // store = (IStore)resource; - // } - // else - // throw new Exception("Can't find a store for the resource."); - //} + // List> list; + // if (stores.TryGetValue(store, out list)) + // lock (((ICollection)list).SyncRoot) + // list.Add(resourceReference); + // } + // } + // // assign self as a store (root store) + // else if (resource is IStore) + // { + // store = (IStore)resource; + // } + // else + // throw new Exception("Can't find a store for the resource."); + //} - resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age); + resource.Instance = new Instance(this, resourceCounter++, instanceName, resource, store, customTemplate, age); if (attributes != null) if (attributes is Map attrs) diff --git a/Esiur/Security/Membership/AuthorizationRequest.cs b/Esiur/Security/Membership/AuthorizationRequest.cs index 394455f..1fe406b 100644 --- a/Esiur/Security/Membership/AuthorizationRequest.cs +++ b/Esiur/Security/Membership/AuthorizationRequest.cs @@ -27,7 +27,7 @@ namespace Esiur.Security.Membership public AuthorizationRequest(Map headers) { Reference = (uint)headers[IIPAuthPacketIAuthHeader.Reference]; - Destination = (IIPAuthPacketIAuthDestination)headers[IIPAuthPacketIAuthHeader.Destination]; + Destination =(IIPAuthPacketIAuthDestination)headers[IIPAuthPacketIAuthHeader.Destination]; Clue = (string)headers[IIPAuthPacketIAuthHeader.Clue]; if (headers.ContainsKey(IIPAuthPacketIAuthHeader.RequiredFormat))