forked from matarillo/LanguageServerProtocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandlerProvider.cs
59 lines (52 loc) · 2.32 KB
/
HandlerProvider.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
using System;
using System.Reflection;
using System.Threading;
namespace LanguageServer {
internal abstract class HandlerProvider {
internal void AddHandlers(RequestHandlerCollection requestHandlers, NotificationHandlerCollection notificationHandlers, Type type) {
foreach(var method in type.GetRuntimeMethods()) {
var rpcMethod = method.GetCustomAttribute<JsonRpcMethodAttribute>()?.Method;
if(rpcMethod != null) {
if(Reflector.IsRequestHandler(method)) {
var requestHandlerDelegate = Reflector.CreateRequestHandlerDelegate(type, method, this);
var requestHandler = new RequestHandler(rpcMethod, Reflector.GetRequestType(method),
Reflector.GetResponseType(method), requestHandlerDelegate);
requestHandlers.AddRequestHandler(requestHandler);
}
else if(Reflector.IsNotificationHandler(method)) {
var notificationHandlerDelegate =
Reflector.CreateNotificationHandlerDelegate(type, method, this);
var notificationHandler = new NotificationHandler(rpcMethod,
Reflector.GetNotificationType(method), notificationHandlerDelegate);
notificationHandlers.AddNotificationHandler(notificationHandler);
}
}
}
}
internal abstract object CreateTargetObject(Type targetType, Connection connection, CancellationToken token);
internal abstract object CreateTargetObject(Type targetType, Connection connection);
}
internal class ServiceHandlerProvider : HandlerProvider {
internal override object CreateTargetObject(Type targetType, Connection connection, CancellationToken token) {
var svc = (Service)Activator.CreateInstance(targetType);
svc.Connection = connection;
svc.CancellationToken = token;
return svc;
}
internal override object CreateTargetObject(Type targetType, Connection connection) {
var svc = (Service)Activator.CreateInstance(targetType);
svc.Connection = connection;
return svc;
}
}
internal class ConnectionHandlerProvider : HandlerProvider {
internal override object CreateTargetObject(Type targetType, Connection connection, CancellationToken token) {
var sc = (ServiceConnection)connection;
sc.CancellationToken = token;
return sc;
}
internal override object CreateTargetObject(Type targetType, Connection connection) {
return connection;
}
}
}