-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathDapResponseRouter.cs
137 lines (115 loc) · 5.09 KB
/
DapResponseRouter.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
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.DebugAdapter.Protocol.Requests;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.JsonRpc.Client;
namespace OmniSharp.Extensions.DebugAdapter.Shared
{
public class DapResponseRouter : IResponseRouter
{
internal readonly IOutputHandler OutputHandler;
internal readonly ISerializer Serializer;
internal readonly ConcurrentDictionary<long, (string method, TaskCompletionSource<JToken> pendingTask)> Requests =
new ConcurrentDictionary<long, (string method, TaskCompletionSource<JToken> pendingTask)>();
internal static readonly ConcurrentDictionary<Type, string> MethodCache =
new ConcurrentDictionary<Type, string>();
public DapResponseRouter(IOutputHandler outputHandler, ISerializer serializer)
{
OutputHandler = outputHandler;
Serializer = serializer;
}
public void SendNotification(string method) =>
OutputHandler.Send(
new OutgoingNotification {
Method = method
}
);
public void SendNotification<T>(string method, T @params) =>
OutputHandler.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)
{
if (!MethodCache.TryGetValue(type, out var methodName))
{
var attribute = MethodAttribute.From(type);
if (attribute == null)
{
throw new NotSupportedException($"Unable to infer method name for type {type.FullName}");
}
methodName = attribute.Method;
MethodCache.TryAdd(type, methodName);
}
return methodName;
}
private class ResponseRouterReturnsImpl : IResponseRouterReturns
{
private readonly DapResponseRouter _router;
private readonly string _method;
private readonly object _params;
public ResponseRouterReturnsImpl(DapResponseRouter 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();
_router.OutputHandler.Send(
new OutgoingRequest {
Method = _method,
Params = _params,
Id = nextId
}
);
if (_method != RequestNames.Cancel)
{
cancellationToken.Register(
() => {
if (tcs.Task.IsCompleted) return;
_router.SendRequest(RequestNames.Cancel, new { requestId = nextId }).Returning<CancelArguments>(CancellationToken.None);
}
);
}
try
{
var result = await tcs.Task;
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);
}
}
}