@@ -10,28 +10,31 @@ namespace OmniSharp.Extensions.JsonRpc
10
10
{
11
11
public class ResponseRouter : IResponseRouter
12
12
{
13
- private readonly IOutputHandler _outputHandler ;
14
- private readonly ISerializer _serializer ;
15
- private readonly object _lock = new object ( ) ;
16
- private readonly ConcurrentDictionary < long , TaskCompletionSource < JToken > > _requests = new ConcurrentDictionary < long , TaskCompletionSource < JToken > > ( ) ;
17
- private static readonly ConcurrentDictionary < Type , string > _methodCache = new ConcurrentDictionary < Type , string > ( ) ;
13
+ internal readonly IOutputHandler OutputHandler ;
14
+ internal readonly ISerializer Serializer ;
15
+
16
+ internal readonly ConcurrentDictionary < long , TaskCompletionSource < JToken > > Requests =
17
+ new ConcurrentDictionary < long , TaskCompletionSource < JToken > > ( ) ;
18
+
19
+ internal static readonly ConcurrentDictionary < Type , string > MethodCache =
20
+ new ConcurrentDictionary < Type , string > ( ) ;
18
21
19
22
public ResponseRouter ( IOutputHandler outputHandler , ISerializer serializer )
20
23
{
21
- _outputHandler = outputHandler ;
22
- _serializer = serializer ;
24
+ OutputHandler = outputHandler ;
25
+ Serializer = serializer ;
23
26
}
24
27
25
28
public void SendNotification ( string method )
26
29
{
27
- _outputHandler . Send ( new Client . Notification ( ) {
30
+ OutputHandler . Send ( new Client . Notification ( ) {
28
31
Method = method
29
32
} , CancellationToken . None ) ;
30
33
}
31
34
32
35
public void SendNotification < T > ( string method , T @params )
33
36
{
34
- _outputHandler . Send ( new Client . Notification ( ) {
37
+ OutputHandler . Send ( new Client . Notification ( ) {
35
38
Method = method ,
36
39
Params = @params
37
40
} , CancellationToken . None ) ;
@@ -42,100 +45,30 @@ public void SendNotification(IRequest @params)
42
45
SendNotification ( GetMethodName ( @params . GetType ( ) ) , @params ) ;
43
46
}
44
47
45
- public async Task < TResponse > SendRequest < T , TResponse > ( string method , T @params , CancellationToken cancellationToken )
46
- {
47
- var tcs = new TaskCompletionSource < JToken > ( ) ;
48
-
49
- var nextId = _serializer . GetNextId ( ) ;
50
- _requests . TryAdd ( nextId , tcs ) ;
51
-
52
- _outputHandler . Send ( new Client . Request ( ) {
53
- Method = method ,
54
- Params = @params ,
55
- Id = nextId
56
- } , cancellationToken ) ;
57
-
58
- try
59
- {
60
- var result = await tcs . Task ;
61
- if ( typeof ( TResponse ) == typeof ( Unit ) )
62
- {
63
- return ( TResponse ) ( object ) Unit . Value ;
64
- }
65
- return result . ToObject < TResponse > ( _serializer . JsonSerializer ) ;
66
- }
67
- finally
68
- {
69
- _requests . TryRemove ( nextId , out _ ) ;
70
- }
71
- }
72
-
73
48
public Task < TResponse > SendRequest < TResponse > ( IRequest < TResponse > @params , CancellationToken cancellationToken )
74
49
{
75
- return SendRequest < IRequest < TResponse > , TResponse > ( GetMethodName ( @params . GetType ( ) ) , @params , cancellationToken ) ;
50
+ return SendRequest ( GetMethodName ( @params . GetType ( ) ) , @params ) . Returning < TResponse > ( cancellationToken ) ;
76
51
}
77
52
78
- public Task SendRequest ( IRequest @params , CancellationToken cancellationToken )
53
+ public IResponseRouterReturns SendRequest ( string method )
79
54
{
80
- return SendRequest ( GetMethodName ( @params . GetType ( ) ) , @params , cancellationToken ) ;
55
+ return new ResponseRouterReturnsImpl ( this , method , null ) ;
81
56
}
82
57
83
- public async Task < TResponse > SendRequest < TResponse > ( string method , CancellationToken cancellationToken )
58
+ public IResponseRouterReturns SendRequest < T > ( string method , T @params )
84
59
{
85
- var nextId = _serializer . GetNextId ( ) ;
86
-
87
- var tcs = new TaskCompletionSource < JToken > ( ) ;
88
- _requests . TryAdd ( nextId , tcs ) ;
89
-
90
- _outputHandler . Send ( new Client . Request ( ) {
91
- Method = method ,
92
- Params = null ,
93
- Id = nextId
94
- } , cancellationToken ) ;
95
-
96
- try
97
- {
98
- var result = await tcs . Task ;
99
- return result . ToObject < TResponse > ( _serializer . JsonSerializer ) ;
100
- }
101
- finally
102
- {
103
- _requests . TryRemove ( nextId , out var _ ) ;
104
- }
105
- }
106
-
107
- public async Task SendRequest < T > ( string method , T @params , CancellationToken cancellationToken )
108
- {
109
- var nextId = _serializer . GetNextId ( ) ;
110
-
111
- var tcs = new TaskCompletionSource < JToken > ( ) ;
112
- _requests . TryAdd ( nextId , tcs ) ;
113
-
114
- _outputHandler . Send ( new Client . Request ( ) {
115
- Method = method ,
116
- Params = @params ,
117
- Id = nextId
118
- } , cancellationToken ) ;
119
-
120
- try
121
- {
122
- await tcs . Task ;
123
- }
124
- finally
125
- {
126
- _requests . TryRemove ( nextId , out var _ ) ;
127
- }
60
+ return new ResponseRouterReturnsImpl ( this , method , @params ) ;
128
61
}
129
62
130
63
public TaskCompletionSource < JToken > GetRequest ( long id )
131
64
{
132
- _requests . TryGetValue ( id , out var source ) ;
65
+ Requests . TryGetValue ( id , out var source ) ;
133
66
return source ;
134
67
}
135
68
136
69
private string GetMethodName ( Type type )
137
70
{
138
- if ( ! _methodCache . TryGetValue ( type , out var methodName ) )
71
+ if ( ! MethodCache . TryGetValue ( type , out var methodName ) )
139
72
{
140
73
var attribute = type . GetCustomAttribute < MethodAttribute > ( true ) ;
141
74
if ( attribute == null )
@@ -144,10 +77,57 @@ private string GetMethodName(Type type)
144
77
}
145
78
146
79
methodName = attribute . Method ;
147
- _methodCache . TryAdd ( type , methodName ) ;
80
+ MethodCache . TryAdd ( type , methodName ) ;
148
81
}
149
82
150
83
return methodName ;
151
84
}
85
+
86
+ class ResponseRouterReturnsImpl : IResponseRouterReturns
87
+ {
88
+ private readonly ResponseRouter _router ;
89
+ private readonly string _method ;
90
+ private readonly object _params ;
91
+
92
+ public ResponseRouterReturnsImpl ( ResponseRouter router , string method , object @params )
93
+ {
94
+ _router = router ;
95
+ _method = method ;
96
+ _params = @params ;
97
+ }
98
+
99
+ public async Task < TResponse > Returning < TResponse > ( CancellationToken cancellationToken )
100
+ {
101
+ var nextId = _router . Serializer . GetNextId ( ) ;
102
+ var tcs = new TaskCompletionSource < JToken > ( ) ;
103
+ _router . Requests . TryAdd ( nextId , tcs ) ;
104
+
105
+ _router . OutputHandler . Send ( new Client . Request ( ) {
106
+ Method = _method ,
107
+ Params = _params ,
108
+ Id = nextId
109
+ } , cancellationToken ) ;
110
+
111
+ try
112
+ {
113
+ var result = await tcs . Task ;
114
+ if ( typeof ( TResponse ) == typeof ( Unit ) )
115
+ {
116
+ return ( TResponse ) ( object ) Unit . Value ;
117
+ }
118
+
119
+ return result . ToObject < TResponse > ( _router . Serializer . JsonSerializer ) ;
120
+ }
121
+ finally
122
+ {
123
+ _router . Requests . TryRemove ( nextId , out var _ ) ;
124
+ }
125
+ }
126
+
127
+ public async Task ReturningVoid ( CancellationToken cancellationToken )
128
+ {
129
+ await Returning < Unit > ( cancellationToken ) ;
130
+ }
131
+ }
152
132
}
153
133
}
0 commit comments