forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeConnector.cs
More file actions
114 lines (94 loc) · 4.08 KB
/
BridgeConnector.cs
File metadata and controls
114 lines (94 loc) · 4.08 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ElectronNET.API
{
internal static class BridgeConnector
{
private static SocketIoFacade _socket;
private static readonly object SyncRoot = new();
public static SocketIoFacade Socket
{
get
{
if (_socket == null)
{
lock (SyncRoot)
{
if (_socket == null)
{
string socketUrl = HybridSupport.IsElectronActive
? $"http://localhost:{BridgeSettings.SocketPort}"
: "http://localhost";
_socket = new SocketIoFacade(socketUrl);
_socket.Connect();
}
}
}
return _socket;
}
}
public static async Task<T> GetValueOverSocketAsync<T>(string eventString, string eventCompletedString)
{
CancellationToken cancellationToken = new();
cancellationToken.ThrowIfCancellationRequested();
var taskCompletionSource = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On(eventCompletedString, (value) =>
{
BridgeConnector.Socket.Off(eventCompletedString);
if (value == null)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
taskCompletionSource.SetCanceled();
return;
}
try
{
taskCompletionSource.SetResult( new JValue(value).ToObject<T>() );
}
catch (Exception e)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
}
});
BridgeConnector.Socket.Emit(eventString);
return await taskCompletionSource.Task.ConfigureAwait(false);
}
}
public static async Task<T> GetObjectOverSocketAsync<T>(string eventString, string eventCompletedString)
{
CancellationToken cancellationToken = new();
cancellationToken.ThrowIfCancellationRequested();
var taskCompletionSource = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On(eventCompletedString, (value) =>
{
BridgeConnector.Socket.Off(eventCompletedString);
taskCompletionSource.SetResult( ((JObject)value).ToObject<T>() );
});
BridgeConnector.Socket.Emit(eventString);
return await taskCompletionSource.Task.ConfigureAwait(false);
}
}
public static async Task<T> GetArrayOverSocketAsync<T>(string eventString, string eventCompletedString)
{
CancellationToken cancellationToken = new();
cancellationToken.ThrowIfCancellationRequested();
var taskCompletionSource = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On(eventCompletedString, (value) =>
{
BridgeConnector.Socket.Off(eventCompletedString);
taskCompletionSource.SetResult( ((JArray)value).ToObject<T>() );
});
BridgeConnector.Socket.Emit(eventString);
return await taskCompletionSource.Task.ConfigureAwait(false);
}
}
}
}