125 lines
3.6 KiB
C#
Raw Normal View History

2019-06-12 21:56:55 +02:00
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Capnp.Rpc
{
class LazyCapability : RefCountingCapability, IResolvingCapability
{
public static LazyCapability CreateBrokenCap(string message)
{
return new LazyCapability(Task.FromException<ConsumedCapability?>(new RpcException(message)));
2019-06-12 21:56:55 +02:00
}
public static LazyCapability CreateCanceledCap(CancellationToken token)
{
return new LazyCapability(Task.FromCanceled<ConsumedCapability?>(token));
2019-06-12 21:56:55 +02:00
}
2020-03-10 21:55:34 +01:00
readonly Task<Proxy>? _proxyTask;
readonly Task<ConsumedCapability?> _capTask;
2020-03-10 21:55:34 +01:00
public LazyCapability(Task<ConsumedCapability?> capabilityTask)
2019-06-12 21:56:55 +02:00
{
_capTask = capabilityTask;
2019-06-12 21:56:55 +02:00
}
2020-03-10 21:55:34 +01:00
public LazyCapability(Task<Proxy> proxyTask)
{
_proxyTask = proxyTask;
async Task<ConsumedCapability?> AwaitCap() => (await _proxyTask!).ConsumedCap;
_capTask = AwaitCap();
2020-03-10 21:55:34 +01:00
}
internal override Action? Export(IRpcEndpoint endpoint, CapDescriptor.WRITER writer)
2019-06-12 21:56:55 +02:00
{
2019-06-22 18:43:30 -04:00
if (WhenResolved.ReplacementTaskIsCompletedSuccessfully())
2019-06-12 21:56:55 +02:00
{
using var proxy = GetResolvedCapability<BareProxy>()!;
2020-03-29 00:07:16 +01:00
return proxy.Export(endpoint, writer);
2019-06-12 21:56:55 +02:00
}
else
{
return this.ExportAsSenderPromise(endpoint, writer);
2019-06-12 21:56:55 +02:00
}
}
2020-03-10 21:55:34 +01:00
protected override void ReleaseRemotely()
2019-06-12 21:56:55 +02:00
{
2020-03-10 21:55:34 +01:00
if (_proxyTask != null)
2019-06-12 21:56:55 +02:00
{
2020-03-10 21:55:34 +01:00
async void DisposeProxyWhenResolved()
{
try { using var _ = await _proxyTask!; }
catch { }
}
2019-06-12 21:56:55 +02:00
2020-03-10 21:55:34 +01:00
DisposeProxyWhenResolved();
}
2019-06-12 21:56:55 +02:00
}
public Task WhenResolved => _capTask;
public T? GetResolvedCapability<T>() where T: class
{
if (_capTask.IsCompleted)
{
try
{
return CapabilityReflection.CreateProxy<T>(_capTask.Result) as T;
}
catch (AggregateException exception)
{
throw exception.InnerException!;
}
}
else
{
return null;
}
}
2019-06-12 21:56:55 +02:00
async Task<DeserializerState> CallImpl(ulong interfaceId, ushort methodId, DynamicSerializerState args, CancellationToken cancellationToken)
2019-06-12 21:56:55 +02:00
{
2020-03-22 13:57:02 +01:00
ConsumedCapability? cap;
try
{
cap = await _capTask;
2020-03-22 13:57:02 +01:00
}
catch
{
args.Dispose();
throw;
}
2019-06-12 21:56:55 +02:00
2020-03-22 13:57:02 +01:00
if (cancellationToken.IsCancellationRequested)
{
args.Dispose();
cancellationToken.ThrowIfCancellationRequested();
}
2019-06-12 21:56:55 +02:00
if (cap == null)
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
using var proxy = new Proxy(cap);
var call = proxy.Call(interfaceId, methodId, args, default);
2019-06-12 21:56:55 +02:00
var whenReturned = call.WhenReturned;
using (var registration = cancellationToken.Register(call.Dispose))
{
return await whenReturned;
}
}
internal override IPromisedAnswer DoCall(ulong interfaceId, ushort methodId, DynamicSerializerState args)
2019-06-12 21:56:55 +02:00
{
var cts = new CancellationTokenSource();
return new LocalAnswer(cts, CallImpl(interfaceId, methodId, args, cts.Token));
2019-06-12 21:56:55 +02:00
}
}
2020-01-11 17:56:12 +01:00
}