Skip to content

Commit

Permalink
Working on #357
Browse files Browse the repository at this point in the history
  • Loading branch information
darkl committed Aug 22, 2023
1 parent 8fef06c commit fac1e63
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,29 @@ public async Task ProgressiveCallsCalleeProxyProgressValueTuples()
Assert.That(result, Is.EqualTo((10, "10")));
}

[Test]
public async Task ProgressiveCallsCalleeProxyProgressTask()
{
WampPlayground playground = new WampPlayground();

CallerCallee dualChannel = await playground.GetCallerCalleeDualChannel();
IWampChannel calleeChannel = dualChannel.CalleeChannel;
IWampChannel callerChannel = dualChannel.CallerChannel;

LongOpService myOperation = new LongOpService();

await calleeChannel.RealmProxy.Services.RegisterCallee(myOperation);
ILongOpService proxy = callerChannel.RealmProxy.Services.GetCalleeProxy<ILongOpService>();

List<int> results = new List<int>();
MyProgress<int> progress = new MyProgress<int>(i => results.Add(i));

await proxy.LongOpTask(10, progress);

CollectionAssert.AreEquivalent(Enumerable.Range(0, 10), results);
}



[Test]
public async Task ProgressiveCallsCalleeProxyObservable()
Expand Down Expand Up @@ -297,6 +320,9 @@ public interface ILongOpService
[WampProgressiveResultProcedure]
Task<(int, string)> LongOpValueTuple(int n, IProgress<(int a, int b)> progress);

[WampProcedure(MyOperation.ProcedureUri + "task")]
[WampProgressiveResultProcedure]
Task LongOpTask(int n, IProgress<int> progress);
}

public class LongOpService : ILongOpService
Expand All @@ -322,6 +348,15 @@ public async Task<string> LongOp(int n, IProgress<int> progress)

return (n, n.ToString());
}

public async Task LongOpTask(int n, IProgress<int> progress)
{
for (int i = 0; i < n; i++)
{
progress.Report(i);
await Task.Delay(100);
}
}
}

public interface ILongOpObservableService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static IOperationResultExtractor<TResult> GetResultExtractor<TResult>(Met
}
else
{
extractor = GetNonTupleExtractor<TResult>(method);
extractor = GetNonTupleExtractor<TResult>(method, method.HasReturnValue());
}

return extractor;
Expand All @@ -37,19 +37,18 @@ public static IOperationResultExtractor<TProgress> GetProgressExtractor<TProgres
}
else
{
extractor = GetNonTupleExtractor<TProgress>(method);
extractor = GetNonTupleExtractor<TProgress>(method, true);
}

return extractor;
}

