libs.capnproto-dotnetcore_R.../Capnp.Net.Runtime/Rpc/ResolvingCapabilityExtensions.cs

99 lines
3.0 KiB
C#
Raw Normal View History

2020-03-10 21:55:34 +01:00
using System;
using System.Threading;
2020-03-10 21:55:34 +01:00
using System.Threading.Tasks;
namespace Capnp.Rpc
2019-06-12 21:56:55 +02:00
{
static class ResolvingCapabilityExtensions
{
public static async Task<ConsumedCapability> Unwrap(this ConsumedCapability cap)
2020-03-29 00:07:16 +01:00
{
while (cap is IResolvingCapability resolving)
{
await resolving.WhenResolved;
using var proxy = resolving.GetResolvedCapability<BareProxy>()!;
cap = proxy.ConsumedCap;
2020-03-29 00:07:16 +01:00
}
return cap;
}
public static Action? ExportAsSenderPromise<T>(this T cap, IRpcEndpoint endpoint, CapDescriptor.WRITER writer)
2019-06-12 21:56:55 +02:00
where T: ConsumedCapability, IResolvingCapability
{
var vine = cap.AsSkeleton();
2019-06-12 21:56:55 +02:00
uint preliminaryId = endpoint.AllocateExport(vine, out bool first);
writer.which = CapDescriptor.WHICH.SenderPromise;
writer.SenderPromise = preliminaryId;
if (first)
{
return async () => {
2019-06-12 21:56:55 +02:00
try
{
await cap.WhenResolved;
using var proxy = cap.GetResolvedCapability<BareProxy>()!;
var resolvedCap = await Unwrap(proxy.ConsumedCap);
2020-03-10 21:55:34 +01:00
endpoint.Resolve(preliminaryId, vine, () => resolvedCap!);
2019-06-12 21:56:55 +02:00
}
catch (System.Exception exception)
{
endpoint.Resolve(preliminaryId, vine, () => throw exception);
}
};
2019-06-12 21:56:55 +02:00
}
return null;
2019-06-12 21:56:55 +02:00
}
2020-03-22 00:12:50 +01:00
public static async Task<Proxy> AsProxyTask<T>(this Task<T> task)
where T: IDisposable?
2019-06-12 21:56:55 +02:00
{
2020-03-22 00:12:50 +01:00
IDisposable? obj;
try
{
obj = await task;
}
catch (TaskCanceledException exception)
{
var token = exception.CancellationToken;
if (!token.IsCancellationRequested)
token = new CancellationToken(true);
return new Proxy(LazyCapability.CreateCanceledCap(token));
2020-03-22 00:12:50 +01:00
}
catch (System.Exception exception)
{
return new Proxy(LazyCapability.CreateBrokenCap(exception.Message));
}
switch (obj)
{
case Proxy proxy: return proxy;
case null: return new Proxy(NullCapability.Instance);
default: return BareProxy.FromImpl(obj);
}
2019-06-12 21:56:55 +02:00
}
public static T? GetResolvedCapability<T>(this Task<Proxy> proxyTask) where T: class
{
if (proxyTask.IsCompleted)
{
try
{
return proxyTask.Result.Cast<T>(false);
}
catch (AggregateException exception)
{
throw exception.InnerException!;
}
}
else
{
return null;
}
}
2019-06-12 21:56:55 +02:00
}
2020-01-11 17:56:12 +01:00
}