-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathResponseRouter.cs
117 lines (98 loc) · 4.74 KB
/
ResponseRouter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc.Client;
namespace OmniSharp.Extensions.JsonRpc
{
internal class ResponseRouter : IResponseRouter
{
internal readonly Lazy<IOutputHandler> OutputHandler;
internal readonly ISerializer Serializer;
private readonly IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor?> _handlerTypeDescriptorProvider;
internal readonly ConcurrentDictionary<long, (string method, TaskCompletionSource<JToken> pendingTask)> Requests =
new ConcurrentDictionary<long, (string method, TaskCompletionSource<JToken> pendingTask)>();
public ResponseRouter(Lazy<IOutputHandler> outputHandler, ISerializer serializer, IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor?> handlerTypeDescriptorProvider)
{
OutputHandler = outputHandler;
Serializer = serializer;
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
}
public void SendNotification(string method) =>
OutputHandler.Value.Send(
new OutgoingNotification {
Method = method
}
);
public void SendNotification<T>(string method, T @params) =>
OutputHandler.Value.Send(
new OutgoingNotification {
Method = method,
Params = @params
}
);
public void SendNotification(IRequest @params) => SendNotification(GetMethodName(@params.GetType()), @params);
public Task<TResponse> SendRequest<TResponse>(IRequest<TResponse> @params, CancellationToken cancellationToken) =>
SendRequest(GetMethodName(@params.GetType()), @params).Returning<TResponse>(cancellationToken);
public IResponseRouterReturns SendRequest(string method) => new ResponseRouterReturnsImpl(this, method, new object());
public IResponseRouterReturns SendRequest<T>(string method, T @params) => new ResponseRouterReturnsImpl(this, method, @params);
public bool TryGetRequest(long id, [NotNullWhen(true)] out string method, [NotNullWhen(true)] out TaskCompletionSource<JToken> pendingTask)
{
var result = Requests.TryGetValue(id, out var source);
method = source.method;
pendingTask = source.pendingTask;
return result;
}
private string GetMethodName(Type type) =>
_handlerTypeDescriptorProvider.GetMethodName(type) ?? throw new NotSupportedException($"Unable to infer method name for type {type.FullName}");
private class ResponseRouterReturnsImpl : IResponseRouterReturns
{
private readonly ResponseRouter _router;
private readonly string _method;
private readonly object? _params;
public ResponseRouterReturnsImpl(ResponseRouter router, string method, object? @params)
{
_router = router;
_method = method;
_params = @params;
}
public async Task<TResponse> Returning<TResponse>(CancellationToken cancellationToken)
{
var nextId = _router.Serializer.GetNextId();
var tcs = new TaskCompletionSource<JToken>();
_router.Requests.TryAdd(nextId, ( _method, tcs ));
cancellationToken.ThrowIfCancellationRequested();
try
{
_router.OutputHandler.Value.Send(
new OutgoingRequest {
Method = _method,
Params = _params,
Id = nextId
}
);
cancellationToken.Register(
() => {
if (tcs.Task.IsCompleted) return;
_router.CancelRequest(new CancelParams { Id = nextId });
}
);
var result = await tcs.Task.ConfigureAwait(false);
if (typeof(TResponse) == typeof(Unit))
{
return (TResponse) (object) Unit.Value;
}
return result.ToObject<TResponse>(_router.Serializer.JsonSerializer);
}
finally
{
_router.Requests.TryRemove(nextId, out _);
}
}
public async Task ReturningVoid(CancellationToken cancellationToken) => await Returning<Unit>(cancellationToken).ConfigureAwait(false);
}
}
}