2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00
This commit is contained in:
Ahmed Zamil 2024-12-07 02:56:49 +03:00
parent ebc80b08cf
commit 07561062ca
5 changed files with 37 additions and 37 deletions

View File

@ -1,12 +0,0 @@
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

@ -9,13 +9,22 @@ namespace Esiur.Core
{ {
private uint CallbackId; private uint CallbackId;
internal bool Ended;
public void Chunk(object value) public void Chunk(object value)
{ {
if (Ended)
throw new Exception("Execution has ended.");
Connection.SendChunk(CallbackId, value);
} }
public void Progress(int value) { public void Progress(int value, int max) {
if (Ended)
throw new Exception("Execution has ended.");
Connection.SendProgress(CallbackId, value, max);
} }
public DistributedConnection Connection { get; internal set; } public DistributedConnection Connection { get; internal set; }

View File

@ -268,7 +268,7 @@ partial class DistributedConnection
.Done(); .Done();
} }
void SendProgress(uint callbackId, int value, int max) internal void SendProgress(uint callbackId, int value, int max)
{ {
SendParams() SendParams()
.AddUInt8((byte)(0xC0 | (byte)IIPPacketReport.ProgressReport)) .AddUInt8((byte)(0xC0 | (byte)IIPPacketReport.ProgressReport))
@ -279,7 +279,7 @@ partial class DistributedConnection
//SendParams(, callbackId, value, max); //SendParams(, callbackId, value, max);
} }
void SendChunk(uint callbackId, object chunk) internal void SendChunk(uint callbackId, object chunk)
{ {
var c = Codec.Compose(chunk, this); var c = Codec.Compose(chunk, this);
SendParams() SendParams()
@ -353,7 +353,7 @@ partial class DistributedConnection
attachedResources.Remove(resourceId); attachedResources.Remove(resourceId);
} }
} }
else if (neededResources.Contains(resourceId)) else if (neededResources.Contains(resourceId))
{ {
@ -1472,6 +1472,8 @@ partial class DistributedConnection
object[] args = new object[pis.Length]; object[] args = new object[pis.Length];
InvocationContext context = null;
if (pis.Length > 0) if (pis.Length > 0)
{ {
if (pis.Last().ParameterType == typeof(DistributedConnection)) if (pis.Last().ParameterType == typeof(DistributedConnection))
@ -1480,16 +1482,33 @@ partial class DistributedConnection
{ {
if (arguments.ContainsKey(i)) if (arguments.ContainsKey(i))
args[i] = DC.CastConvert(arguments[i], pis[i].ParameterType); args[i] = DC.CastConvert(arguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable)// Nullable.GetUnderlyingType(pis[i].ParameterType) != null) else if (ft.Arguments[i].Type.Nullable)
args[i] = null; args[i] = null;
else else
args[i] = Type.Missing; args[i] = Type.Missing;
} }
//args[i] = arguments.ContainsKey(i) ?
// DC.CastConvert(arguments[i], pis[i].ParameterType) : Type.Missing;
args[args.Length - 1] = this; args[args.Length - 1] = this;
} }
else if (pis.Last().ParameterType == typeof(InvocationContext))
{
context = new InvocationContext(this, callback);
for (byte i = 0; i < pis.Length - 1; i++)
{
if (arguments.ContainsKey(i))
args[i] = DC.CastConvert(arguments[i], pis[i].ParameterType);
else if (ft.Arguments[i].Type.Nullable)
args[i] = null;
else
args[i] = Type.Missing;
}
args[args.Length - 1] = context;
}
else else
{ {
for (byte i = 0; i < pis.Length; i++) for (byte i = 0; i < pis.Length; i++)
@ -1509,6 +1528,7 @@ partial class DistributedConnection
try try
{ {
rt = ft.MethodInfo.Invoke(target, args); rt = ft.MethodInfo.Invoke(target, args);
context.Ended = true;
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -10,7 +10,6 @@ namespace Esiur.Resource;
public class ExportAttribute : Attribute public class ExportAttribute : Attribute
{ {
public string Name { get; private set; } = null; public string Name { get; private set; } = null;
public Type ReturnType { get; private set; } = null;
public ExportAttribute() public ExportAttribute()
{ {
@ -22,16 +21,4 @@ public class ExportAttribute : Attribute
Name = name; Name = name;
} }
public ExportAttribute(Type returnType)
{
ReturnType = returnType;
}
public ExportAttribute(string name, Type returnType)
{
Name = name;
ReturnType = returnType;
}
} }

View File

@ -91,17 +91,13 @@ public class FunctionTemplate : MemberTemplate
else if (genericRtType == typeof(IEnumerable<>) || genericRtType == typeof(IAsyncEnumerable<>)) else if (genericRtType == typeof(IEnumerable<>) || genericRtType == typeof(IAsyncEnumerable<>))
{ {
// get export // get export
rtType = RepresentationType.FromType(mi.GetCustomAttribute<ExportAttribute>()?.ReturnType ?? typeof(object)); rtType = RepresentationType.FromType(mi.ReturnType.GetGenericArguments()[0]);
} }
else else
{ {
rtType = RepresentationType.FromType(mi.ReturnType); 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");