forked from Yuvix25/ReHUD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIpcCommunication.cs
More file actions
68 lines (58 loc) · 2.26 KB
/
IpcCommunication.cs
File metadata and controls
68 lines (58 loc) · 2.26 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
if (HybridSupport.IsElectronActive)
{
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));
}
else
{
promise.SetResult(null);
}
return await promise.Task;
} catch (Exception e) {
Startup.logger.Error("Error invoking IPC", e);
return null;
}
}
}