-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
IpcCommunication.cs
51 lines (44 loc) · 1.88 KB
/
IpcCommunication.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
using ElectronNET.API;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ReHUD;
static class IpcCommunication {
public static readonly int DELAY_WARNING = 20;
public static readonly int DELAY_ERROR = 500;
/// <summary>
/// Invokes a channel on the render process and returns the result.
/// </summary>
public static async Task<JToken?> Invoke(BrowserWindow window, string channel, object? data = null) {
try {
var promise = new TaskCompletionSource<JToken?>();
var conversationid = Guid.NewGuid().ToString();
var newData = new object[data == null ? 1 : 2];
newData[0] = conversationid;
if (data != null) newData[1] = data;
Electron.IpcMain.Once(conversationid, (args) => {
try {
var timeNow = DateTimeOffset.Now.ToUnixTimeMilliseconds();
var array = (JArray)JsonConvert.DeserializeObject(args.ToString()!)!;
var diff = timeNow - array[0].ToObject<long>();
if (diff > DELAY_WARNING) {
Startup.logger.WarnFormat("IPC responded in {0}ms", diff);
}
if (diff > DELAY_ERROR) {
Startup.logger.ErrorFormat("IPC responded in {0}ms", diff);
}
if (array.Count > 1) {
promise.SetResult(array[1]);
}
} catch (Exception e) {
Startup.logger.Error("Error invoking IPC", e);
}
promise.SetResult(null);
});
Electron.IpcMain.Send(window, channel, JsonConvert.SerializeObject(newData));
return await promise.Task;
} catch (Exception e) {
Startup.logger.Error("Error invoking IPC", e);
return null;
}
}
}