-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathIpcClient.cs
More file actions
50 lines (46 loc) · 1.96 KB
/
Copy pathIpcClient.cs
File metadata and controls
50 lines (46 loc) · 1.96 KB
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
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
namespace RoboBackup {
/// <summary>
/// Client side for interprocess communication via named pipe between the GUI (client) and the service (server).
/// </summary>
public static class IpcClient {
/// <summary>
/// Interprocess communication client channel.
/// </summary>
private static IpcClientChannel _channel;
/// <summary>
/// Interprocess communication object proxy.
/// </summary>
private static IpcService _service;
/// <summary>
/// Tests connection via IPC and return <see cref="IpcService"/> object proxy.
/// </summary>
/// <returns><see cref="IpcService"/> object proxy.</returns>
public static IpcService GetService() {
try {
_service.Ping();
} catch {
RegisterIpcClient();
}
return _service;
}
/// <summary>
/// Registers interprocess communication client channel and the <see cref="IpcService"/> object proxy.
/// </summary>
private static void RegisterIpcClient() {
// Unregister the channel if it was previously registered but the underlying pipe has been closed (e.g. when the service was restarted but the GUI wasn't).
if (_channel != null) {
ChannelServices.UnregisterChannel(_channel);
}
_channel = new IpcClientChannel();
ChannelServices.RegisterChannel(_channel, false);
if (_service == null) {
WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(IpcService), "ipc://RoboBackup/ipc");
RemotingConfiguration.RegisterWellKnownClientType(remoteType);
_service = new IpcService();
}
}
}
}