7
7
using System . Net . WebSockets ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
- using UiPath . CoreIpc . NamedPipe ;
11
- using UiPath . CoreIpc . WebSockets ;
10
+ using UiPath . Ipc . Transport . NamedPipe ;
11
+ using UiPath . Ipc . Transport . WebSocket ;
12
12
13
- namespace UiPath . CoreIpc . NodeInterop ;
13
+ namespace UiPath . Ipc . NodeInterop ;
14
14
15
15
using static Contracts ;
16
16
using static ServiceImpls ;
17
17
using static Signalling ;
18
+ using static UiPath . Ipc . NodeInterop . Extensions ;
18
19
19
20
class Program
20
21
{
@@ -61,6 +62,11 @@ static async Task<int> Main(
61
62
62
63
static async Task MainCore ( string ? pipeName , string ? webSocketUrl , int ? maybeSecondsPowerOnDelay )
63
64
{
65
+ if ( pipeName is null && webSocketUrl is null )
66
+ {
67
+ throw new ArgumentException ( $ "At least one of { nameof ( pipeName ) } or { nameof ( webSocketUrl ) } must be specified.") ;
68
+ }
69
+
64
70
if ( maybeSecondsPowerOnDelay is { } secondsPowerOnDelay )
65
71
{
66
72
await Task . Delay ( TimeSpan . FromSeconds ( secondsPowerOnDelay ) ) ;
@@ -71,34 +77,41 @@ static async Task MainCore(string? pipeName, string? webSocketUrl, int? maybeSec
71
77
72
78
var sp = services
73
79
. AddLogging ( )
74
- . AddIpc ( )
75
80
. AddSingleton < IAlgebra , Algebra > ( )
76
81
. AddSingleton < ICalculus , Calculus > ( )
77
82
. AddSingleton < IBrittleService , BrittleService > ( )
78
83
. AddSingleton < IEnvironmentVariableGetter , EnvironmentVariableGetter > ( )
79
84
. AddSingleton < IDtoService , DtoService > ( )
80
85
. BuildServiceProvider ( ) ;
81
86
82
- var serviceHost = new ServiceHostBuilder ( sp )
83
- . UseNamedPipesAndOrWebSockets ( pipeName , webSocketUrl )
84
- . AddEndpoint < IAlgebra , IArithmetic > ( )
85
- . AddEndpoint < ICalculus > ( )
86
- . AddEndpoint < IBrittleService > ( )
87
- . AddEndpoint < IEnvironmentVariableGetter > ( )
88
- . AddEndpoint < IDtoService > ( )
89
- . Build ( ) ;
90
-
91
87
var thread = new AsyncContextThread ( ) ;
92
88
thread . Context . SynchronizationContext . Send ( _ => Thread . CurrentThread . Name = "GuiThread" , null ) ;
93
- var sched = thread . Context . Scheduler ;
89
+ var scheduler = thread . Context . Scheduler ;
90
+
91
+ var ipcServer = new IpcServer ( )
92
+ {
93
+ Endpoints = new ( )
94
+ {
95
+ typeof ( IAlgebra ) ,
96
+ typeof ( ICalculus ) ,
97
+ typeof ( IBrittleService ) ,
98
+ typeof ( IEnvironmentVariableGetter ) ,
99
+ typeof ( IDtoService )
100
+ } ,
101
+ Listeners = [
102
+ ..EnumerateListeners ( pipeName , webSocketUrl )
103
+ ] ,
104
+ ServiceProvider = sp ,
105
+ Scheduler = scheduler
106
+ } ;
107
+ ipcServer . Start ( ) ;
94
108
95
109
_ = Task . Run ( async ( ) =>
96
110
{
97
111
try
98
112
{
99
113
await using var sp = new ServiceCollection ( )
100
114
. AddLogging ( )
101
- . AddIpc ( )
102
115
. BuildServiceProvider ( ) ;
103
116
104
117
var callback = new Arithmetic ( ) ;
@@ -107,25 +120,51 @@ IEnumerable<Task> EnumeratePings()
107
120
{
108
121
if ( webSocketUrl is not null )
109
122
{
110
- yield return new WebSocketClientBuilder < IAlgebra , IArithmetic > ( uri : new ( webSocketUrl ) , sp )
111
- . RequestTimeout ( TimeSpan . FromHours ( 5 ) )
112
- . CallbackInstance ( callback )
113
- . Build ( )
114
- . Ping ( ) ;
123
+ yield return new IpcClient
124
+ {
125
+ Config = new ( )
126
+ {
127
+ ServiceProvider = sp ,
128
+ RequestTimeout = TimeSpan . FromHours ( 5 ) ,
129
+ Callbacks = new ( )
130
+ {
131
+ { typeof ( IArithmetic ) , callback }
132
+ } ,
133
+ } ,
134
+ Transport = new WebSocketTransport
135
+ {
136
+ Uri = new ( webSocketUrl ) ,
137
+ }
138
+ }
139
+ . GetProxy < IAlgebra > ( )
140
+ . Ping ( ) ;
115
141
}
116
142
117
143
if ( pipeName is not null )
118
144
{
119
- yield return new NamedPipeClientBuilder < IAlgebra , IArithmetic > ( pipeName , sp )
120
- . RequestTimeout ( TimeSpan . FromHours ( 5 ) )
121
- . CallbackInstance ( callback )
122
- . Build ( )
123
- . Ping ( ) ;
145
+ yield return new IpcClient
146
+ {
147
+ Config = new ( )
148
+ {
149
+ ServiceProvider = sp ,
150
+ RequestTimeout = TimeSpan . FromHours ( 5 ) ,
151
+ Callbacks = new ( )
152
+ {
153
+ { typeof ( IArithmetic ) , callback }
154
+ }
155
+ } ,
156
+ Transport = new NamedPipeTransport ( )
157
+ {
158
+ PipeName = pipeName ,
159
+ }
160
+ }
161
+ . GetProxy < IAlgebra > ( )
162
+ . Ping ( ) ;
124
163
}
125
164
}
126
165
127
166
await Task . WhenAll ( EnumeratePings ( ) ) ;
128
-
167
+
129
168
Send ( SignalKind . ReadyToConnect ) ;
130
169
}
131
170
catch ( Exception ex )
@@ -135,7 +174,29 @@ IEnumerable<Task> EnumeratePings()
135
174
}
136
175
} ) ;
137
176
138
- await serviceHost . RunAsync ( sched ) ;
177
+ await ipcServer . WaitForStop ( ) ;
178
+
179
+ IEnumerable < ListenerConfig > EnumerateListeners ( string ? pipeName , string ? webSocketUrl )
180
+ {
181
+ if ( pipeName is not null )
182
+ {
183
+ yield return new NamedPipeListener ( ) { PipeName = pipeName } ;
184
+ }
185
+
186
+ if ( webSocketUrl is not null )
187
+ {
188
+ string url = CurateWebSocketUrl ( webSocketUrl ) ;
189
+ var accept = new HttpSysWebSocketsListener ( url ) . Accept ;
190
+ yield return new WebSocketListener ( ) { Accept = accept } ;
191
+ }
192
+
193
+ static string CurateWebSocketUrl ( string raw )
194
+ {
195
+ var builder = new UriBuilder ( raw ) ;
196
+ builder . Scheme = "http" ;
197
+ return builder . ToString ( ) ;
198
+ }
199
+ }
139
200
}
140
201
141
202
private class Arithmetic : IArithmetic
@@ -148,37 +209,6 @@ private class Arithmetic : IArithmetic
148
209
149
210
internal static class Extensions
150
211
{
151
- public static ServiceHostBuilder UseNamedPipesAndOrWebSockets ( this ServiceHostBuilder builder , string ? pipeName , string ? webSocketUrl )
152
- {
153
- if ( pipeName is null && webSocketUrl is null )
154
- {
155
- throw new ArgumentOutOfRangeException ( ) ;
156
- }
157
-
158
- if ( pipeName is not null )
159
- {
160
- builder = builder . UseNamedPipes ( new NamedPipeSettings ( pipeName ) ) ;
161
- }
162
-
163
- if ( webSocketUrl is not null )
164
- {
165
- string url = CurateWebSocketUrl ( webSocketUrl ) ;
166
- var accept = new HttpSysWebSocketsListener ( url ) . Accept ;
167
- WebSocketSettings settings = new ( accept ) ;
168
-
169
- builder = builder . UseWebSockets ( settings ) ;
170
- }
171
-
172
- return builder ;
173
- }
174
-
175
- private static string CurateWebSocketUrl ( string raw )
176
- {
177
- var builder = new UriBuilder ( raw ) ;
178
- builder . Scheme = "http" ;
179
- return builder . ToString ( ) ;
180
- }
181
-
182
212
public class HttpSysWebSocketsListener : IDisposable
183
213
{
184
214
HttpListener _httpListener = new ( ) ;
0 commit comments