1+ using System . Diagnostics ;
12using System . IO . Pipes ;
23using System . Net . Http . Json ;
34using System . Text . Json ;
45using UniGetUI . Core . Data ;
56using UniGetUI . Core . Logging ;
6- using UniGetUI . Core . SettingsEngine ;
77
88namespace UniGetUI . Interface ;
99
@@ -17,13 +17,42 @@ public sealed class BackgroundApiClient : IDisposable
1717 private BackgroundApiClient ( BackgroundApiTransportOptions transportOptions , string ? token = null )
1818 {
1919 TransportOptions = transportOptions ;
20- _token = token ?? Settings . GetValue ( Settings . K . CurrentSessionToken ) ;
20+ _token = token ?? string . Empty ;
2121 _httpClient = CreateHttpClient ( transportOptions ) ;
2222 }
2323
2424 public static BackgroundApiClient CreateForCli ( IReadOnlyList < string > ? args = null )
2525 {
26- return new BackgroundApiClient ( BackgroundApiTransportOptions . LoadForClient ( args ) ) ;
26+ args ??= Environment . GetCommandLineArgs ( ) ;
27+ BackgroundApiTransportOptions requestedOptions = BackgroundApiTransportOptions . LoadForClient ( args ) ;
28+
29+ if ( BackgroundApiTransportOptions . HasExplicitClientOverride ( args ) )
30+ {
31+ var matchingRegistrations = BackgroundApiTransportOptions . LoadPersistedRegistrations ( )
32+ . Where ( candidate => candidate . Matches ( requestedOptions ) )
33+ . ToArray ( ) ;
34+ var registration = SelectLiveRegistration (
35+ matchingRegistrations
36+ ) ;
37+
38+ return new BackgroundApiClient (
39+ requestedOptions ,
40+ registration ? . Token ?? matchingRegistrations . FirstOrDefault ( ) ? . Token
41+ ) ;
42+ }
43+
44+ var preferredRegistration = SelectLiveRegistration (
45+ BackgroundApiTransportOptions . OrderRegistrationsForCliSelection (
46+ BackgroundApiTransportOptions . LoadPersistedRegistrations ( )
47+ )
48+ ) ;
49+
50+ return preferredRegistration is not null
51+ ? new BackgroundApiClient (
52+ preferredRegistration . ToTransportOptions ( ) ,
53+ preferredRegistration . Token
54+ )
55+ : new BackgroundApiClient ( requestedOptions ) ;
2756 }
2857
2958 public async Task < BackgroundApiStatus > GetStatusAsync ( )
@@ -1433,6 +1462,58 @@ or TaskCanceledException
14331462 or OperationCanceledException ;
14341463 }
14351464
1465+ private static BackgroundApiEndpointRegistration ? SelectLiveRegistration (
1466+ IEnumerable < BackgroundApiEndpointRegistration > candidates
1467+ )
1468+ {
1469+ foreach ( BackgroundApiEndpointRegistration candidate in candidates )
1470+ {
1471+ if ( candidate . ProcessId > 0 && ! IsProcessRunning ( candidate . ProcessId ) )
1472+ {
1473+ BackgroundApiTransportOptions . DeletePersistedMetadata ( candidate . SessionId ) ;
1474+ continue ;
1475+ }
1476+
1477+ if ( IsEndpointAlive ( candidate ) )
1478+ {
1479+ return candidate ;
1480+ }
1481+
1482+ BackgroundApiTransportOptions . DeletePersistedMetadata ( candidate . SessionId ) ;
1483+ }
1484+
1485+ return null ;
1486+ }
1487+
1488+ private static bool IsEndpointAlive ( BackgroundApiEndpointRegistration candidate )
1489+ {
1490+ try
1491+ {
1492+ using var client = new BackgroundApiClient ( candidate . ToTransportOptions ( ) , candidate . Token ) ;
1493+ return client . GetStatusAsync ( ) . GetAwaiter ( ) . GetResult ( ) . Running ;
1494+ }
1495+ catch ( Exception ex ) when ( IsConnectivityException ( ex ) || ex is InvalidOperationException )
1496+ {
1497+ Logger . Debug (
1498+ $ "Background API registration { candidate . SessionId } probe failed: { ex . Message } "
1499+ ) ;
1500+ return false ;
1501+ }
1502+ }
1503+
1504+ private static bool IsProcessRunning ( int processId )
1505+ {
1506+ try
1507+ {
1508+ using Process process = Process . GetProcessById ( processId ) ;
1509+ return ! process . HasExited ;
1510+ }
1511+ catch ( Exception )
1512+ {
1513+ return false ;
1514+ }
1515+ }
1516+
14361517 private void EnsureTokenAvailable ( )
14371518 {
14381519 if ( string . IsNullOrWhiteSpace ( _token ) )
0 commit comments