165 lines
5.3 KiB
C#
Raw Normal View History

2019-06-12 21:56:55 +02:00
using System;
2020-03-10 21:55:34 +01:00
using System.Collections.Generic;
2019-06-12 21:56:55 +02:00
using System.Threading;
using System.Threading.Tasks;
namespace Capnp.Rpc
{
class PendingAnswer: IDisposable
{
2020-01-11 17:21:31 +01:00
readonly CancellationTokenSource? _cts;
2020-02-22 13:46:03 +01:00
readonly TaskCompletionSource<AnswerOrCounterquestion> _cancelCompleter;
readonly Task<AnswerOrCounterquestion> _answerTask;
2019-06-12 21:56:55 +02:00
2020-01-11 17:21:31 +01:00
public PendingAnswer(Task<AnswerOrCounterquestion> callTask, CancellationTokenSource? cts)
2019-06-12 21:56:55 +02:00
{
2020-02-22 13:46:03 +01:00
async Task<AnswerOrCounterquestion> CancelableAwaitWhenReady()
2019-06-12 21:56:55 +02:00
{
2020-02-22 13:46:03 +01:00
return await await Task.WhenAny(callTask, _cancelCompleter.Task);
2019-06-12 21:56:55 +02:00
}
2020-02-22 13:46:03 +01:00
if (callTask == null)
throw new ArgumentNullException(nameof(callTask));
2019-06-12 21:56:55 +02:00
2020-02-22 13:46:03 +01:00
_cts = cts;
_cancelCompleter = new TaskCompletionSource<AnswerOrCounterquestion>();
_answerTask = CancelableAwaitWhenReady();
Chain(async t =>
{
var aorcq = default(AnswerOrCounterquestion);
try
{
aorcq = await t;
}
catch
{
}
if (aorcq.Answer != null)
{
if (aorcq.Answer.Caps != null)
{
foreach (var cap in aorcq.Answer.Caps)
{
cap?.AddRef();
}
}
}
});
2019-06-12 21:56:55 +02:00
}
2020-02-22 13:46:03 +01:00
public CancellationToken CancellationToken => _cts?.Token ?? CancellationToken.None;
public IReadOnlyList<CapDescriptor.WRITER>? CapTable { get; set; }
2020-03-10 21:55:34 +01:00
2020-02-22 13:46:03 +01:00
public void Cancel()
2019-06-12 21:56:55 +02:00
{
2020-02-22 13:46:03 +01:00
_cts?.Cancel();
_cancelCompleter.SetCanceled();
2019-06-12 21:56:55 +02:00
}
2020-02-22 13:46:03 +01:00
public void Chain(Action<Task<AnswerOrCounterquestion>> func)
2019-06-12 21:56:55 +02:00
{
2020-02-22 13:46:03 +01:00
func(_answerTask);
2019-06-12 21:56:55 +02:00
}
2020-02-22 13:46:03 +01:00
public void Chain(PromisedAnswer.READER rd, Action<Task<Proxy>> func)
2019-06-12 21:56:55 +02:00
{
2020-02-22 13:46:03 +01:00
Chain(t =>
2019-06-12 21:56:55 +02:00
{
async Task<Proxy> EvaluateProxy()
{
var aorcq = await t;
if (aorcq.Answer != null)
{
DeserializerState cur = aorcq.Answer;
foreach (var op in rd.Transform)
{
switch (op.which)
{
case PromisedAnswer.Op.WHICH.GetPointerField:
try
{
cur = cur.StructReadPointer(op.GetPointerField);
}
catch (System.Exception)
{
throw new ArgumentOutOfRangeException("Illegal pointer field in transformation operation");
}
break;
case PromisedAnswer.Op.WHICH.Noop:
break;
default:
throw new ArgumentOutOfRangeException("Unknown transformation operation");
}
}
Proxy proxy;
switch (cur.Kind)
{
case ObjectKind.Capability:
try
{
2020-01-11 17:21:31 +01:00
var cap = aorcq.Answer.Caps![(int)cur.CapabilityIndex];
2019-06-12 21:56:55 +02:00
proxy = new Proxy(cap ?? LazyCapability.Null);
}
catch (ArgumentOutOfRangeException)
{
throw new ArgumentOutOfRangeException("Bad capability table in internal answer - internal error?");
}
return proxy;
default:
throw new ArgumentOutOfRangeException("Transformation did not result in a capability");
}
}
else
{
var path = MemberAccessPath.Deserialize(rd);
var cap = new RemoteAnswerCapability(aorcq.Counterquestion!, path);
2019-06-12 21:56:55 +02:00
return new Proxy(cap);
}
}
2020-02-22 13:46:03 +01:00
func(EvaluateProxy());
2019-06-12 21:56:55 +02:00
});
}
2020-02-22 13:46:03 +01:00
public void Dispose()
2019-06-12 21:56:55 +02:00
{
2020-02-22 13:46:03 +01:00
_cts?.Dispose();
Chain(async t =>
{
AnswerOrCounterquestion aorcq;
try
{
aorcq = await t;
}
catch
{
return;
}
if (aorcq.Answer != null)
{
if (aorcq.Answer.Caps != null)
{
foreach (var cap in aorcq.Answer.Caps)
{
cap?.Release();
}
}
}
});
2019-06-12 21:56:55 +02:00
}
}
2020-01-11 17:56:12 +01:00
}