2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-26 13:03:13 +00:00
This commit is contained in:
2025-05-30 18:00:10 +03:00
parent 276e7b17fd
commit a720b11959
10 changed files with 58 additions and 33 deletions

View File

@ -257,9 +257,9 @@ public static class Codec
{ {
var genericType = type.GetGenericTypeDefinition(); var genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(DistributedPropertyContext<>)) if (genericType == typeof(PropertyContext<>))
{ {
valueOrSource = ((IDistributedPropertyContext)valueOrSource).GetValue(connection); valueOrSource = ((IPropertyContext)valueOrSource).GetValue(connection);
} }
else if (genericType == typeof(Func<>)) else if (genericType == typeof(Func<>))
{ {

View File

@ -34,7 +34,7 @@ namespace Esiur.Data
public UUID(byte[] data, uint offset) public UUID(byte[] data, uint offset)
{ {
if (offset + 16 < data.Length) if (offset + 16 > data.Length)
throw new Exception("UUID data size must be at least 16 bytes"); throw new Exception("UUID data size must be at least 16 bytes");
Data = DC.Clip(data, offset, 16); Data = DC.Clip(data, offset, 16);

View File

@ -1909,7 +1909,7 @@ partial class DistributedConnection
if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition()
== typeof(DistributedPropertyContext<>)) == typeof(PropertyContext<>))
{ {
value = Activator.CreateInstance(pi.PropertyType, this, value); value = Activator.CreateInstance(pi.PropertyType, this, value);
//value = new DistributedPropertyContext(this, value); //value = new DistributedPropertyContext(this, value);

View File

@ -4,30 +4,30 @@ using System.Text;
namespace Esiur.Net.IIP; namespace Esiur.Net.IIP;
public interface IDistributedPropertyContext public interface IPropertyContext
{ {
object GetValue(DistributedConnection connection); object GetValue(DistributedConnection connection);
} }
public class DistributedPropertyContext<T> : IDistributedPropertyContext public class PropertyContext<T> : IPropertyContext
{ {
public T Value { get; private set; } public T Value { get; private set; }
public DistributedConnection Connection { get; private set; } public DistributedConnection Connection { get; private set; }
public Func<DistributedConnection, T> Method { get; private set; } public Func<DistributedConnection, T> Method { get; private set; }
public DistributedPropertyContext(DistributedConnection connection, T value) public PropertyContext(DistributedConnection connection, T value)
{ {
this.Value = value; this.Value = value;
this.Connection = connection; this.Connection = connection;
} }
public DistributedPropertyContext(Func<DistributedConnection, T> method) public PropertyContext(Func<DistributedConnection, T> method)
{ {
this.Method = method; this.Method = method;
} }
public static implicit operator DistributedPropertyContext<T>(Func<DistributedConnection, T> method) public static implicit operator PropertyContext<T>(Func<DistributedConnection, T> method)
=> new DistributedPropertyContext<T>(method); => new PropertyContext<T>(method);
public object GetValue(DistributedConnection connection) public object GetValue(DistributedConnection connection)
{ {

View File

@ -352,7 +352,7 @@ public class Instance
*/ */
if (pt.PropertyInfo.PropertyType.IsGenericType if (pt.PropertyInfo.PropertyType.IsGenericType
&& pt.PropertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(DistributedPropertyContext<>)) && pt.PropertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(PropertyContext<>))
return false; return false;

View File

@ -151,7 +151,7 @@ public class PropertyTemplate : MemberTemplate
{ {
var genericPropType = pi.PropertyType.IsGenericType ? pi.PropertyType.GetGenericTypeDefinition() : null; var genericPropType = pi.PropertyType.IsGenericType ? pi.PropertyType.GetGenericTypeDefinition() : null;
var propType = genericPropType == typeof(DistributedPropertyContext<>) ? var propType = genericPropType == typeof(PropertyContext<>) ?
RepresentationType.FromType(pi.PropertyType.GetGenericArguments()[0]) : RepresentationType.FromType(pi.PropertyType.GetGenericArguments()[0]) :
RepresentationType.FromType(pi.PropertyType); RepresentationType.FromType(pi.PropertyType);
@ -174,7 +174,7 @@ public class PropertyTemplate : MemberTemplate
//var nullableAttr = pi.GetCustomAttribute<NullableAttribute>(true); //var nullableAttr = pi.GetCustomAttribute<NullableAttribute>(true);
//var flags = ((byte[]) nullableAttr?.NullableFlags ?? new byte[0]).ToList(); //var flags = ((byte[]) nullableAttr?.NullableFlags ?? new byte[0]).ToList();
if (nullableAttrFlags.Count > 0 && genericPropType == typeof(DistributedPropertyContext<>)) if (nullableAttrFlags.Count > 0 && genericPropType == typeof(PropertyContext<>))
nullableAttrFlags.RemoveAt(0); nullableAttrFlags.RemoveAt(0);
if (nullableContextAttrFlag == 2) if (nullableContextAttrFlag == 2)

View File

@ -191,7 +191,7 @@ public class TypeTemplate
var genericTypeArgs = type.GetGenericArguments(); var genericTypeArgs = type.GetGenericArguments();
if (genericType == typeof(List<>) if (genericType == typeof(List<>)
|| genericType == typeof(DistributedPropertyContext<>)) || genericType == typeof(PropertyContext<>))
{ {
return GetDistributedTypes(genericTypeArgs[0]); return GetDistributedTypes(genericTypeArgs[0]);
} }

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Test namespace Test
{ {
@ -19,5 +20,13 @@ namespace Test
[Export] public string HelloParent() => "Hi from Parent"; [Export] public string HelloParent() => "Hi from Parent";
[Export]
[Annotation("This function computes the standard deviation of a list")]
public double StDev(double[] values)
{
double avg = values.Average();
return Math.Sqrt(values.Average(v => Math.Pow(v - avg, 2)));
}
} }
} }

View File

@ -106,15 +106,28 @@ public partial class MyService
[Export] object[] objectArray = new object[] { 1, 1.2f, Math.PI, "Hello World" }; [Export] object[] objectArray = new object[] { 1, 1.2f, Math.PI, "Hello World" };
[Export] [Export]
public DistributedPropertyContext<int> PropertyContext public PropertyContext<int> PropertyContext
{ {
get => new DistributedPropertyContext<int>((sender) => sender.RemoteEndPoint.Port); get => new PropertyContext<int>((sender) => sender.RemoteEndPoint.Port);
set set
{ {
Console.WriteLine($"PropertyContext Set: {value.Value} {value.Connection.RemoteEndPoint.Port}"); Console.WriteLine($"PropertyContext Set: {value.Value} {value.Connection.RemoteEndPoint.Port}");
} }
} }
int MyPasscode = 2025;
public PropertyContext<int> Passcode
{
get => new ((sender) => sender.Session.AuthorizedAccount == "alice" ? MyPasscode : 0);
set
{
if (value.Connection.Session.AuthorizedAccount != "alice")
throw new Exception("Only Alice is allowed.");
MyPasscode = value.Value;
}
}
[Export] public SizeEnum Enum => SizeEnum.Medium; [Export] public SizeEnum Enum => SizeEnum.Medium;
@ -144,8 +157,11 @@ public partial class MyService
Console.WriteLine("Void()"); Console.WriteLine("Void()");
[Export] [Export]
public void InvokeEvents(string msg) public void InvokeEvents(string msg, InvocationContext context)
{ {
if (context.Connection.Session.AuthorizedAccount != "Alice")
throw new Exception("Only Alice is allowed.");
StringEvent?.Invoke(msg); StringEvent?.Invoke(msg);
ArrayEvent?.Invoke(new object[] { DateTime.UtcNow, "Event", msg }); ArrayEvent?.Invoke(new object[] { DateTime.UtcNow, "Event", msg });
} }

View File

@ -82,29 +82,29 @@ namespace Test
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
var x = LogLevel.Warning; //var x = LogLevel.Warning;
TestSerialization(LogLevel.Warning); //TestSerialization(LogLevel.Warning);
TestSerialization(new Map<string, byte?> //TestSerialization(new Map<string, byte?>
{ //{
["C++"] = 1, // ["C++"] = 1,
["C#"] = 2, // ["C#"] = 2,
["JS"] = null // ["JS"] = null
}); //});
TestSerialization(new StudentRecord() { Name = "Ali", Grade = 90 }); //TestSerialization(new StudentRecord() { Name = "Ali", Grade = 90 });
var tn = Encoding.UTF8.GetBytes("Test.StudentRecord"); //var tn = Encoding.UTF8.GetBytes("Test.StudentRecord");
var hash = System.Security.Cryptography.SHA256.Create().ComputeHash(tn).Clip(0, 16); //var hash = System.Security.Cryptography.SHA256.Create().ComputeHash(tn).Clip(0, 16);
hash[6] = (byte)((hash[6] & 0xF) | 0x80); //hash[6] = (byte)((hash[6] & 0xF) | 0x80);
hash[8] = (byte)((hash[8] & 0xF) | 0x80); //hash[8] = (byte)((hash[8] & 0xF) | 0x80);
var g = new UUID(hash); //var g = new UUID(hash);
Console.WriteLine(g); //Console.WriteLine(g);
var hhhh = Warehouse.GetTemplateByType(typeof(IMyRecord)); var hhhh = Warehouse.GetTemplateByType(typeof(IMyRecord));
@ -148,7 +148,7 @@ namespace Test
var res3 = await Warehouse.Put("sys/service/c1", new MyChildResource() { ChildName = "Child 1", Description = "Child Testing 3", CategoryId = 12 }); 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 }); var res4 = await Warehouse.Put("sys/service/c2", new MyChildResource() { ChildName = "Child 2 Destroy", Description = "Testing Destroy Handler", CategoryId = 12 });
TestSerialization(res1); //TestSerialization(res1);
server.MapCall("Hello", (string msg, DateTime time, DistributedConnection sender) => server.MapCall("Hello", (string msg, DateTime time, DistributedConnection sender) =>
{ {