private static IOperationResultExtractor<TResult> GetNonTupleExtractor<TResult>(MethodInfo method)
private static IOperationResultExtractor<TResult> GetNonTupleExtractor<TResult>(MethodInfo method, bool hasReturnValue)
{
IOperationResultExtractor<TResult> extractor;

if (!method.HasMultivaluedResult(typeof(TResult)))
{
bool hasReturnValue = method.HasReturnValue();
extractor = new SingleValueExtractor<TResult>(hasReturnValue);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ protected void CallResult(IWampRawRpcOperationRouterCallback caller, object resu
{
yieldOptions = yieldOptions ?? new YieldOptions();

object[] resultArguments = GetResultArguments(result);
object[] resultArguments = GetResultArguments(result, yieldOptions);

IDictionary<string, object> resultArgumentKeywords =
GetResultArgumentKeywords(result);
GetResultArgumentKeywords(result, yieldOptions);

CallResult(caller,
yieldOptions,
resultArguments,
resultArgumentKeywords);
}

protected virtual IDictionary<string, object> GetResultArgumentKeywords(object result)
protected virtual IDictionary<string, object> GetResultArgumentKeywords(
object result, YieldOptions yieldOptions)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public IWampCancellableInvocation Invoke<TMessage>(IWampRawRpcOperationRouterCal
return InnerInvoke(caller, formatter, details, arguments, argumentsKeywords);
}

protected virtual object[] GetResultArguments(object result)
protected virtual object[] GetResultArguments(object result, YieldOptions yieldOptions)
{
IWampResultExtractor extractor = WampResultExtractor.GetResultExtractor(this);

Expand All @@ -77,7 +77,7 @@ protected void CallResult(IWampRawRpcOperationRouterCallback caller, YieldOption
{
caller.Result(ObjectFormatter, options, arguments, argumentKeywords);
}
else if (!this.HasResult)
else if (options.Progress != true && !this.HasResult)
{
caller.Result(ObjectFormatter, options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ protected override Task<object> InvokeAsync<TMessage>(IWampRawRpcOperationRouter
}
}

protected override object[] GetResultArguments(object result)
protected override object[] GetResultArguments(object result, YieldOptions yieldOptions)
{
return mResultExtractor.GetArguments(result);
}

protected override IDictionary<string, object> GetResultArgumentKeywords(object result)
protected override IDictionary<string, object> GetResultArgumentKeywords(
object result, YieldOptions yieldOptions)
{
return mResultExtractor.GetArgumentKeywords(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace WampSharp.V2.Rpc
public class ProgressiveAsyncMethodInfoRpcOperation<TProgress, TResult> : AsyncMethodInfoRpcOperation
{
private readonly RpcParameter[] mRpcParameters;
private readonly IWampResultExtractor mProgressExtractor;

public ProgressiveAsyncMethodInfoRpcOperation(Func<object> instanceProvider, MethodInfo method, string procedureName) :
base(instanceProvider, method, procedureName)
Expand All @@ -23,6 +24,15 @@ public ProgressiveAsyncMethodInfoRpcOperation(Func<object> instanceProvider, Met
Array.Copy(baseParameters,
mRpcParameters,
mRpcParameters.Length);

mProgressExtractor = WampResultExtractor.GetResultExtractor(this, true);

ParameterInfo progressParameter = method.GetProgressParameter();

if (progressParameter.ParameterType.IsValueType)
{
mProgressExtractor = WampResultExtractor.GetValueTupleResultExtractor(progressParameter.ParameterType, progressParameter);
}
}

protected override object[] GetMethodParameters<TMessage>(IWampRawRpcOperationRouterCallback caller, CancellationToken cancellationToken, IWampFormatter<TMessage> formatter, TMessage[] arguments, IDictionary<string, TMessage> argumentsKeywords)
Expand All @@ -43,6 +53,30 @@ protected override object[] GetMethodParameters<TMessage>(IWampRawRpcOperationRo
return result;
}

protected override object[] GetResultArguments(object result, YieldOptions yieldOptions)
{
if (yieldOptions.Progress == true)
{
return mProgressExtractor.GetArguments(result);
}
else
{
return base.GetResultArguments(result, yieldOptions);
}
}

protected override IDictionary<string, object> GetResultArgumentKeywords(object result, YieldOptions yieldOptions)
{
if (yieldOptions.Progress == true)
{
return mProgressExtractor.GetArgumentKeywords(result);
}
else
{
return base.GetResultArgumentKeywords(result, yieldOptions);
}
}

public override RpcParameter[] Parameters => mRpcParameters;

private class CallerProgress : IProgress<TProgress>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,38 @@ public static IWampResultExtractor GetResultExtractor(LocalRpcOperation operatio
return extractor;
}

public static IWampResultExtractor GetResultExtractor(LocalRpcOperation operation, bool hasResult)
{
IWampResultExtractor extractor = new EmptyResultExtractor();

if (hasResult)
{
if (operation.CollectionResultTreatment == CollectionResultTreatment.SingleValue)
{
extractor = new SingleResultExtractor();
}
else
{
extractor = new MultiResultExtractor();
}
}

return extractor;
}

public static IWampResultExtractor GetValueTupleResultExtractor(MethodInfo method)
{
Type tupleType = TaskExtensions.UnwrapReturnType(method.ReturnType);

return GetValueTupleResultExtractor(tupleType, method.ReturnParameter);
}

public static IWampResultExtractor GetValueTupleResultExtractor(Type tupleType, ParameterInfo valueTupleParameter)
{
IWampResultExtractor result = new PositionalTupleExtractor(tupleType);

TupleElementNamesAttribute attribute =
method.ReturnParameter.GetCustomAttribute<TupleElementNamesAttribute>();
valueTupleParameter.GetCustomAttribute<TupleElementNamesAttribute>();

if (attribute != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override object InvokeSync<TMessage>
}
}

protected override object[] GetResultArguments(object result)
protected override object[] GetResultArguments(object result, YieldOptions yieldOptions)
{
return mResultExtractor.GetArguments(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected void HandleException(IWampRawRpcOperationRouterCallback caller, Except
protected void CallResult(IWampRawRpcOperationRouterCallback caller, object result, IDictionary<string, object> outputs, YieldOptions yieldOptions = null)
{
yieldOptions = yieldOptions ?? new YieldOptions();
object[] resultArguments = GetResultArguments(result);
object[] resultArguments = GetResultArguments(result, yieldOptions);

IDictionary<string, object> argumentKeywords =
GetResultArgumentKeywords(result, outputs);
Expand Down

0 comments on commit fac1e63

Please sign in to comment.