From ebc80b08cf0c96296be82f852fdda8e6ca6c7614 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Thu, 5 Dec 2024 15:11:24 +0300 Subject: [PATCH] invoke --- Esiur/Core/AsyncChunk.cs | 12 +++++++ Esiur/Core/InvocationContext.cs | 31 +++++++++++++++++ .../Net/IIP/DistributedConnectionProtocol.cs | 7 ++-- Esiur/Resource/ExportAttribute.cs | 24 +++++++++++-- Esiur/Resource/Template/FunctionTemplate.cs | 34 ++++++++++++++----- Esiur/Resource/Template/MemberData.cs | 8 +++-- Esiur/Resource/Template/TypeTemplate.cs | 2 -- 7 files changed, 98 insertions(+), 20 deletions(-) create mode 100644 Esiur/Core/AsyncChunk.cs create mode 100644 Esiur/Core/InvocationContext.cs diff --git a/Esiur/Core/AsyncChunk.cs b/Esiur/Core/AsyncChunk.cs new file mode 100644 index 0000000..6e04fb4 --- /dev/null +++ b/Esiur/Core/AsyncChunk.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Core +{ + // This interface is used to provide return type for templates and support chunk callbacks using IAsyncEnumerable feature of C# 8 + public interface IAsyncChunk : IAsyncEnumerable + { + + } +} diff --git a/Esiur/Core/InvocationContext.cs b/Esiur/Core/InvocationContext.cs new file mode 100644 index 0000000..6ad6090 --- /dev/null +++ b/Esiur/Core/InvocationContext.cs @@ -0,0 +1,31 @@ +using Esiur.Net.IIP; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Core +{ + public class InvocationContext + { + private uint CallbackId; + + public void Chunk(object value) + { + + } + + public void Progress(int value) { + + } + + public DistributedConnection Connection { get; internal set; } + + + internal InvocationContext(DistributedConnection connection, uint callbackId) + { + Connection = connection; + CallbackId = callbackId; + + } + } +} diff --git a/Esiur/Net/IIP/DistributedConnectionProtocol.cs b/Esiur/Net/IIP/DistributedConnectionProtocol.cs index 96511f8..7b76626 100644 --- a/Esiur/Net/IIP/DistributedConnectionProtocol.cs +++ b/Esiur/Net/IIP/DistributedConnectionProtocol.cs @@ -1545,18 +1545,15 @@ partial class DistributedConnection #if NETSTANDARD var res = t.GetType().GetTypeInfo().GetProperty("Result").GetValue(t); #else - var res = t.GetType().GetProperty("Result").GetValue(t); + var res = t.GetType().GetProperty("Result").GetValue(t); #endif SendReply(actionType, callback) .AddUInt8Array(Codec.Compose(res, this)) .Done(); }); - //await t; - //SendParams((byte)0x90, callback, Codec.Compose(res, this)); } - else if (rt is AsyncReply)// Codec.ImplementsInterface(rt.GetType(), typeof(IAsyncReply<>)))// rt.GetType().GetTypeInfo().IsGenericType - //&& rt.GetType().GetGenericTypeDefinition() == typeof(IAsyncReply<>)) + else if (rt is AsyncReply) { (rt as AsyncReply).Then(res => { diff --git a/Esiur/Resource/ExportAttribute.cs b/Esiur/Resource/ExportAttribute.cs index 1e15899..c7fa916 100644 --- a/Esiur/Resource/ExportAttribute.cs +++ b/Esiur/Resource/ExportAttribute.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Xml.Linq; namespace Esiur.Resource; @@ -8,10 +9,29 @@ namespace Esiur.Resource; public class ExportAttribute : Attribute { - public string Name { get; set; } + public string Name { get; private set; } = null; + public Type ReturnType { get; private set; } = null; - public ExportAttribute(string name = null) + public ExportAttribute() + { + + } + + public ExportAttribute(string name) { Name = name; } + + public ExportAttribute(Type returnType) + { + ReturnType = returnType; + } + + public ExportAttribute(string name, Type returnType) + { + Name = name; + ReturnType = returnType; + } + + } diff --git a/Esiur/Resource/Template/FunctionTemplate.cs b/Esiur/Resource/Template/FunctionTemplate.cs index 4dbbcf8..981f6c7 100644 --- a/Esiur/Resource/Template/FunctionTemplate.cs +++ b/Esiur/Resource/Template/FunctionTemplate.cs @@ -66,8 +66,8 @@ public class FunctionTemplate : MemberTemplate return bl.ToArray(); } - public FunctionTemplate(TypeTemplate template, byte index, string name, bool inherited, bool isStatic, ArgumentTemplate[] arguments, RepresentationType returnType, string annotation = null) - : base(template, index, name, inherited) + public FunctionTemplate(TypeTemplate template, byte index, string name, bool inherited, bool isStatic, ArgumentTemplate[] arguments, RepresentationType returnType, string annotation = null) + : base(template, index, name, inherited) { this.Arguments = arguments; this.ReturnType = returnType; @@ -82,9 +82,25 @@ public class FunctionTemplate : MemberTemplate var genericRtType = mi.ReturnType.IsGenericType ? mi.ReturnType.GetGenericTypeDefinition() : null; - var rtType = genericRtType == typeof(AsyncReply<>) ? - RepresentationType.FromType(mi.ReturnType.GetGenericArguments()[0]) : - RepresentationType.FromType(mi.ReturnType); + RepresentationType rtType; + + if (genericRtType == typeof(AsyncReply<>)) + { + rtType = RepresentationType.FromType(mi.ReturnType.GetGenericArguments()[0]); + } + else if (genericRtType == typeof(IEnumerable<>) || genericRtType == typeof(IAsyncEnumerable<>)) + { + // get export + rtType = RepresentationType.FromType(mi.GetCustomAttribute()?.ReturnType ?? typeof(object)); + } + else + { + rtType = RepresentationType.FromType(mi.ReturnType); + } + + //var rtType = genericRtType == typeof(AsyncReply<>) ? + // RepresentationType.FromType(mi.ReturnType.GetGenericArguments()[0]) : + // RepresentationType.FromType(mi.ReturnType); if (rtType == null) throw new Exception($"Unsupported type `{mi.ReturnType}` in method `{type.Name}.{mi.Name}` return"); @@ -106,7 +122,7 @@ public class FunctionTemplate : MemberTemplate //var rtNullableAttr = mi.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(NullableAttribute), true).FirstOrDefault() as NullableAttribute; var rtNullableAttr = mi.ReturnTypeCustomAttributes.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute"); - + // var rtNullableContextAttr = mi.ReturnTypeCustomAttributes // .GetCustomAttributes(typeof(NullableContextAttribute), true) @@ -115,7 +131,7 @@ public class FunctionTemplate : MemberTemplate var rtNullableContextAttr = mi.ReturnTypeCustomAttributes - .GetCustomAttributes(true).FirstOrDefault(x=>x.GetType().Name == "NullableContextAttribute") + .GetCustomAttributes(true).FirstOrDefault(x => x.GetType().Name == "NullableContextAttribute") ?? nullableContextAttr; var rtNullableAttrFlags = (rtNullableAttr?.GetType().GetField("NullableFlags")?.GetValue(rtNullableAttr) as byte[] ?? new byte[0]).ToList(); @@ -164,7 +180,7 @@ public class FunctionTemplate : MemberTemplate var argNullableAttr = x.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableAttribute"); var argNullableContextAttr = x.GetCustomAttributes(true).FirstOrDefault(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttr"); - + //var argFlags = argNullableAttr?.Flags?.ToList() ?? new List(); //var argFlags = ((byte[])argNullableAttr?.NullableFlags ?? new byte[0]).ToList(); @@ -196,7 +212,7 @@ public class FunctionTemplate : MemberTemplate }) .ToArray(); - var fn = customName ?? mi.Name; + var fn = customName ?? mi.Name; var ft = new FunctionTemplate(typeTemplate, index, fn, mi.DeclaringType != type, mi.IsStatic, diff --git a/Esiur/Resource/Template/MemberData.cs b/Esiur/Resource/Template/MemberData.cs index 62c8b8d..a715393 100644 --- a/Esiur/Resource/Template/MemberData.cs +++ b/Esiur/Resource/Template/MemberData.cs @@ -16,10 +16,14 @@ public class MemberData public MemberData? Parent; public MemberData? Child; public byte Index; + //public ExportAttribute ExportAttribute; - public MemberData(string name, MemberInfo info, int order) + + //public string Name => ExportAttribute?.Name ?? Info.Name; + + public MemberData(MemberInfo info, int order) { - this.Name = name; + this.Name = info.GetCustomAttribute()?.Name ?? info.Name; this.Info = info; this.Order = order; } diff --git a/Esiur/Resource/Template/TypeTemplate.cs b/Esiur/Resource/Template/TypeTemplate.cs index 2c1294e..67f4cfb 100644 --- a/Esiur/Resource/Template/TypeTemplate.cs +++ b/Esiur/Resource/Template/TypeTemplate.cs @@ -611,7 +611,6 @@ public class TypeTemplate ) )) .Select(x => new MemberData( - name: x.GetCustomAttribute()?.Name ?? x.Name, info: x, order: order )) @@ -631,7 +630,6 @@ public class TypeTemplate .Where(x => x.GetCustomAttribute() != null) .Where(x => !(x is MethodInfo m && m.IsSpecialName)) .Select(x => new MemberData ( - name : x.GetCustomAttribute()?.Name ?? x.Name, info : x, order : order ))