Skip to content

Commit 0e9bf5f

Browse files
committed
Allowing TProgress and TResult to be different
1 parent daaf1dd commit 0e9bf5f

12 files changed

+110
-57
lines changed

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/AsyncCalleeProxyInterceptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override object Invoke(MethodInfo method, object[] arguments)
2929
}
3030

3131
Task result =
32-
Handler.InvokeAsync<TResult>(Interceptor, method, Extractor, methodArguments, cancellationToken);
32+
Handler.InvokeAsync<TResult>(Interceptor, method, ResultExtractor, methodArguments, cancellationToken);
3333

3434
return result;
3535
}

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/Callbacks/Async/AsyncOperationCallback.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace WampSharp.V2.CalleeProxy
1111
internal class AsyncOperationCallback<TResult> : IWampRawRpcOperationClientCallback
1212
{
1313
private readonly TaskCompletionSource<TResult> mTask = new TaskCompletionSource<TResult>();
14-
private readonly IOperationResultExtractor<TResult> mExtractor;
14+
private readonly IOperationResultExtractor<TResult> mResultExtractor;
1515

16-
public AsyncOperationCallback(IOperationResultExtractor<TResult> extractor)
16+
public AsyncOperationCallback(IOperationResultExtractor<TResult> resultExtractor)
1717
{
18-
mExtractor = extractor;
18+
mResultExtractor = resultExtractor;
1919
}
2020

2121
public Task<TResult> Task => mTask.Task;
@@ -25,11 +25,6 @@ protected virtual void SetResult(ResultDetails details, TResult result)
2525
mTask.SetResult(result);
2626
}
2727

28-
protected virtual TResult GetResult<TMessage>(IWampFormatter<TMessage> formatter, TMessage[] arguments, IDictionary<string, TMessage> argumentsKeywords)
29-
{
30-
return mExtractor.GetResult(formatter, arguments, argumentsKeywords);
31-
}
32-
3328
public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details)
3429
{
3530
// TODO: throw exception if not nullable.
@@ -46,12 +41,26 @@ public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails d
4641
SetResult(details, formatter, arguments, argumentsKeywords);
4742
}
4843

