using System; using System.Threading.Tasks; using LANCommander.SDK.Abstractions; using LANCommander.SDK.Extensions; using LANCommander.SDK.Rpc.Client; using LANCommander.SDK.Rpc.Server; using LANCommander.SDK.Services; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.Logging; namespace LANCommander.SDK.Rpc.Clients; internal partial class RpcSubscriber(ITokenProvider tokenProvider, ILogger logger) : IRpcSubscriber { private HubConnection _connection = default!; public async Task ConnectAsync(Uri serverAddress) { try { _connection = new HubConnectionBuilder() .WithUrl(serverAddress.Join("rpc"), options => { options.AccessTokenProvider = () => Task.FromResult(tokenProvider.GetToken().AccessToken); }) .Build(); RpcClient.Hub = _connection.ServerProxy(); _ = _connection.ClientRegistration(this); await _connection.StartAsync(); return true; } catch(Exception ex) { logger.LogError(ex, "Failed to connect to RPC server at {ServerAddress}", serverAddress); return false; } } public async Task DisconnectAsync() { try { await _connection.StopAsync(); return true; } catch (Exception ex) { logger.LogError(ex, "Failed to disconnect from RPC server"); return false; } } }