-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
206 lines (180 loc) · 6.55 KB
/
Program.cs
File metadata and controls
206 lines (180 loc) · 6.55 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using Microsoft.Extensions.DependencyInjection;
using Nito.AsyncEx;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using UiPath.Ipc.NamedPipe;
using UiPath.Ipc.WebSockets;
namespace UiPath.Ipc.NodeInterop;
using static Contracts;
using static ServiceImpls;
using static Signalling;
class Program
{
/// <summary>
/// .NET - Nodejs Interop Helper
/// </summary>
/// <param name="pipe">The pipe name on which the CoreIpc endpoints will be hosted at.</param>
/// <param name="websocket">The websocket url on which the CoreIpc endpoints will be hosted at.</param>
/// <param name="mutex">Optional process mutual exclusion name.</param>
/// <param name="delay">Optional number of seconds that the process will wait before it exposes the CoreIpc endpoints.</param>
static async Task<int> Main(
string? pipe,
string? websocket,
string? mutex = null,
int? delay = null)
{
if ((pipe, websocket) is (null, null))
{
Console.Error.WriteLine($"Expecting either a non-null pipe name or a non-null websocket url or both.");
return 1;
}
try
{
if (mutex is { })
{
using var _ = new Mutex(initiallyOwned: false, mutex, out bool createdNew);
if (!createdNew) { return 2; }
await MainCore(pipe, websocket, delay);
}
else
{
await MainCore(pipe, websocket, delay);
}
}
catch (Exception ex)
{
Throw(ex);
throw;
}
return 0;
}
static async Task MainCore(string? pipeName, string? webSocketUrl, int? maybeSecondsPowerOnDelay)
{
if (maybeSecondsPowerOnDelay is { } secondsPowerOnDelay)
{
await Task.Delay(TimeSpan.FromSeconds(secondsPowerOnDelay));
}
Send(SignalKind.PoweringOn);
var services = new ServiceCollection();
var sp = services
.AddLogging()
.AddIpc()
.AddSingleton<IAlgebra, Algebra>()
.AddSingleton<ICalculus, Calculus>()
.AddSingleton<IBrittleService, BrittleService>()
.AddSingleton<IEnvironmentVariableGetter, EnvironmentVariableGetter>()
.AddSingleton<IDtoService, DtoService>()
.BuildServiceProvider();
var serviceHost = new ServiceHostBuilder(sp)
.UseNamedPipesAndOrWebSockets(pipeName, webSocketUrl)
.AddEndpoint<IAlgebra, IArithmetic>()
.AddEndpoint<ICalculus>()
.AddEndpoint<IBrittleService>()
.AddEndpoint<IEnvironmentVariableGetter>()
.AddEndpoint<IDtoService>()
.Build();
var thread = new AsyncContextThread();
thread.Context.SynchronizationContext.Send(_ => Thread.CurrentThread.Name = "GuiThread", null);
var sched = thread.Context.Scheduler;
_ = Task.Run(async () =>
{
try
{
await using var sp = new ServiceCollection()
.AddLogging()
.AddIpc()
.BuildServiceProvider();
var callback = new Arithmetic();
IEnumerable<Task> EnumeratePings()
{
if (webSocketUrl is not null)
{
yield return new WebSocketClientBuilder<IAlgebra, IArithmetic>(uri: new(webSocketUrl), sp)
.RequestTimeout(TimeSpan.FromHours(5))
.CallbackInstance(callback)
.Build()
.Ping();
}
if (pipeName is not null)
{
yield return new NamedPipeClientBuilder<IAlgebra, IArithmetic>(pipeName, sp)
.RequestTimeout(TimeSpan.FromHours(5))
.CallbackInstance(callback)
.Build()
.Ping();
}
}
await Task.WhenAll(EnumeratePings());
Send(SignalKind.ReadyToConnect);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
CannotConnect(ex);
}
});
await serviceHost.RunAsync(sched);
}
private class Arithmetic : IArithmetic
{
public Task<int> Sum(int x, int y) => Task.FromResult(x + y);
public Task<bool> SendMessage(Message<int> message) => Task.FromResult(true);
}
}
internal static class Extensions
{
public static ServiceHostBuilder UseNamedPipesAndOrWebSockets(this ServiceHostBuilder builder, string? pipeName, string? webSocketUrl)
{
if (pipeName is null && webSocketUrl is null)
{
throw new ArgumentOutOfRangeException();
}
if (pipeName is not null)
{
builder = builder.UseNamedPipes(new NamedPipeSettings(pipeName));
}
if (webSocketUrl is not null)
{
string url = CurateWebSocketUrl(webSocketUrl);
var accept = new HttpSysWebSocketsListener(url).Accept;
WebSocketSettings settings = new(accept);
builder = builder.UseWebSockets(settings);
}
return builder;
}
private static string CurateWebSocketUrl(string raw)
{
var builder = new UriBuilder(raw);
builder.Scheme = "http";
return builder.ToString();
}
public class HttpSysWebSocketsListener : IDisposable
{
HttpListener _httpListener = new();
public HttpSysWebSocketsListener(string uriPrefix)
{
_httpListener.Prefixes.Add(uriPrefix);
_httpListener.Start();
}
public async Task<WebSocket> Accept(CancellationToken token)
{
while (true)
{
var listenerContext = await _httpListener.GetContextAsync();
if (listenerContext.Request.IsWebSocketRequest)
{
var webSocketContext = await listenerContext.AcceptWebSocketAsync(subProtocol: null);
return webSocketContext.WebSocket;
}
listenerContext.Response.StatusCode = 400;
listenerContext.Response.Close();
}
}
public void Dispose() => _httpListener.Stop();
}
}