121 lines
3.7 KiB
C#
Raw Normal View History

2020-04-11 15:48:02 +02:00
using Capnp.Util;
using System;
2019-06-12 21:56:55 +02:00
using System.Threading;
using System.Threading.Tasks;
namespace Capnp.Rpc
{
2019-06-12 21:56:55 +02:00
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
}
readonly StrictlyOrderedAwaitTask<Proxy>? _proxyTask;
2020-04-11 15:48:02 +02:00
readonly StrictlyOrderedAwaitTask<ConsumedCapability> _capTask;
2020-03-10 21:55:34 +01:00
public LazyCapability(Task<ConsumedCapability> capabilityTask)
2019-06-12 21:56:55 +02:00
{
2020-04-11 15:48:02 +02:00
_capTask = capabilityTask.EnforceAwaitOrder();
2019-06-12 21:56:55 +02:00
}
2020-03-10 21:55:34 +01:00
public LazyCapability(Task<Proxy> proxyTask)
{
_proxyTask = proxyTask.EnforceAwaitOrder();
2020-03-10 21:55:34 +01:00
async Task<ConsumedCapability> AwaitCap() => (await _proxyTask!).ConsumedCap;
2020-03-10 21:55:34 +01:00
2020-04-11 15:48:02 +02:00
_capTask = AwaitCap().EnforceAwaitOrder();
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
{
if (WhenResolved.IsCompleted && WhenResolved.WrappedTask.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 StrictlyOrderedAwaitTask WhenResolved => _capTask;
public T? GetResolvedCapability<T>() where T: class
{
2020-04-11 15:48:02 +02:00
if (_capTask.WrappedTask.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
{
ConsumedCapability cap;
2020-03-22 13:57:02 +01:00
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
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
}