1
1
namespace UiPath . Ipc ;
2
2
3
- using System . Linq . Expressions ;
4
- using ServiceClientProperFactory = Func < ClientBase , Type , ServiceClient > ;
5
-
6
3
internal abstract class ServiceClient : IDisposable
7
4
{
8
5
#region " NonGeneric-Generic adapter cache "
@@ -19,7 +16,6 @@ private static InvokeDelegate CreateInvokeDelegate(Type returnType)
19
16
#endregion
20
17
21
18
protected abstract TimeSpan RequestTimeout { get ; }
22
- protected abstract ConnectionFactory ? ConnectionFactory { get ; }
23
19
protected abstract BeforeCallHandler ? BeforeCall { get ; }
24
20
protected abstract ILogger ? Log { get ; }
25
21
protected abstract string DebugName { get ; }
@@ -151,47 +147,11 @@ private void Dispose(bool disposing)
151
147
public override string ToString ( ) => DebugName ;
152
148
}
153
149
154
- internal static class ServiceClientProper
155
- {
156
- private static readonly ConcurrentDictionary < Type , ServiceClientProperFactory > CachedFactories = new ( ) ;
157
- private static ServiceClientProperFactory GetFactory ( Type clientType ) => CachedFactories . GetOrAdd ( clientType , CreateFactory ) ;
158
- private static ServiceClientProperFactory CreateFactory ( Type clientType )
159
- {
160
- if ( clientType
161
- . GetInterfaces ( )
162
- . SingleOrDefault ( x => x . IsGenericType && x . GetGenericTypeDefinition ( ) == typeof ( IClient < , > ) )
163
- ? . GetGenericArguments ( )
164
- is not [ var clientStateType , var clientType2 ] || clientType2 != clientType )
165
- {
166
- throw new ArgumentOutOfRangeException ( nameof ( clientType ) , "The client implements 0 or more than 1 IClient<,> interfaces or the single interface's 2nd generic argument is not the client type itself." ) ;
167
- }
168
-
169
- var ctor = typeof ( ServiceClientProper < , > )
170
- . MakeGenericType ( clientType , clientStateType )
171
- . GetConstructor ( [ clientType , typeof ( Type ) ] ) ! ;
172
-
173
- var paramofClientBase = Expression . Parameter ( typeof ( ClientBase ) ) ;
174
- var paramofType = Expression . Parameter ( typeof ( Type ) ) ;
175
- return Expression . Lambda < ServiceClientProperFactory > (
176
- Expression . New (
177
- ctor ,
178
- Expression . Convert ( paramofClientBase , clientType ) ,
179
- paramofType ) ,
180
- paramofClientBase ,
181
- paramofType ) . Compile ( ) ;
182
- }
183
-
184
- public static ServiceClient Create ( ClientBase client , Type proxyType )
185
- => GetFactory ( client . GetType ( ) ) ( client , proxyType ) ;
186
- }
187
-
188
- internal sealed class ServiceClientProper < TClient , TClientState > : ServiceClient
189
- where TClient : ClientBase , IClient < TClientState , TClient >
190
- where TClientState : class , IClientState < TClient , TClientState > , new ( )
150
+ internal sealed class ServiceClientProper : ServiceClient
191
151
{
192
152
private readonly FastAsyncLock _lock = new ( ) ;
193
- private readonly TClientState _clientState = new ( ) ;
194
- private readonly TClient _client ;
153
+ private readonly IpcClient _client ;
154
+ private readonly IClientState _clientState ;
195
155
196
156
private Connection ? _latestConnection ;
197
157
private Server ? _latestServer ;
@@ -222,9 +182,10 @@ private Connection? LatestConnection
222
182
223
183
public override Stream ? Network => LatestConnection ? . Network ;
224
184
225
- public ServiceClientProper ( TClient client , Type interfaceType ) : base ( interfaceType )
185
+ public ServiceClientProper ( IpcClient client , Type interfaceType ) : base ( interfaceType )
226
186
{
227
187
_client = client ;
188
+ _clientState = client . Transport . CreateState ( ) ;
228
189
}
229
190
230
191
public override async ValueTask CloseConnection ( )
@@ -248,21 +209,15 @@ public override async ValueTask CloseConnection()
248
209
}
249
210
250
211
LatestConnection = new Connection ( await Connect ( ct ) , Serializer , Log , DebugName ) ;
251
- var router = new Router ( _client . CreateCallbackRouterConfig ( ) , _client . ServiceProvider ) ;
252
- _latestServer = new Server ( router , _client . RequestTimeout , LatestConnection ) ;
212
+ var router = new Router ( _client . Config . CreateCallbackRouterConfig ( ) , _client . Config . ServiceProvider ) ;
213
+ _latestServer = new Server ( router , _client . Config . RequestTimeout , LatestConnection ) ;
253
214
LatestConnection . Listen ( ) . LogException ( Log , DebugName ) ;
254
215
return ( LatestConnection , newlyConnected : true ) ;
255
216
}
256
217
}
257
218
258
219
private async Task < Network > Connect ( CancellationToken ct )
259
220
{
260
- if ( ConnectionFactory is not null
261
- && await ConnectionFactory ( _clientState . Network , ct ) is { } userProvidedNetwork )
262
- {
263
- return userProvidedNetwork ;
264
- }
265
-
266
221
await _clientState . Connect ( _client , ct ) ;
267
222
268
223
if ( _clientState . Network is not { } network )
@@ -273,12 +228,11 @@ private async Task<Network> Connect(CancellationToken ct)
273
228
return network ;
274
229
}
275
230
276
- protected override TimeSpan RequestTimeout => _client . RequestTimeout ;
277
- protected override ConnectionFactory ? ConnectionFactory => _client . ConnectionFactory ;
278
- protected override BeforeCallHandler ? BeforeCall => _client . BeforeCall ;
279
- protected override ILogger ? Log => _client . Logger ;
280
- protected override string DebugName => _client . ToString ( ) ;
281
- protected override ISerializer ? Serializer => _client . Serializer ;
231
+ protected override TimeSpan RequestTimeout => _client . Config . RequestTimeout ;
232
+ protected override BeforeCallHandler ? BeforeCall => _client . Config . BeforeCall ;
233
+ protected override ILogger ? Log => _client . Config . Logger ;
234
+ protected override string DebugName => _client . Transport . ToString ( ) ;
235
+ protected override ISerializer ? Serializer => _client . Config . Serializer ;
282
236
}
283
237
284
238
internal sealed class ServiceClientForCallback : ServiceClient
@@ -298,29 +252,8 @@ public ServiceClientForCallback(Connection connection, Listener listener, Type i
298
252
=> Task . FromResult ( ( _connection , newlyConnected : false ) ) ;
299
253
300
254
protected override TimeSpan RequestTimeout => _listener . Config . RequestTimeout ;
301
- protected override ConnectionFactory ? ConnectionFactory => null ;
302
255
protected override BeforeCallHandler ? BeforeCall => null ;
303
256
protected override ILogger ? Log => null ;
304
257
protected override string DebugName => $ "ReverseClient for { _listener } ";
305
258
protected override ISerializer ? Serializer => null ;
306
259
}
307
-
308
- public class IpcProxy : DispatchProxy , IDisposable
309
- {
310
- internal ServiceClient ServiceClient { get ; set ; } = null ! ;
311
-
312
- protected override object ? Invoke ( MethodInfo ? targetMethod , object ? [ ] ? args )
313
- => ServiceClient . Invoke ( targetMethod ! , args ! ) ;
314
-
315
- public void Dispose ( ) => ServiceClient ? . Dispose ( ) ;
316
-
317
- public ValueTask CloseConnection ( ) => ServiceClient . CloseConnection ( ) ;
318
-
319
- public event EventHandler ConnectionClosed
320
- {
321
- add => ServiceClient . ConnectionClosed += value ;
322
- remove => ServiceClient . ConnectionClosed -= value ;
323
- }
324
-
325
- public Stream ? Network => ServiceClient . Network ;
326
- }
0 commit comments