2019-06-12 21:56:55 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Capnp.Rpc
|
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
|
2019-06-12 21:56:55 +02:00
|
|
|
|
class LocalAnswerCapability : RefCountingCapability, IResolvingCapability
|
|
|
|
|
{
|
2020-03-22 00:12:50 +01:00
|
|
|
|
static async Task<Proxy> TransferOwnershipToDummyProxy(Task<DeserializerState> answer, MemberAccessPath access)
|
|
|
|
|
{
|
|
|
|
|
var result = await answer;
|
|
|
|
|
var cap = access.Eval(result);
|
|
|
|
|
var proxy = new Proxy(cap);
|
2020-03-22 16:08:28 +01:00
|
|
|
|
cap?.Release();
|
2020-03-22 00:12:50 +01:00
|
|
|
|
return proxy;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
readonly Task<Proxy> _whenResolvedProxy;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
public LocalAnswerCapability(Task<Proxy> proxyTask)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
_whenResolvedProxy = proxyTask;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 00:12:50 +01:00
|
|
|
|
public LocalAnswerCapability(Task<DeserializerState> answer, MemberAccessPath access):
|
|
|
|
|
this(TransferOwnershipToDummyProxy(answer, access))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 17:21:31 +01:00
|
|
|
|
internal override void Freeze(out IRpcEndpoint? boundEndpoint)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
boundEndpoint = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal override void Unfreeze()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-03 22:10:40 +02:00
|
|
|
|
public Task WhenResolved => _whenResolvedProxy;
|
|
|
|
|
|
|
|
|
|
public T? GetResolvedCapability<T>() where T : class => _whenResolvedProxy.GetResolvedCapability<T>();
|
2019-06-12 21:56:55 +02:00
|
|
|
|
|
2020-03-21 13:27:46 +01:00
|
|
|
|
internal override Action? Export(IRpcEndpoint endpoint, CapDescriptor.WRITER writer)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
if (_whenResolvedProxy.IsCompleted)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
_whenResolvedProxy.Result.Export(endpoint, writer);
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
catch (AggregateException exception)
|
|
|
|
|
{
|
2020-03-21 13:27:46 +01:00
|
|
|
|
throw exception.InnerException!;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
2020-03-21 13:27:46 +01:00
|
|
|
|
|
|
|
|
|
return null;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-21 13:27:46 +01:00
|
|
|
|
return this.ExportAsSenderPromise(endpoint, writer);
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 18:48:25 +01:00
|
|
|
|
async Task<DeserializerState> CallImpl(ulong interfaceId, ushort methodId, DynamicSerializerState args, CancellationToken cancellationToken)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
var proxy = await _whenResolvedProxy;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
if (proxy.IsNull)
|
2020-03-22 13:57:02 +01:00
|
|
|
|
{
|
|
|
|
|
args.Dispose();
|
2019-06-12 21:56:55 +02:00
|
|
|
|
throw new RpcException("Broken capability");
|
2020-03-22 13:57:02 +01:00
|
|
|
|
}
|
2019-06-12 21:56:55 +02:00
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
var call = proxy.Call(interfaceId, methodId, args, default);
|
2019-06-12 21:56:55 +02:00
|
|
|
|
var whenReturned = call.WhenReturned;
|
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
using var registration = cancellationToken.Register(() => call.Dispose());
|
|
|
|
|
return await whenReturned;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 18:48:25 +01:00
|
|
|
|
internal override IPromisedAnswer DoCall(ulong interfaceId, ushort methodId, DynamicSerializerState args)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
var cts = new CancellationTokenSource();
|
2019-11-06 18:48:25 +01:00
|
|
|
|
return new LocalAnswer(cts, CallImpl(interfaceId, methodId, args, cts.Token));
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
protected async override void ReleaseRemotely()
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
try { using var _ = await _whenResolvedProxy; }
|
|
|
|
|
catch { }
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-11 17:56:12 +01:00
|
|
|
|
}
|