2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +00:00
This commit is contained in:
Ahmed Zamil 2024-12-05 15:11:24 +03:00
parent 2bccebd419
commit ebc80b08cf
7 changed files with 98 additions and 20 deletions

12
Esiur/Core/AsyncChunk.cs Normal file
View File

@ -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<T> : IAsyncEnumerable<object>
{
}
}

View File

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

View File

@ -1545,18 +1545,15 @@ partial class DistributedConnection
#if NETSTANDARD #if NETSTANDARD
var res = t.GetType().GetTypeInfo().GetProperty("Result").GetValue(t); var res = t.GetType().GetTypeInfo().GetProperty("Result").GetValue(t);
#else #else
var res = t.GetType().GetProperty("Result").GetValue(t); var res = t.GetType().GetProperty("Result").GetValue(t);
#endif #endif
SendReply(actionType, callback) SendReply(actionType, callback)
.AddUInt8Array(Codec.Compose(res, this)) .AddUInt8Array(Codec.Compose(res, this))
.Done(); .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 else if (rt is AsyncReply)
//&& rt.GetType().GetGenericTypeDefinition() == typeof(IAsyncReply<>))
{ {
(rt as AsyncReply).Then(res => (rt as AsyncReply).Then(res =>
{ {

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Xml.Linq;
namespace Esiur.Resource; namespace Esiur.Resource;
@ -8,10 +9,29 @@ namespace Esiur.Resource;
public class ExportAttribute : Attribute 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; Name = name;
} }
public ExportAttribute(Type returnType)
{
ReturnType = returnType;
}
public ExportAttribute(string name, Type returnType)
{
Name = name;
ReturnType = returnType;
}
} }

View File

@ -66,8 +66,8 @@ public class FunctionTemplate : MemberTemplate
return bl.ToArray(); return bl.ToArray();
} }
public FunctionTemplate(TypeTemplate template, byte index, string name, bool inherited, bool isStatic, ArgumentTemplate[] arguments, RepresentationType returnType, string annotation = null) public FunctionTemplate(TypeTemplate template, byte index, string name, bool inherited, bool isStatic, ArgumentTemplate[] arguments, RepresentationType returnType, string annotation = null)
: base(template, index, name, inherited) : base(template, index, name, inherited)
{ {
this.Arguments = arguments; this.Arguments = arguments;
this.ReturnType = returnType; this.ReturnType = returnType;
@ -82,9 +82,25 @@ public class FunctionTemplate : MemberTemplate
var genericRtType = mi.ReturnType.IsGenericType ? mi.ReturnType.GetGenericTypeDefinition() : null; var genericRtType = mi.ReturnType.IsGenericType ? mi.ReturnType.GetGenericTypeDefinition() : null;
var rtType = genericRtType == typeof(AsyncReply<>) ? RepresentationType rtType;
RepresentationType.FromType(mi.ReturnType.GetGenericArguments()[0]) :
RepresentationType.FromType(mi.ReturnType); 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<ExportAttribute>()?.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) if (rtType == null)
throw new Exception($"Unsupported type `{mi.ReturnType}` in method `{type.Name}.{mi.Name}` return"); throw new Exception($"Unsupported type `{mi.ReturnType}` in method `{type.Name}.{mi.Name}` return");
@ -115,7 +131,7 @@ public class FunctionTemplate : MemberTemplate
var rtNullableContextAttr = mi.ReturnTypeCustomAttributes var rtNullableContextAttr = mi.ReturnTypeCustomAttributes
.GetCustomAttributes(true).FirstOrDefault(x=>x.GetType().Name == "NullableContextAttribute") .GetCustomAttributes(true).FirstOrDefault(x => x.GetType().Name == "NullableContextAttribute")
?? nullableContextAttr; ?? nullableContextAttr;
var rtNullableAttrFlags = (rtNullableAttr?.GetType().GetField("NullableFlags")?.GetValue(rtNullableAttr) as byte[] ?? new byte[0]).ToList(); var rtNullableAttrFlags = (rtNullableAttr?.GetType().GetField("NullableFlags")?.GetValue(rtNullableAttr) as byte[] ?? new byte[0]).ToList();
@ -196,7 +212,7 @@ public class FunctionTemplate : MemberTemplate
}) })
.ToArray(); .ToArray();
var fn = customName ?? mi.Name; var fn = customName ?? mi.Name;
var ft = new FunctionTemplate(typeTemplate, index, fn, mi.DeclaringType != type, var ft = new FunctionTemplate(typeTemplate, index, fn, mi.DeclaringType != type,
mi.IsStatic, mi.IsStatic,

View File

@ -16,10 +16,14 @@ public class MemberData
public MemberData? Parent; public MemberData? Parent;
public MemberData? Child; public MemberData? Child;
public byte Index; 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<ExportAttribute>()?.Name ?? info.Name;
this.Info = info; this.Info = info;
this.Order = order; this.Order = order;
} }

View File

@ -611,7 +611,6 @@ public class TypeTemplate
) )
)) ))
.Select(x => new MemberData( .Select(x => new MemberData(
name: x.GetCustomAttribute<ExportAttribute>()?.Name ?? x.Name,
info: x, info: x,
order: order order: order
)) ))
@ -631,7 +630,6 @@ public class TypeTemplate
.Where(x => x.GetCustomAttribute<ExportAttribute>() != null) .Where(x => x.GetCustomAttribute<ExportAttribute>() != null)
.Where(x => !(x is MethodInfo m && m.IsSpecialName)) .Where(x => !(x is MethodInfo m && m.IsSpecialName))
.Select(x => new MemberData ( .Select(x => new MemberData (
name : x.GetCustomAttribute<ExportAttribute>()?.Name ?? x.Name,
info : x, info : x,
order : order order : order
)) ))