49-
private void SetResult<TMessage>(ResultDetails details, IWampFormatter<TMessage> formatter, TMessage[] arguments, IDictionary<string, TMessage> argumentsKeywords = null)
44+
protected virtual void SetResult<TMessage>(ResultDetails details, IWampFormatter<TMessage> formatter, TMessage[] arguments, IDictionary<string, TMessage> argumentsKeywords = null)
45+
{
46+
void ReportAction(ResultDetails resultDetails, TResult result)
47+
{
48+
SetResult(resultDetails, result);
49+
}
50+
51+
SetResultValue(details, formatter, arguments, argumentsKeywords, mResultExtractor,
52+
ReportAction);
53+
}
54+
55+
protected void SetResultValue<TResultType, TMessage>(ResultDetails details, IWampFormatter<TMessage> formatter, TMessage[] arguments,
56+
IDictionary<string, TMessage> argumentsKeywords,
57+
IOperationResultExtractor<TResultType> extractor,
58+
Action<ResultDetails, TResultType> setResultAction)
5059
{
5160
try
5261
{
53-
TResult result = GetResult(formatter, arguments, argumentsKeywords);
54-
SetResult(details, result);
62+
TResultType result = extractor.GetResult(formatter, arguments, argumentsKeywords);
63+
setResultAction(details, result);
5564
}
5665
catch (Exception ex)
5766
{

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/Callbacks/Async/ProgressiveAsyncOperationCallback.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
using System;
2+
using System.Collections.Generic;
3+
using WampSharp.Core.Serialization;
24
using WampSharp.V2.Core.Contracts;
35

46
namespace WampSharp.V2.CalleeProxy
57
{
6-
class ProgressiveAsyncOperationCallback<TResult> : AsyncOperationCallback<TResult>
8+
class ProgressiveAsyncOperationCallback<TProgress, TResult> : AsyncOperationCallback<TResult>
79
{
8-
private readonly IProgress<TResult> mProgress;
10+
private readonly IProgress<TProgress> mProgress;
11+
private readonly IOperationResultExtractor<TProgress> mProgressTypeExtractor;
912

10-
public ProgressiveAsyncOperationCallback(IProgress<TResult> progress,
11-
IOperationResultExtractor<TResult> extractor) :
12-
base(extractor)
13+
public ProgressiveAsyncOperationCallback(IProgress<TProgress> progress,
14+
IOperationResultExtractor<TProgress> progressExtractor,
15+
IOperationResultExtractor<TResult> resultExtractor) :
16+
base(resultExtractor)
1317
{
18+
mProgressTypeExtractor = progressExtractor;
1419
mProgress = progress;
1520
}
1621

17-
protected override void SetResult(ResultDetails details, TResult result)
22+
protected override void SetResult<TMessage>(ResultDetails details, IWampFormatter<TMessage> formatter, TMessage[] arguments,
23+
IDictionary<string, TMessage> argumentsKeywords = null)
1824
{
1925
if (details.Progress == true)
2026
{
21-
mProgress.Report(result);
27+
void ReportAction(ResultDetails resultDetails, TProgress progressValue)
28+
{
29+
mProgress.Report(progressValue);
30+
}
31+
32+
base.SetResultValue(details,formatter,arguments,argumentsKeywords,
33+
mProgressTypeExtractor, ReportAction);
2234
}
2335
else
2436
{
25-
base.SetResult(details, result);
37+
base.SetResult(details, formatter, arguments, argumentsKeywords);
2638
}
2739
}
2840
}

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/Callbacks/Sync/SyncCallback.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ namespace WampSharp.V2.CalleeProxy
99
{
1010
internal class SyncCallback<TResult> : SyncCallbackBase
1111
{
12-
private readonly IOperationResultExtractor<TResult> mExtractor;
12+
private readonly IOperationResultExtractor<TResult> mResultExtractor;
1313
private readonly ILog mLogger;
1414
protected readonly MethodInfoHelper mMethodInfoHelper;
1515
private readonly object[] mArguments;
1616

17-
public SyncCallback(string procedureUri, MethodInfoHelper methodInfoHelper, object[] arguments, IOperationResultExtractor<TResult> extractor)
17+
public SyncCallback(string procedureUri, MethodInfoHelper methodInfoHelper, object[] arguments, IOperationResultExtractor<TResult> resultExtractor)
1818
{
1919
mMethodInfoHelper = methodInfoHelper;
2020
mLogger = LogProvider.GetLogger(typeof (SyncCallback<TResult>) + "." + procedureUri);
2121
mArguments = arguments;
22-
mExtractor = extractor;
22+
mResultExtractor = resultExtractor;
2323
}
2424

2525
public TResult OperationResult { get; private set; }
@@ -59,7 +59,7 @@ private void SetResult<TMessage>(IWampFormatter<TMessage> formatter, TMessage[]
5959
{
6060
try
6161
{
62-
TResult result = mExtractor.GetResult(formatter, arguments, argumentKeywords);
62+
TResult result = mResultExtractor.GetResult(formatter, arguments, argumentKeywords);
6363
SetResult(result);
6464
}
6565
catch (Exception ex)

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/Callbacks/WampCalleeProxyInvocationHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public Task<T> InvokeAsync<T>(ICalleeProxyInterceptor interceptor, MethodInfo me
5353
return task;
5454
}
5555

56-
public Task<T> InvokeProgressiveAsync<T>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor<T> extractor, object[] arguments, IProgress<T> progress, CancellationToken cancellationToken)
56+
public Task<TResult> InvokeProgressiveAsync<TProgress, TResult>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor<TProgress> progressExtractor, IOperationResultExtractor<TResult> resultExtractor, object[] arguments, IProgress<TProgress> progress, CancellationToken cancellationToken)
5757
{
58-
ProgressiveAsyncOperationCallback<T> asyncOperationCallback =
59-
new ProgressiveAsyncOperationCallback<T>(progress, extractor);
58+
ProgressiveAsyncOperationCallback<TProgress, TResult> asyncOperationCallback =
59+
new ProgressiveAsyncOperationCallback<TProgress, TResult>(progress, progressExtractor,resultExtractor);
6060

61-
Task<T> task = InnerInvokeAsync<T>(asyncOperationCallback, interceptor, method, arguments, cancellationToken);
61+
Task<TResult> task = InnerInvokeAsync(asyncOperationCallback, interceptor, method, arguments, cancellationToken);
6262

6363
return task;
6464
}

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/CalleeProxyBase.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CalleeProxyBase
1212
{
1313
protected delegate T InvokeSyncDelegate<T>(CalleeProxyBase proxy, params object[] arguments);
1414
protected delegate Task<T> InvokeAsyncDelegate<T>(CalleeProxyBase proxy, CancellationToken cancellationToken, params object[] arguments);
15-
protected delegate Task<T> InvokeProgressiveAsyncDelegate<T>(CalleeProxyBase proxy, IProgress<T> progress, CancellationToken cancellationToken, params object[] arguments);
15+
protected delegate Task<TResult> InvokeProgressiveAsyncDelegate<TProgress, TResult>(CalleeProxyBase proxy, IProgress<TProgress> progress, CancellationToken cancellationToken, params object[] arguments);
1616

1717
private readonly WampCalleeProxyInvocationHandler mHandler;
1818
private readonly ICalleeProxyInterceptor mInterceptor;
@@ -36,10 +36,11 @@ protected static InvokeAsyncDelegate<T> GetInvokeAsync<T>(MethodInfo method)
3636
return GetInvokeAsync(method, extractor);
3737
}
3838

39-
protected static InvokeProgressiveAsyncDelegate<T> GetInvokeProgressiveAsync<T>(MethodInfo method)
39+
protected static InvokeProgressiveAsyncDelegate<TProgress, TResult> GetInvokeProgressiveAsync<TProgress, TResult>(MethodInfo method)
4040
{
41-
IOperationResultExtractor<T> extractor = GetExtractor<T>(method);
42-
return GetInvokeProgressiveAsync(method, extractor);
41+
IOperationResultExtractor<TResult> resultExtractor = GetExtractor<TResult>(method);
42+
IOperationResultExtractor<TProgress> progressExtractor = GetExtractor<TProgress>(method);
43+
return GetInvokeProgressiveAsync(method, progressExtractor, resultExtractor);
4344
}
4445

4546
private static IOperationResultExtractor<T> GetExtractor<T>(MethodInfo method)
@@ -66,13 +67,14 @@ private static InvokeAsyncDelegate<T> GetInvokeAsync<T>(MethodBase method, IOper
6667
return result;
6768
}
6869

69-
private static InvokeProgressiveAsyncDelegate<T>
70-
GetInvokeProgressiveAsync<T>(MethodBase method,
71-
IOperationResultExtractor<T> extractor)
70+
private static InvokeProgressiveAsyncDelegate<TProgress, TResult>
71+
GetInvokeProgressiveAsync<TProgress, TResult>(MethodBase method,
72+
IOperationResultExtractor<TProgress> progressExtractor,
73+
IOperationResultExtractor<TResult> resultExtractor)
7274
{
73-
InvokeProgressiveAsyncDelegate<T> result =
75+
InvokeProgressiveAsyncDelegate<TProgress, TResult> result =
7476
(proxy, progress, cancellationToken, arguments) =>
75-
proxy.InvokeProgressiveAsync(method, progress, cancellationToken, extractor, arguments);
77+
proxy.InvokeProgressiveAsync(method, progress, cancellationToken, progressExtractor, resultExtractor, arguments);
7678

7779
return result;
7880
}
@@ -107,16 +109,16 @@ protected Task<T[]> MultiInvokeAsync<T>(MethodBase method, params object[] argum
107109
return InvokeAsync(method, new MultiValueExtractor<T>(), arguments);
108110
}
109111

110-
protected Task<T> SingleInvokeProgressiveAsync<T>(MethodBase method, IProgress<T> progress,
112+
protected Task<TResult> SingleInvokeProgressiveAsync<TProgress, TResult>(MethodBase method, IProgress<TProgress> progress,
111113
params object[] arguments)
112114
{
113-
return InvokeProgressiveAsync<T>(method, progress, new SingleValueExtractor<T>(true), arguments);
115+
return InvokeProgressiveAsync<TProgress, TResult>(method, progress, new SingleValueExtractor<TProgress>(true), new SingleValueExtractor<TResult>(true), arguments);
114116
}
115117

116-
protected Task<T[]> MultiInvokeProgressiveAsync<T>(MethodBase method, IProgress<T[]> progress,
118+
protected Task<TResult[]> MultiInvokeProgressiveAsync<TProgress, TResult>(MethodBase method, IProgress<TProgress[]> progress,
117119
params object[] arguments)
118120
{
119-
return InvokeProgressiveAsync<T[]>(method, progress, new MultiValueExtractor<T>(), arguments);
121+
return InvokeProgressiveAsync<TProgress[], TResult[]>(method, progress, new MultiValueExtractor<TProgress>(), new MultiValueExtractor<TResult>(), arguments);
120122
}
121123

122124
private T InvokeSync<T>(MethodBase method,
@@ -154,32 +156,36 @@ private Task<T> InvokeAsync<T>(MethodBase method,
154156
cancellationToken);
155157
}
156158

157-
private Task<T> InvokeProgressiveAsync<T>
159+
private Task<TResult> InvokeProgressiveAsync<TProgress, TResult>
158160
(MethodBase method,
159-
IProgress<T> progress,
160-
IOperationResultExtractor<T> resultExtractor,
161+
IProgress<TProgress> progress,
162+
IOperationResultExtractor<TProgress> progressExtractor,
163+
IOperationResultExtractor<TResult> resultExtractor,
161164
params object[] arguments)
162165
{
163-
return InvokeProgressiveAsync<T>
166+
return InvokeProgressiveAsync
164167
(method,
165168
progress,
166169
CancellationToken.None,
170+
progressExtractor,
167171
resultExtractor,
168172
arguments);
169173
}
170174

171-
private Task<T> InvokeProgressiveAsync<T>
175+
private Task<TResult> InvokeProgressiveAsync<TProgress, TResult>
172176
(MethodBase method,
173-
IProgress<T> progress,
177+
IProgress<TProgress> progress,
174178
CancellationToken cancellationToken,
175-
IOperationResultExtractor<T> resultExtractor,
179+
IOperationResultExtractor<TProgress> progressExtractor,
180+
IOperationResultExtractor<TResult> resultExtractor,
176181
params object[] arguments)
177182
{
178183
MethodInfo methodInfo = (MethodInfo) method;
179184

180185
return mHandler.InvokeProgressiveAsync
181186
(mInterceptor,
182187
methodInfo,
188+
progressExtractor,
183189
resultExtractor,
184190
arguments,
185191
progress,

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/CalleeProxyInterceptorBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public CalleeProxyInterceptorBase(MethodInfo method, IWampCalleeProxyInvocationH
2828
ICalleeProxyInterceptor interceptor)
2929
: base(method, handler, interceptor)
3030
{
31-
Extractor = OperationResultExtractor.Get<TResult>(method);
31+
ResultExtractor = OperationResultExtractor.Get<TResult>(method);
3232
}
3333

34-
public IOperationResultExtractor<TResult> Extractor { get; }
34+
public IOperationResultExtractor<TResult> ResultExtractor { get; }
3535
}
3636
}

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/CalleeProxyInterceptorFactory.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Linq;
23
using System.Reactive;
34
using System.Reflection;
5+
using System.Threading;
46
using System.Threading.Tasks;
57
using WampSharp.V2.Rpc;
68
using TaskExtensions = WampSharp.Core.Utilities.TaskExtensions;
@@ -50,7 +52,10 @@ private static Type GetRelevantInterceptorType(MethodInfo method)
5052
if (method.IsDefined(typeof(WampProgressiveResultProcedureAttribute)))
5153
{
5254
MethodInfoValidation.ValidateProgressiveMethod(method);
53-
interceptorType = typeof(ProgressiveAsyncCalleeProxyInterceptor<>);
55+
interceptorType = typeof(ProgressiveAsyncCalleeProxyInterceptor<,>);
56+
Type processGenericArgument = GetProgressType(method);
57+
Type result = interceptorType.MakeGenericType(processGenericArgument, genericArgument);
58+
return result;
5459
}
5560
else
5661
{
@@ -62,5 +67,22 @@ private static Type GetRelevantInterceptorType(MethodInfo method)
6267
Type closedGenericType = interceptorType.MakeGenericType(genericArgument);
6368
return closedGenericType;
6469
}
70+
71+
private static Type GetProgressType(MethodInfo method)
72+
{
73+
ParameterInfo[] parameters = method.GetParameters();
74+
ParameterInfo lastParameter = parameters.LastOrDefault();
75+
ParameterInfo progressParameter = lastParameter;
76+
77+
if ((lastParameter != null) &&
78+
(lastParameter.ParameterType == typeof(CancellationToken)))
79+
{
80+
progressParameter =
81+
parameters.Take(parameters.Length - 1).LastOrDefault();
82+
}
83+
84+
return progressParameter.ParameterType.GetGenericArguments()[0];
85+
}
86+
6587
}
6688
}

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/IWampCalleeProxyInvocationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ internal interface IWampCalleeProxyInvocationHandler
99
{
1010
T Invoke<T>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor<T> extractor, object[] arguments);
1111
Task<T> InvokeAsync<T>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor<T> extractor, object[] arguments, CancellationToken cancellationToken);
12-
Task<T> InvokeProgressiveAsync<T>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor<T> extractor, object[] arguments, IProgress<T> progress, CancellationToken cancellationToken);
12+
Task<TResult> InvokeProgressiveAsync<TProgress,TResult>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor<TProgress> progressExtractor, IOperationResultExtractor<TResult> resultExtractor, object[] arguments, IProgress<TProgress> progress, CancellationToken cancellationToken);
1313
}
1414
}

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/ObservableCalleeProxyInterceptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public override object Invoke(MethodInfo method, object[] arguments)
1919
return Observable.Create<T>(async (observer, cancellationToken) =>
2020
{
2121
T last = await Handler.InvokeProgressiveAsync
22-
(Interceptor, method, Extractor, arguments, observer.ToProgress(),
22+
(Interceptor, method, ResultExtractor, ResultExtractor, arguments, observer.ToProgress(),
2323
cancellationToken);
2424

2525
if (last != null)

0 commit comments

Comments
 (0)