Skip to content

Commit 9931feb

Browse files
committed
refactoring
1 parent 33c2965 commit 9931feb

File tree

9 files changed

+141
-31
lines changed

9 files changed

+141
-31
lines changed

src/IpcServiceSample.ConsoleServer/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ static void Main(string[] args)
2727

2828
private static IServiceCollection ConfigureServices(IServiceCollection services)
2929
{
30-
return services
31-
.AddLogging()
32-
.AddScoped<IMyIpcService, MyIpcService>()
30+
services
31+
.AddLogging();
32+
33+
services
34+
.AddIpc()
35+
.AddService<IMyIpcService, MyIpcService>()
3336
;
37+
38+
return services;
3439
}
3540
}
3641
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json;
2+
using System.Text;
3+
4+
namespace JKang.IpcServiceFramework
5+
{
6+
internal class DefaultIpcMessageSerializer : IIpcMessageSerializer
7+
{
8+
private static readonly JsonSerializerSettings _settings = new JsonSerializerSettings
9+
{
10+
TypeNameHandling = TypeNameHandling.Objects
11+
};
12+
13+
public IpcRequest DeserializeRequest(byte[] binary)
14+
{
15+
return Deserialize<IpcRequest>(binary);
16+
}
17+
18+
public IpcResponse DeserializeResponse(byte[] binary)
19+
{
20+
return Deserialize<IpcResponse>(binary);
21+
}
22+
23+
public byte[] SerializeRequest(IpcRequest request)
24+
{
25+
return Serialize(request);
26+
}
27+
28+
public byte[] SerializeResponse(IpcResponse response)
29+
{
30+
return Serialize(response);
31+
}
32+
33+
private T Deserialize<T>(byte[] binary)
34+
{
35+
string json = Encoding.UTF8.GetString(binary);
36+
return JsonConvert.DeserializeObject<T>(json);
37+
}
38+
39+
private byte[] Serialize(object obj)
40+
{
41+
string json = JsonConvert.SerializeObject(obj, _settings);
42+
return Encoding.UTF8.GetBytes(json);
43+
}
44+
}
45+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace JKang.IpcServiceFramework
2+
{
3+
public interface IIpcMessageSerializer
4+
{
5+
byte[] SerializeRequest(IpcRequest request);
6+
IpcResponse DeserializeResponse(byte[] binary);
7+
IpcRequest DeserializeRequest(byte[] binary);
8+
byte[] SerializeResponse(IpcResponse response);
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace JKang.IpcServiceFramework
2+
{
3+
public interface IIpcServiceCollection
4+
{
5+
IIpcServiceCollection AddService<TInterface, TImplementation>()
6+
where TInterface: class
7+
where TImplementation: class, TInterface;
8+
}
9+
}

src/JKang.IpcServiceFramework/IpcRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace JKang.IpcServiceFramework
22
{
3-
internal class IpcRequest
3+
public class IpcRequest
44
{
55
public string InterfaceName { get; set; }
66
public string MethodName { get; set; }

src/JKang.IpcServiceFramework/IpcServiceClient.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
using Newtonsoft.Json;
2-
using Newtonsoft.Json.Linq;
1+
using Newtonsoft.Json.Linq;
32
using System;
43
using System.IO;
54
using System.IO.Pipes;
6-
using System.Text;
75
using System.Threading.Tasks;
86

97
namespace JKang.IpcServiceFramework
108
{
119
public abstract class IpcServiceClient<TInterface>
1210
{
1311
private readonly string _pipeName;
12+
private readonly IIpcMessageSerializer _serializer;
1413

1514
public IpcServiceClient(string pipeName)
15+
: this(pipeName, new DefaultIpcMessageSerializer())
16+
{ }
17+
18+
public IpcServiceClient(string pipeName, IIpcMessageSerializer requestSerializer)
1619
{
1720
_pipeName = pipeName;
21+
_serializer = requestSerializer;
1822
}
1923

2024
public async Task<TResult> InvokeAsync<TResult>(string method, params object[] args)
@@ -30,17 +34,17 @@ public async Task<TResult> InvokeAsync<TResult>(string method, params object[] a
3034
MethodName = method,
3135
Parameters = args,
3236
};
33-
string json = JsonConvert.SerializeObject(request);
34-
byte[] bytes = Encoding.UTF8.GetBytes(json);
37+
byte[] requestBin = _serializer.SerializeRequest(request);
3538
var writer = new BinaryWriter(client);
36-
writer.Write(bytes.Length);
37-
writer.Write(bytes);
39+
writer.Write(requestBin.Length);
40+
writer.Write(requestBin);
3841
await client.FlushAsync();
3942

4043
// receive response
41-
var reader = new StreamReader(client);
42-
string responseJson = await reader.ReadToEndAsync();
43-
IpcResponse response = JsonConvert.DeserializeObject<IpcResponse>(responseJson);
44+
var reader = new BinaryReader(client);
45+
int responseLength = reader.ReadInt32();
46+
byte[] responseBin = reader.ReadBytes(responseLength);
47+
IpcResponse response = _serializer.DeserializeResponse(responseBin);
4448
if (response.Succeed)
4549
{
4650
// TODO: handle primitive types
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace JKang.IpcServiceFramework
4+
{
5+
internal class IpcServiceCollection : IIpcServiceCollection
6+
{
7+
private readonly IServiceCollection _services;
8+
9+
public IpcServiceCollection(IServiceCollection services)
10+
{
11+
_services = services;
12+
}
13+
14+
IIpcServiceCollection IIpcServiceCollection.AddService<TInterface, TImplementation>()
15+
{
16+
_services.AddScoped<TInterface, TImplementation>();
17+
return this;
18+
}
19+
}
20+
}

src/JKang.IpcServiceFramework/IpcServiceHost.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,36 @@ public void Start()
2929
{
3030
using (var server = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 1))
3131
using (var reader = new BinaryReader(server))
32-
using (var writer = new StreamWriter(server))
32+
using (var writer = new BinaryWriter(server))
3333
{
3434
while (true)
3535
{
3636
_logger.LogInformation("IPC service started. Waiting for connection");
3737
server.WaitForConnection();
3838

39+
_logger.LogDebug("client connected");
3940
try
4041
{
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;
5042
using (IServiceScope scope = _serviceProvider.CreateScope())
5143
{
52-
response = GetReponse(request, scope);
53-
}
44+
IIpcMessageSerializer serializer = scope.ServiceProvider.GetRequiredService<IIpcMessageSerializer>();
5445

55-
_logger.LogDebug("sending response...");
56-
string resultJson = JsonConvert.SerializeObject(response);
57-
writer.Write(resultJson);
58-
writer.Flush();
59-
server.Disconnect();
46+
int requestLength = reader.ReadInt32();
47+
byte[] requestBin = reader.ReadBytes(requestLength);
48+
IpcRequest request = serializer.DeserializeRequest(requestBin);
49+
50+
_logger.LogDebug("request received, invoking corresponding method...");
51+
IpcResponse response = GetReponse(request, scope);
52+
53+
_logger.LogDebug("sending response...");
54+
byte[] responseBin = serializer.SerializeResponse(response);
55+
writer.Write(responseBin.Length);
56+
writer.Write(responseBin);
57+
writer.Flush();
58+
59+
// disconnect client
60+
server.Disconnect();
61+
}
6062
}
6163
catch (Exception ex)
6264
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace JKang.IpcServiceFramework
4+
{
5+
public static class IpcServiceServiceCollectionExtensions
6+
{
7+
public static IIpcServiceCollection AddIpc(this IServiceCollection services)
8+
{
9+
services
10+
.AddScoped<IIpcMessageSerializer, DefaultIpcMessageSerializer>();
11+
12+
return new IpcServiceCollection(services);
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)