|
1 | | -using System; |
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using System.IO.Pipes; |
| 8 | +using System.Reflection; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | +using System.Text; |
| 11 | +using System.Threading.Tasks; |
2 | 12 |
|
3 | 13 | namespace JKang.IpcServiceFramework |
4 | 14 | { |
5 | | - public static class IpcServiceHost |
| 15 | + public class IpcServiceHost : IIpcServiceHost |
6 | 16 | { |
7 | | - public static IIpcServiceHost Buid(IServiceProvider serviceProvider) |
| 17 | + private readonly string _pipeName; |
| 18 | + private readonly IServiceProvider _serviceProvider; |
| 19 | + private readonly ILogger<IpcServiceHost> _logger; |
| 20 | + |
| 21 | + public IpcServiceHost(string pipeName, IServiceProvider serviceProvider) |
| 22 | + { |
| 23 | + _pipeName = pipeName; |
| 24 | + _serviceProvider = serviceProvider; |
| 25 | + _logger = _serviceProvider.GetService<ILogger<IpcServiceHost>>(); |
| 26 | + } |
| 27 | + |
| 28 | + public void Start() |
| 29 | + { |
| 30 | + using (var server = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 1)) |
| 31 | + using (var reader = new BinaryReader(server)) |
| 32 | + using (var writer = new StreamWriter(server)) |
| 33 | + { |
| 34 | + while (true) |
| 35 | + { |
| 36 | + _logger.LogInformation("IPC service started. Waiting for connection"); |
| 37 | + server.WaitForConnection(); |
| 38 | + |
| 39 | + try |
| 40 | + { |
| 41 | + _logger.LogDebug("client connected"); |
| 42 | + |
| 43 | + int length = reader.ReadInt32(); |
| 44 | + byte[] bytes = reader.ReadBytes(length); |
| 45 | + string json = Encoding.UTF8.GetString(bytes); |
| 46 | + IpcRequest request = JsonConvert.DeserializeObject<IpcRequest>(json); |
| 47 | + |
| 48 | + _logger.LogDebug("request received, invoking corresponding method..."); |
| 49 | + IpcResponse response; |
| 50 | + using (IServiceScope scope = _serviceProvider.CreateScope()) |
| 51 | + { |
| 52 | + response = GetReponse(request, scope); |
| 53 | + } |
| 54 | + |
| 55 | + _logger.LogDebug("sending response..."); |
| 56 | + string resultJson = JsonConvert.SerializeObject(response); |
| 57 | + writer.Write(resultJson); |
| 58 | + writer.Flush(); |
| 59 | + server.Disconnect(); |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + _logger.LogError(ex, ex.Message); |
| 64 | + server.Disconnect(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static IpcResponse GetReponse(IpcRequest request, IServiceScope scope) |
8 | 71 | { |
9 | | - throw new NotImplementedException(); |
| 72 | + var @interface = Type.GetType(request.InterfaceName); |
| 73 | + if (@interface == null) |
| 74 | + { |
| 75 | + return IpcResponse.Fail($"Interface '{@interface}' not found."); |
| 76 | + } |
| 77 | + |
| 78 | + object service = scope.ServiceProvider.GetService(@interface); |
| 79 | + if (service == null) |
| 80 | + { |
| 81 | + return IpcResponse.Fail($"No implementation of interface '{@interface}' found."); |
| 82 | + } |
| 83 | + |
| 84 | + MethodInfo method = service.GetType().GetMethod(request.MethodName); |
| 85 | + if (method == null) |
| 86 | + { |
| 87 | + return IpcResponse.Fail($"Method '{request.MethodName}' not found in interface '{@interface}'."); |
| 88 | + } |
| 89 | + |
| 90 | + ParameterInfo[] paramInfos = method.GetParameters(); |
| 91 | + if (paramInfos.Length != request.Parameters.Length) |
| 92 | + { |
| 93 | + return IpcResponse.Fail($"Parameter mismatch."); |
| 94 | + } |
| 95 | + |
| 96 | + object[] args = new object[paramInfos.Length]; |
| 97 | + for (int i = 0; i < args.Length; i++) |
| 98 | + { |
| 99 | + if (request.Parameters[i].GetType() == paramInfos[i].ParameterType) |
| 100 | + { |
| 101 | + args[i] = request.Parameters[i]; |
| 102 | + } |
| 103 | + else if (request.Parameters[i] is JObject jObj) |
| 104 | + { |
| 105 | + args[i] = jObj.ToObject(paramInfos[i].ParameterType); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + return IpcResponse.Fail($"Cannot convert value of parameter '{paramInfos[i].Name}'"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + try |
| 114 | + { |
| 115 | + dynamic @return = (dynamic)method.Invoke(service, args); |
| 116 | + return IpcResponse.Success(@return.Result); // TODO: handle non-waitable method |
| 117 | + } |
| 118 | + catch |
| 119 | + { |
| 120 | + return IpcResponse.Fail("Internal server error"); |
| 121 | + } |
10 | 122 | } |
11 | 123 | } |
12 | 124 | } |
0 commit comments