forked from OmniSharp/csharp-language-server-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonRpcServer.cs
208 lines (178 loc) · 7.66 KB
/
JsonRpcServer.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using OmniSharp.Extensions.Embedded.MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc;
using System.Reactive.Disposables;
namespace OmniSharp.Extensions.JsonRpc
{
public class JsonRpcServer : IJsonRpcServer
{
private readonly Connection _connection;
private readonly IRequestRouter<IHandlerDescriptor> _requestRouter;
private readonly IReciever _reciever;
private readonly ISerializer _serializer;
private readonly HandlerCollection _collection;
private readonly List<(string method, Func<IServiceProvider, IJsonRpcHandler>)> _namedHandlers = new List<(string method, Func<IServiceProvider, IJsonRpcHandler>)>();
private readonly IResponseRouter _responseRouter;
private readonly IServiceProvider _serviceProvider;
public static Task<IJsonRpcServer> From(Action<JsonRpcServerOptions> optionsAction)
{
var options = new JsonRpcServerOptions();
optionsAction(options);
return From(options);
}
public static async Task<IJsonRpcServer> From(JsonRpcServerOptions options)
{
var server = new JsonRpcServer(
options.Input,
options.Output,
options.Reciever,
options.RequestProcessIdentifier,
options.LoggerFactory,
options.Serializer,
options.Services,
options.HandlerTypes.Select(x => x.Assembly)
.Distinct().Concat(options.HandlerAssemblies),
options.Handlers,
options.NamedHandlers,
options.NamedServiceHandlers
);
await server.Initialize();
return server;
}
internal JsonRpcServer(
Stream input,
Stream output,
IReciever reciever,
IRequestProcessIdentifier requestProcessIdentifier,
ILoggerFactory loggerFactory,
ISerializer serializer,
IServiceCollection services,
IEnumerable<Assembly> assemblies,
IEnumerable<IJsonRpcHandler> handlers,
IEnumerable<(string name, IJsonRpcHandler handler)> namedHandlers,
IEnumerable<(string name, Func<IServiceProvider, IJsonRpcHandler> handlerFunc)> namedServiceHandlers)
{
var outputHandler = new OutputHandler(output, serializer);
services.AddLogging();
_reciever = reciever;
_serializer = serializer;
_collection = new HandlerCollection();
services.AddSingleton<IOutputHandler>(outputHandler);
services.AddSingleton(_collection);
services.AddSingleton(_serializer);
services.AddSingleton<OmniSharp.Extensions.JsonRpc.ISerializer>(_serializer);
services.AddSingleton(requestProcessIdentifier);
services.AddSingleton(_reciever);
services.AddSingleton(loggerFactory);
services.AddJsonRpcMediatR(assemblies);
services.AddSingleton<IJsonRpcServer>(this);
services.AddSingleton<IRequestRouter<IHandlerDescriptor>, RequestRouter>();
services.AddSingleton<IResponseRouter, ResponseRouter>();
var foundHandlers = services
.Where(x => typeof(IJsonRpcHandler).IsAssignableFrom(x.ServiceType) && x.ServiceType != typeof(IJsonRpcHandler))
.ToArray();
// Handlers are created at the start and maintained as a singleton
foreach (var handler in foundHandlers)
{
services.Remove(handler);
if (handler.ImplementationFactory != null)
services.Add(ServiceDescriptor.Singleton(typeof(IJsonRpcHandler), handler.ImplementationFactory));
else if (handler.ImplementationInstance != null)
services.Add(ServiceDescriptor.Singleton(typeof(IJsonRpcHandler), handler.ImplementationInstance));
else
services.Add(ServiceDescriptor.Singleton(typeof(IJsonRpcHandler), handler.ImplementationType));
}
_serviceProvider = services.BuildServiceProvider();
var serviceHandlers = _serviceProvider.GetServices<IJsonRpcHandler>().ToArray();
_collection.Add(serviceHandlers);
_collection.Add(handlers.ToArray());
foreach (var (name, handler) in namedHandlers)
{
_collection.Add(name, handler);
}
foreach (var (name, handlerFunc) in namedServiceHandlers)
{
_collection.Add(name, handlerFunc(_serviceProvider));
}
_requestRouter = _serviceProvider.GetRequiredService<IRequestRouter<IHandlerDescriptor>>();
_collection.Add(new CancelRequestHandler<IHandlerDescriptor>(_requestRouter));
_responseRouter = _serviceProvider.GetRequiredService<IResponseRouter>();
_connection = ActivatorUtilities.CreateInstance<Connection>(_serviceProvider, input);
}
public IDisposable AddHandler(string method, IJsonRpcHandler handler)
{
return _collection.Add(method, handler);
}
public IDisposable AddHandler(string method, Func<IServiceProvider, IJsonRpcHandler> handlerFunc)
{
_namedHandlers.Add((method, handlerFunc));
return Disposable.Empty;
}
public IDisposable AddHandlers(params IJsonRpcHandler[] handlers)
{
return _collection.Add(handlers);
}
public IDisposable AddHandler(string method, Type handlerType)
{
return _collection.Add(method, ActivatorUtilities.CreateInstance(_serviceProvider, handlerType) as IJsonRpcHandler);
}
public IDisposable AddHandler<T>()
where T : IJsonRpcHandler
{
return AddHandlers(typeof(T));
}
public IDisposable AddHandlers(params Type[] handlerTypes)
{
return _collection.Add(
handlerTypes
.Select(handlerType => ActivatorUtilities.CreateInstance(_serviceProvider, handlerType) as IJsonRpcHandler)
.ToArray());
}
private async Task Initialize()
{
await Task.Yield();
_connection.Open();
}
public void SendNotification(string method)
{
_responseRouter.SendNotification(method);
}
public void SendNotification<T>(string method, T @params)
{
_responseRouter.SendNotification(method, @params);
}
public Task<TResponse> SendRequest<T, TResponse>(string method, T @params)
{
return _responseRouter.SendRequest<T, TResponse>(method, @params);
}
public Task<TResponse> SendRequest<TResponse>(string method)
{
return _responseRouter.SendRequest<TResponse>(method);
}
public Task SendRequest<T>(string method, T @params)
{
return _responseRouter.SendRequest(method, @params);
}
public TaskCompletionSource<JToken> GetRequest(long id)
{
return _responseRouter.GetRequest(id);
}
public void Dispose()
{
_connection?.Dispose();
}
}
}