2019-06-12 21:56:55 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Capnp.Rpc
|
|
|
|
|
{
|
|
|
|
|
class LocalCapability : ConsumedCapability
|
|
|
|
|
{
|
|
|
|
|
static async Task<DeserializerState> AwaitAnswer(Task<AnswerOrCounterquestion> call)
|
|
|
|
|
{
|
|
|
|
|
var aorcq = await call;
|
2020-01-11 17:21:31 +01:00
|
|
|
|
return aorcq.Answer ?? await aorcq.Counterquestion!.WhenReturned;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Skeleton ProvidedCap { get; }
|
|
|
|
|
|
2020-04-10 18:29:06 +02:00
|
|
|
|
internal override Skeleton AsSkeleton() => ProvidedCap;
|
|
|
|
|
|
|
|
|
|
public LocalCapability(Skeleton providedCap)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
ProvidedCap = providedCap ?? throw new ArgumentNullException(nameof(providedCap));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal override void AddRef()
|
|
|
|
|
{
|
2020-03-22 16:08:28 +01:00
|
|
|
|
ProvidedCap.Claim();
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 16:08:28 +01:00
|
|
|
|
internal override void Release()
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
2020-03-22 16:08:28 +01:00
|
|
|
|
ProvidedCap.Relinquish();
|
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();
|
|
|
|
|
var call = ProvidedCap.Invoke(interfaceId, methodId, args, cts.Token);
|
|
|
|
|
return new LocalAnswer(cts, AwaitAnswer(call));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 13:27:46 +01:00
|
|
|
|
internal override Action? Export(IRpcEndpoint endpoint, CapDescriptor.WRITER capDesc)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
capDesc.which = CapDescriptor.WHICH.SenderHosted;
|
|
|
|
|
capDesc.SenderHosted = endpoint.AllocateExport(ProvidedCap, out bool _);
|
2020-03-21 13:27:46 +01:00
|
|
|
|
return null;
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void ReleaseRemotely()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-11 17:56:12 +01:00
|
|
|
|
}
|