-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathResponseRouter.cs
151 lines (130 loc) · 5.3 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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)
{
return SendRequest(GetMethodName(@params.GetType()), @params).Returning<TResponse>(cancellationToken);
}
public IResponseRouterReturns SendRequest(string method)
{
return new ResponseRouterReturnsImpl(this, method, new object());
}
public IResponseRouterReturns SendRequest<T>(string method, T @params)
{
return 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)
{
return _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 ));
try
{
cancellationToken.ThrowIfCancellationRequested();
_router.OutputHandler.Value.Send(
new OutgoingRequest
{
Method = _method,
Params = _params,
Id = nextId
}
);
cancellationToken.Register(
() =>
{
if (tcs.Task.IsCompleted) return;
if (cancellationToken.IsCancellationRequested)
{
tcs.SetCanceled();
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);
}
}
}
}