@@ -10,28 +10,31 @@ namespace OmniSharp.Extensions.JsonRpc
1010{
1111 public class ResponseRouter : IResponseRouter
1212 {
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 > ( ) ;
1821
1922 public ResponseRouter ( IOutputHandler outputHandler , ISerializer serializer )
2023 {
21- _outputHandler = outputHandler ;
22- _serializer = serializer ;
24+ OutputHandler = outputHandler ;
25+ Serializer = serializer ;
2326 }
2427
2528 public void SendNotification ( string method )
2629 {
27- _outputHandler . Send ( new Client . Notification ( ) {
30+ OutputHandler . Send ( new Client . Notification ( ) {
2831 Method = method
2932 } , CancellationToken . None ) ;
3033 }
3134
3235 public void SendNotification < T > ( string method , T @params )
3336 {
34- _outputHandler . Send ( new Client . Notification ( ) {
37+ OutputHandler . Send ( new Client . Notification ( ) {
3538 Method = method ,
3639 Params = @params
3740 } , CancellationToken . None ) ;
@@ -42,100 +45,30 @@ public void SendNotification(IRequest @params)
4245 SendNotification ( GetMethodName ( @params . GetType ( ) ) , @params ) ;
4346 }
4447
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-
7348 public Task < TResponse > SendRequest < TResponse > ( IRequest < TResponse > @params , CancellationToken cancellationToken )
7449 {
75- return SendRequest < IRequest < TResponse > , TResponse > ( GetMethodName ( @params . GetType ( ) ) , @params , cancellationToken ) ;
50+ return SendRequest ( GetMethodName ( @params . GetType ( ) ) , @params ) . Returning < TResponse > ( cancellationToken ) ;
7651 }
7752
78- public Task SendRequest ( IRequest @params , CancellationToken cancellationToken )
53+ public IResponseRouterReturns SendRequest ( string method )
7954 {
80- return SendRequest ( GetMethodName ( @params . GetType ( ) ) , @params , cancellationToken ) ;
55+ return new ResponseRouterReturnsImpl ( this , method , null ) ;
8156 }
8257
83- public async Task < TResponse > SendRequest < TResponse > ( string method , CancellationToken cancellationToken )
58+ public IResponseRouterReturns SendRequest < T > ( string method , T @params )
8459 {
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 ) ;
12861 }
12962
13063 public TaskCompletionSource < JToken > GetRequest ( long id )
13164 {
132- _requests . TryGetValue ( id , out var source ) ;
65+ Requests . TryGetValue ( id , out var source ) ;
13366 return source ;
13467 }
13568
13669 private string GetMethodName ( Type type )
13770 {
138- if ( ! _methodCache . TryGetValue ( type , out var methodName ) )
71+ if ( ! MethodCache . TryGetValue ( type , out var methodName ) )
13972 {
14073 var attribute = type . GetCustomAttribute < MethodAttribute > ( true ) ;
14174 if ( attribute == null )
@@ -144,10 +77,57 @@ private string GetMethodName(Type type)
14477 }
14578
14679 methodName = attribute . Method ;
147- _methodCache . TryAdd ( type , methodName ) ;
80+ MethodCache . TryAdd ( type , methodName ) ;
14881 }
14982
15083 return methodName ;
15184 }
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+ }
152132 }
153133}
0 commit comments