diff options
| -rw-r--r-- | src/slskd/Application/Application.cs | 637 |
1 files changed, 325 insertions, 312 deletions
diff --git a/src/slskd/Application/Application.cs b/src/slskd/Application/Application.cs index 8a4f86ac..73c82d11 100644 --- a/src/slskd/Application/Application.cs +++ b/src/slskd/Application/Application.cs @@ -45,8 +45,7 @@ namespace slskd public Application( OptionsAtStartup optionsAtStartup, IOptionsMonitor<Options> optionsMonitor, - IStateMonitor<ApplicationState> stateMonitor, - IStateMutator<ApplicationState> stateMutator, + IManagedState<ApplicationState> state, ITransferTracker transferTracker, IBrowseTracker browseTracker, IConversationTracker conversationTracker, @@ -61,9 +60,8 @@ namespace slskd PreviousOptions = OptionsMonitor.CurrentValue; - StateMonitor = stateMonitor; - StateMonitor.OnChange(state => StateMonitor_OnChange(state)); - StateMutator = stateMutator; + State = state; + State.OnChange(state => State_OnChange(state)); SharedFileCache = sharedFileCache; SharedFileCache.StateMonitor.OnChange(state => SharedFileCacheState_OnChange(state)); @@ -147,65 +145,26 @@ namespace slskd private IConversationTracker ConversationTracker { get; set; } private ILogger Logger { get; set; } = Log.ForContext<Application>(); private ConcurrentDictionary<string, ILogger> Loggers { get; } = new ConcurrentDictionary<string, ILogger>(); - private IOptionsMonitor<Options> OptionsMonitor { get; set; } private Options Options => OptionsMonitor.CurrentValue; private OptionsAtStartup OptionsAtStartup { get; set; } + private IOptionsMonitor<Options> OptionsMonitor { get; set; } + private ReaderWriterLockSlim OptionsSyncRoot { get; } = new ReaderWriterLockSlim(); private Options PreviousOptions { get; set; } + private IPushbulletService Pushbullet { get; } private IRoomTracker RoomTracker { get; set; } - private IStateMonitor<ApplicationState> StateMonitor { get; } - private IStateMutator<ApplicationState> StateMutator { get; } - private ApplicationState State => StateMonitor.CurrentValue; private ISharedFileCache SharedFileCache { get; set; } - private ITransferTracker TransferTracker { get; set; } - private IPushbulletService Pushbullet { get; } - private ReaderWriterLockSlim OptionsSyncRoot { get; } = new ReaderWriterLockSlim(); private DateTime SharesRefreshStarted { get; set; } + private IManagedState<ApplicationState> State { get; } + private ITransferTracker TransferTracker { get; set; } - public async Task StartAsync(CancellationToken cancellationToken) - { - Logger.Information("Client started"); - Logger.Information("Listening on port {Port}", OptionsAtStartup.Soulseek.ListenPort); - - if (OptionsAtStartup.Soulseek.Connection.Proxy.Enabled) - { - Logger.Information($"Using Proxy {OptionsAtStartup.Soulseek.Connection.Proxy.Address}:{OptionsAtStartup.Soulseek.Connection.Proxy.Port}"); - } - - if (!OptionsAtStartup.NoVersionCheck) - { - _ = CheckVersionAsync(); - } + public Task AcknowledgePrivateMessageAsync(int id) => SoulseekClient.AcknowledgePrivateMessageAsync(id); - if (OptionsAtStartup.NoShareScan) - { - Logger.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); - } - else - { - _ = SharedFileCache.FillAsync(); - } + public Task AddPrivateRoomMemberAsync(string roomName, string username) + => SoulseekClient.AddPrivateRoomMemberAsync(roomName, username); - if (OptionsAtStartup.NoConnect) - { - Logger.Warning("Not connecting to the Soulseek server; 'no-connect' option is enabled"); - } - else if (string.IsNullOrEmpty(OptionsAtStartup.Soulseek.Username) || string.IsNullOrEmpty(OptionsAtStartup.Soulseek.Password)) - { - Logger.Warning($"Not connecting to the Soulseek server; username and/or password invalid. Specify valid credentials and manually connect, or update config and restart."); - } - else - { - await Client.ConnectAsync(OptionsAtStartup.Soulseek.Username, OptionsAtStartup.Soulseek.Password).ConfigureAwait(false); - } - } + public Task AddUserAsync(string username) => SoulseekClient.AddUserAsync(username); - public Task StopAsync(CancellationToken cancellationToken) - { - Client.Disconnect("Shutting down", new ApplicationShutdownException("Shutting down")); - Client.Dispose(); - Logger.Information("Client stopped"); - return Task.CompletedTask; - } + public Task<BrowseResponse> BrowseAsync(string username) => SoulseekClient.BrowseAsync(username); /// <summary> /// Gets the version of the latest application release. @@ -230,12 +189,12 @@ namespace slskd if (latestVersion > Version.Parse(Program.InformationalVersion)) { - StateMutator.SetValue(state => state with { Version = state.Version with { Latest = latestVersion.ToString(), IsUpdateAvailable = true } }); + State.SetValue(state => state with { Version = state.Version with { Latest = latestVersion.ToString(), IsUpdateAvailable = true } }); Logger.Information("A new version is available! {CurrentVersion} -> {LatestVersion}", Program.InformationalVersion, latestVersion); } else { - StateMutator.SetValue(state => state with { Version = state.Version with { Latest = Program.InformationalVersion, IsUpdateAvailable = false } }); + State.SetValue(state => state with { Version = state.Version with { Latest = Program.InformationalVersion, IsUpdateAvailable = false } }); Logger.Information("Version {CurrentVersion} is up to date.", Program.InformationalVersion); } } @@ -261,224 +220,119 @@ namespace slskd public void Disconnect(string message = null, Exception exception = null) => Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message)); - /// <summary> - /// Re-scans shared directories. - /// </summary> - /// <returns>The operation context.</returns> - public Task RescanSharesAsync() => SharedFileCache.FillAsync(); - - public Task StartPublicChatAsync() => SoulseekClient.StartPublicChatAsync(); - public Task StopPublicChatAsync() => SoulseekClient.StopPublicChatAsync(); - public Task SendPrivateMessageAsync(string username, string message) => SoulseekClient.SendPrivateMessageAsync(username, message); - public Task AcknowledgePrivateMessageAsync(int id) => SoulseekClient.AcknowledgePrivateMessageAsync(id); - public Task SendRoomMessageAsync(string roomName, string message) - => SoulseekClient.SendRoomMessageAsync(roomName, message); - public Task SetRoomTickerAsync(string roomName, string message) - => SoulseekClient.SetRoomTickerAsync(roomName, message); - public Task AddPrivateRoomMemberAsync(string roomName, string username) - => SoulseekClient.AddPrivateRoomMemberAsync(roomName, username); - public Task<RoomList> GetRoomListAsync() => SoulseekClient.GetRoomListAsync(); - public Task LeaveRoomAsync(string roomName) => SoulseekClient.LeaveRoomAsync(roomName); public Task DownloadAsync(string username, string filename, Stream outputStream, long? size, long startOffset = 0, int? token = null, TransferOptions options = null, CancellationToken? cancellationToken = null) => SoulseekClient.DownloadAsync(username, filename, outputStream, size, startOffset, token, options, cancellationToken); + public Task<int> GetDownloadPlaceInQueueAsync(string username, string filename) => SoulseekClient.GetDownloadPlaceInQueueAsync(username, filename); - public Task<IPEndPoint> GetUserEndPointAsync(string username) => SoulseekClient.GetUserEndPointAsync(username); - public Task<UserStatus> GetUserStatusAsync(string username) => SoulseekClient.GetUserStatusAsync(username); - public Task GrantUserPrivilegesAsync(string username, int days) => SoulseekClient.GrantUserPrivilegesAsync(username, days); - public Task<bool> GetUserPrivilegedAsync(string username) => SoulseekClient.GetUserPrivilegedAsync(username); - public Task AddUserAsync(string username) => SoulseekClient.AddUserAsync(username); - public Task<BrowseResponse> BrowseAsync(string username) => SoulseekClient.BrowseAsync(username); - public Task<UserInfo> GetUserInfoAsync(string username) => SoulseekClient.GetUserInfoAsync(username); - public int GetNextToken() => SoulseekClient.GetNextToken(); - public Task<Soulseek.Search> SearchAsync(SearchQuery query, Action<SearchResponse> responseReceived, SearchScope scope = null, int? token = null, SearchOptions options = null, CancellationToken? cancellationToken = null) - => SoulseekClient.SearchAsync(query, responseReceived, scope, token, options, cancellationToken); - - private async Task OptionsMonitor_OnChange(Options newOptions) - { - // this code is known to fire more than once per update. i'm not sure - // whether these might be executed concurrently. lock to be safe, because - // we need to accurately track the last value of Options for diffing purposes. - // threading shenanigans here could lead to missed updates. - OptionsSyncRoot.EnterWriteLock(); - - try - { - var pendingRestart = false; - var pendingReconnect = false; - var soulseekRequiresReconnect = false; - - var diff = PreviousOptions.DiffWith(newOptions); - - // don't react to duplicate/no-change events - // https://github.com/slskd/slskd/issues/126 - if (!diff.Any()) - { - return; - } - - foreach (var (property, fqn, left, right) in diff) - { - static bool HasAttribute<T>(PropertyInfo property) => property.CustomAttributes.Any(a => a.AttributeType == typeof(T)); - var requiresRestart = HasAttribute<RequiresRestartAttribute>(property); - var requiresReconnect = HasAttribute<RequiresReconnectAttribute>(property); - - Logger.Debug($"{fqn} changed from '{left.ToJson() ?? "<null>"}' to '{right.ToJson() ?? "<null>"}'{(requiresRestart ? "; Restart required to take effect." : string.Empty)}{(requiresReconnect ? "; Reconnect required to take effect." : string.Empty)}"); - - pendingRestart |= requiresRestart; - pendingReconnect |= requiresReconnect; - } - - if (PreviousOptions.Directories.Shared.Except(newOptions.Directories.Shared).Any() - || newOptions.Directories.Shared.Except(PreviousOptions.Directories.Shared).Any()) - { - StateMutator.SetValue(state => state with { PendingShareRescan = true }); - Logger.Information("Shared directory configuration changed. Shares must be re-scanned for changes to take effect."); - } + public int GetNextToken() => SoulseekClient.GetNextToken(); - if (PreviousOptions.Filters.Share.Except(newOptions.Filters.Share).Any() - || newOptions.Filters.Share.Except(PreviousOptions.Filters.Share).Any()) - { - StateMutator.SetValue(state => state with { PendingShareRescan = true }); - Logger.Information("File filter configuration changed. Shares must be re-scanned for changes to take effect."); - } + public Task<RoomList> GetRoomListAsync() => SoulseekClient.GetRoomListAsync(); - if (PreviousOptions.Rooms.Except(newOptions.Rooms).Any() - || newOptions.Rooms.Except(PreviousOptions.Rooms).Any()) - { - Logger.Information("Room configuration changed. Joining any newly added rooms."); - _ = AutoJoinRoomsAsync(newOptions.Rooms); - } + public Task<IPEndPoint> GetUserEndPointAsync(string username) => SoulseekClient.GetUserEndPointAsync(username); - // determine whether any Soulseek options changed. if so, we need to construct a patch - // and invoke ReconfigureOptionsAsync(). - var slskDiff = PreviousOptions.Soulseek.DiffWith(newOptions.Soulseek); + public Task<UserInfo> GetUserInfoAsync(string username) => SoulseekClient.GetUserInfoAsync(username); - if (slskDiff.Any()) - { - var old = PreviousOptions.Soulseek; - var update = newOptions.Soulseek; + public Task<bool> GetUserPrivilegedAsync(string username) => SoulseekClient.GetUserPrivilegedAsync(username); - Logger.Debug("Soulseek options changed from {Previous} to {Current}", old.ToJson(), update.ToJson()); + public Task<UserStatus> GetUserStatusAsync(string username) => SoulseekClient.GetUserStatusAsync(username); - // determine whether any Connection options changed. if so, replace the whole object. - // Soulseek.NET doesn't offer a way to patch parts of connection options. the updates only affect - // new connections, so a partial patch doesn't make a lot of sense anyway. - var connectionDiff = old.Connection.DiffWith(update.Connection); + public Task GrantUserPrivilegesAsync(string username, int days) => SoulseekClient.GrantUserPrivilegesAsync(username, days); - ConnectionOptions connectionPatch = null; + public async Task<RoomData> JoinRoomAsync(string roomName) + { + Logger.Debug("Joining room {Room}", roomName); - if (connectionDiff.Any()) - { - var connection = update.Connection; + try + { + var data = await SoulseekClient.JoinRoomAsync(roomName); + Logger.Information("Joined room {Room}", roomName); + Logger.Debug("Room data for {Room}: {Info}", roomName, data.ToJson()); + return data; + } + catch (Exception ex) + { + Logger.Warning("Failed to join room {Room}: {Message}", roomName, ex.Message); + throw; + } + } - ProxyOptions proxyPatch = null; + public Task LeaveRoomAsync(string roomName) => SoulseekClient.LeaveRoomAsync(roomName); - if (connection.Proxy.Enabled) - { - proxyPatch = new ProxyOptions( - connection.Proxy.Address, - connection.Proxy.Port.Value, - connection.Proxy.Username, - connection.Proxy.Password); - } + /// <summary> + /// Re-scans shared directories. + /// </summary> + /// <returns>The operation context.</returns> + public Task RescanSharesAsync() => SharedFileCache.FillAsync(); - connectionPatch = new ConnectionOptions( - connection.Buffer.Read, - connection.Buffer.Write, - connection.Timeout.Connect, - connection.Timeout.Inactivity, - proxyOptions: proxyPatch); - } + public Task<Soulseek.Search> SearchAsync(SearchQuery query, Action<SearchResponse> responseReceived, SearchScope scope = null, int? token = null, SearchOptions options = null, CancellationToken? cancellationToken = null) + => SoulseekClient.SearchAsync(query, responseReceived, scope, token, options, cancellationToken); - var patch = new SoulseekClientOptionsPatch( - listenPort: old.ListenPort == update.ListenPort ? null : update.ListenPort, - enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled, - distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit, - serverConnectionOptions: connectionPatch, - peerConnectionOptions: connectionPatch, - transferConnectionOptions: connectionPatch, - incomingConnectionOptions: connectionPatch, - distributedConnectionOptions: connectionPatch); + public Task SendPrivateMessageAsync(string username, string message) => SoulseekClient.SendPrivateMessageAsync(username, message); - Logger.Debug("Patching Soulseek options with {Patch}", patch.ToJson()); + public Task SendRoomMessageAsync(string roomName, string message) + => SoulseekClient.SendRoomMessageAsync(roomName, message); - soulseekRequiresReconnect = await SoulseekClient.ReconfigureOptionsAsync(patch); - } + public Task SetRoomTickerAsync(string roomName, string message) + => SoulseekClient.SetRoomTickerAsync(roomName, message); - // require a reconnect if the client is connected and any options marked [RequiresReconnect] changed, - // OR if the call to reconfigure the client requires a reconnect - if ((Client.State.HasFlag(SoulseekClientStates.Connected) && pendingReconnect) || soulseekRequiresReconnect) - { - StateMutator.SetValue(state => state with { PendingReconnect = true }); - Logger.Information("One or more updated Soulseek options requires the client to be disconnected, then reconnected to the network to take effect."); - } + public async Task StartAsync(CancellationToken cancellationToken) + { + Logger.Information("Client started"); + Logger.Information("Listening on port {Port}", OptionsAtStartup.Soulseek.ListenPort); - if (pendingRestart) - { - StateMutator.SetValue(state => state with { PendingRestart = true }); - Logger.Information("One or more updated options requires an application restart to take effect."); - } + if (OptionsAtStartup.Soulseek.Connection.Proxy.Enabled) + { + Logger.Information($"Using Proxy {OptionsAtStartup.Soulseek.Connection.Proxy.Address}:{OptionsAtStartup.Soulseek.Connection.Proxy.Port}"); + } - Logger.Information("Options updated successfully."); + if (!OptionsAtStartup.NoVersionCheck) + { + _ = CheckVersionAsync(); } - catch (Exception ex) + + if (OptionsAtStartup.NoShareScan) { - Logger.Error(ex, "Failed to apply option update: {Message}", ex.Message); + Logger.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed."); } - finally + else { - PreviousOptions = OptionsMonitor.CurrentValue; - OptionsSyncRoot.ExitWriteLock(); + _ = SharedFileCache.FillAsync(); } - } - private void StateMonitor_OnChange((ApplicationState Previous, ApplicationState Current) state) - { - Logger.Debug("State changed from {Previous} to {Current}", state.Previous.ToJson(), state.Current.ToJson()); - } - - private void SharedFileCacheState_OnChange((SharedFileCacheState Previous, SharedFileCacheState Current) state) - { - if (!state.Previous.Filling && state.Current.Filling) + if (OptionsAtStartup.NoConnect) { - SharesRefreshStarted = DateTime.UtcNow; - - StateMutator.SetValue(s => s with { SharedFileCache = state.Current }); - Logger.Information("Scanning shares"); + Logger.Warning("Not connecting to the Soulseek server; 'no-connect' option is enabled"); } - - var lastProgress = Math.Round(state.Previous.FillProgress * 100); - var currentProgress = Math.Round(state.Current.FillProgress * 100); - - if (lastProgress != currentProgress && Math.Round(currentProgress, 0) % 10 == 0) + else if (string.IsNullOrEmpty(OptionsAtStartup.Soulseek.Username) || string.IsNullOrEmpty(OptionsAtStartup.Soulseek.Password)) { - StateMutator.SetValue(s => s with { SharedFileCache = state.Current }); - Logger.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, state.Current.Files); + Logger.Warning($"Not connecting to the Soulseek server; username and/or password invalid. Specify valid credentials and manually connect, or update config and restart."); } - - if (state.Previous.Filling && !state.Current.Filling) + else { - StateMutator.SetValue(s => s with { SharedFileCache = state.Current }); + await Client.ConnectAsync(OptionsAtStartup.Soulseek.Username, OptionsAtStartup.Soulseek.Password).ConfigureAwait(false); + } + } - if (state.Current.Faulted) - { - Logger.Error("Failed to scan shares."); - } - else - { - StateMutator.SetValue(s => s with { PendingShareRescan = false }); - Logger.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", state.Current.Directories, state.Current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); + public Task StartPublicChatAsync() => SoulseekClient.StartPublicChatAsync(); - SharesRefreshStarted = default; + public Task StopAsync(CancellationToken cancellationToken) + { + Client.Disconnect("Shutting down", new ApplicationShutdownException("Shutting down")); + Client.Dispose(); + Logger.Information("Client stopped"); + return Task.CompletedTask; + } - if (Client.State.HasFlag(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn)) - { - _ = SoulseekClient.SetSharedCountsAsync(State.SharedFileCache.Directories, State.SharedFileCache.Files); - } - } - } + public Task StopPublicChatAsync() => SoulseekClient.StopPublicChatAsync(); + + private Task AutoJoinRoomsAsync(string[] rooms) + { + // don't try to join rooms that we've already joined. the server returns no response in this case and the client + // believes the request has timed out. + var unjoined = rooms.Where(room => !RoomTracker.Rooms.ContainsKey(room)); + return JoinRoomsAsync(unjoined); } /// <summary> @@ -500,6 +354,11 @@ namespace slskd BrowseTracker.AddOrUpdate(args.Username, args); } + private void Client_Connected(object sender, EventArgs e) + { + Logger.Information("Connected to the Soulseek server"); + } + private void Client_DiagnosticGenerated(object sender, DiagnosticEventArgs args) { static LogEventLevel TranslateLogLevel(DiagnosticLevel diagnosticLevel) => diagnosticLevel switch @@ -516,81 +375,11 @@ namespace slskd logger.Write(TranslateLogLevel(args.Level), "{@Message}", args.Message); } - private void Client_StateChanged(object sender, SoulseekClientStateChangedEventArgs e) - { - StateMutator.SetValue(state => state with { Server = new ServerState() { Address = Client.Address, IPEndPoint = Client.IPEndPoint, State = Client.State, Username = Client.Username } }); - } - - private void Client_Connected(object sender, EventArgs e) - { - Logger.Information("Connected to the Soulseek server"); - } - - private async void Client_LoggedIn(object sender, EventArgs e) - { - Logger.Information("Logged in to the Soulseek server as {Username}", Client.Username); - - // send whatever counts we have currently. we'll probably connect before the cache is primed, - // so these will be zero initially, but we'll update them when the cache is filled. - _ = SoulseekClient.SetSharedCountsAsync(State.SharedFileCache.Directories, State.SharedFileCache.Files); - - // rejoin any rooms that we might have been in during the previous session - await JoinRoomsAsync(RoomTracker.Rooms.Keys); - - // join the rooms configured for autojoin. always do this after rejoining has completed. - await AutoJoinRoomsAsync(OptionsMonitor.CurrentValue.Rooms); - } - - private Task AutoJoinRoomsAsync(string[] rooms) - { - // don't try to join rooms that we've already joined. the server returns no response in this case - // and the client believes the request has timed out. - var unjoined = rooms.Where(room => !RoomTracker.Rooms.ContainsKey(room)); - return JoinRoomsAsync(unjoined); - } - - private async Task JoinRoomsAsync(IEnumerable<string> roomNames) - { - var tasks = new List<Task>(); - - foreach (var room in roomNames) - { - tasks.Add(JoinRoomAsync(room)); - } - - try - { - await Task.WhenAll(tasks); - } - catch (Exception ex) - { - Logger.Debug(ex, "Caught Exception in JoinRoomsAsync"); - } - } - - public async Task<RoomData> JoinRoomAsync(string roomName) - { - Logger.Debug("Joining room {Room}", roomName); - - try - { - var data = await SoulseekClient.JoinRoomAsync(roomName); - Logger.Information("Joined room {Room}", roomName); - Logger.Debug("Room data for {Room}: {Info}", roomName, data.ToJson()); - return data; - } - catch (Exception ex) - { - Logger.Warning("Failed to join room {Room}: {Message}", roomName, ex.Message); - throw; - } - } - private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args) { - if (State.PendingReconnect) + if (State.CurrentValue.PendingReconnect) { - StateMutator.SetValue(state => state with { PendingReconnect = false }); + State.SetValue(state => state with { PendingReconnect = false }); } if (args.Exception is ObjectDisposedException || args.Exception is ApplicationShutdownException) @@ -635,8 +424,9 @@ namespace slskd try { - // reconnect with the latest configuration values we have for username and password, instead of the options that were - // captured at startup. if a user has updated these values prior to the disconnect, the changes will take effect now. + // reconnect with the latest configuration values we have for username and password, instead of the + // options that were captured at startup. if a user has updated these values prior to the disconnect, the + // changes will take effect now. await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); break; } @@ -649,6 +439,21 @@ namespace slskd } } + private async void Client_LoggedIn(object sender, EventArgs e) + { + Logger.Information("Logged in to the Soulseek server as {Username}", Client.Username); + + // send whatever counts we have currently. we'll probably connect before the cache is primed, so these will be zero + // initially, but we'll update them when the cache is filled. + _ = SoulseekClient.SetSharedCountsAsync(State.CurrentValue.SharedFileCache.Directories, State.CurrentValue.SharedFileCache.Files); + + // rejoin any rooms that we might have been in during the previous session + await JoinRoomsAsync(RoomTracker.Rooms.Keys); + + // join the rooms configured for autojoin. always do this after rejoining has completed. + await AutoJoinRoomsAsync(OptionsMonitor.CurrentValue.Rooms); + } + private void Client_PrivateMessageRecieved(object sender, PrivateMessageReceivedEventArgs args) { ConversationTracker.AddOrUpdate(args.Username, PrivateMessage.FromEventArgs(args)); @@ -695,6 +500,11 @@ namespace slskd } } + private void Client_StateChanged(object sender, SoulseekClientStateChangedEventArgs e) + { + State.SetValue(state => state with { Server = new ServerState() { Address = Client.Address, IPEndPoint = Client.IPEndPoint, State = Client.State, Username = Client.Username } }); + } + private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) { // this is really verbose. Console.WriteLine($"[{args.Transfer.Direction.ToString().ToUpper()}] @@ -794,6 +604,162 @@ namespace slskd return Task.CompletedTask; } + private async Task JoinRoomsAsync(IEnumerable<string> roomNames) + { + var tasks = new List<Task>(); + + foreach (var room in roomNames) + { + tasks.Add(JoinRoomAsync(room)); + } + + try + { + await Task.WhenAll(tasks); + } + catch (Exception ex) + { + Logger.Debug(ex, "Caught Exception in JoinRoomsAsync"); + } + } + + private async Task OptionsMonitor_OnChange(Options newOptions) + { + // this code is known to fire more than once per update. i'm not sure whether these might be executed concurrently. + // lock to be safe, because we need to accurately track the last value of Options for diffing purposes. threading + // shenanigans here could lead to missed updates. + OptionsSyncRoot.EnterWriteLock(); + + try + { + var pendingRestart = false; + var pendingReconnect = false; + var soulseekRequiresReconnect = false; + + var diff = PreviousOptions.DiffWith(newOptions); + + // don't react to duplicate/no-change events https://github.com/slskd/slskd/issues/126 + if (!diff.Any()) + { + return; + } + + foreach (var (property, fqn, left, right) in diff) + { + static bool HasAttribute<T>(PropertyInfo property) => property.CustomAttributes.Any(a => a.AttributeType == typeof(T)); + + var requiresRestart = HasAttribute<RequiresRestartAttribute>(property); + var requiresReconnect = HasAttribute<RequiresReconnectAttribute>(property); + + Logger.Debug($"{fqn} changed from '{left.ToJson() ?? "<null>"}' to '{right.ToJson() ?? "<null>"}'{(requiresRestart ? "; Restart required to take effect." : string.Empty)}{(requiresReconnect ? "; Reconnect required to take effect." : string.Empty)}"); + + pendingRestart |= requiresRestart; + pendingReconnect |= requiresReconnect; + } + + if (PreviousOptions.Directories.Shared.Except(newOptions.Directories.Shared).Any() + || newOptions.Directories.Shared.Except(PreviousOptions.Directories.Shared).Any()) + { + State.SetValue(state => state with { PendingShareRescan = true }); + Logger.Information("Shared directory configuration changed. Shares must be re-scanned for changes to take effect."); + } + + if (PreviousOptions.Filters.Share.Except(newOptions.Filters.Share).Any() + || newOptions.Filters.Share.Except(PreviousOptions.Filters.Share).Any()) + { + State.SetValue(state => state with { PendingShareRescan = true }); + Logger.Information("File filter configuration changed. Shares must be re-scanned for changes to take effect."); + } + + if (PreviousOptions.Rooms.Except(newOptions.Rooms).Any() + || newOptions.Rooms.Except(PreviousOptions.Rooms).Any()) + { + Logger.Information("Room configuration changed. Joining any newly added rooms."); + _ = AutoJoinRoomsAsync(newOptions.Rooms); + } + + // determine whether any Soulseek options changed. if so, we need to construct a patch and invoke ReconfigureOptionsAsync(). + var slskDiff = PreviousOptions.Soulseek.DiffWith(newOptions.Soulseek); + + if (slskDiff.Any()) + { + var old = PreviousOptions.Soulseek; + var update = newOptions.Soulseek; + + Logger.Debug("Soulseek options changed from {Previous} to {Current}", old.ToJson(), update.ToJson()); + + // determine whether any Connection options changed. if so, replace the whole object. Soulseek.NET doesn't + // offer a way to patch parts of connection options. the updates only affect new connections, so a partial + // patch doesn't make a lot of sense anyway. + var connectionDiff = old.Connection.DiffWith(update.Connection); + + ConnectionOptions connectionPatch = null; + + if (connectionDiff.Any()) + { + var connection = update.Connection; + + ProxyOptions proxyPatch = null; + + if (connection.Proxy.Enabled) + { + proxyPatch = new ProxyOptions( + connection.Proxy.Address, + connection.Proxy.Port.Value, + connection.Proxy.Username, + connection.Proxy.Password); + } + + connectionPatch = new ConnectionOptions( + connection.Buffer.Read, + connection.Buffer.Write, + connection.Timeout.Connect, + connection.Timeout.Inactivity, + proxyOptions: proxyPatch); + } + + var patch = new SoulseekClientOptionsPatch( + listenPort: old.ListenPort == update.ListenPort ? null : update.ListenPort, + enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled, + distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit, + serverConnectionOptions: connectionPatch, + peerConnectionOptions: connectionPatch, + transferConnectionOptions: connectionPatch, + incomingConnectionOptions: connectionPatch, + distributedConnectionOptions: connectionPatch); + + Logger.Debug("Patching Soulseek options with {Patch}", patch.ToJson()); + + soulseekRequiresReconnect = await SoulseekClient.ReconfigureOptionsAsync(patch); + } + + // require a reconnect if the client is connected and any options marked [RequiresReconnect] changed, OR if the + // call to reconfigure the client requires a reconnect + if ((Client.State.HasFlag(SoulseekClientStates.Connected) && pendingReconnect) || soulseekRequiresReconnect) + { + State.SetValue(state => state with { PendingReconnect = true }); + Logger.Information("One or more updated Soulseek options requires the client to be disconnected, then reconnected to the network to take effect."); + } + + if (pendingRestart) + { + State.SetValue(state => state with { PendingRestart = true }); + Logger.Information("One or more updated options requires an application restart to take effect."); + } + + Logger.Information("Options updated successfully."); + } + catch (Exception ex) + { + Logger.Error(ex, "Failed to apply option update: {Message}", ex.Message); + } + finally + { + PreviousOptions = OptionsMonitor.CurrentValue; + OptionsSyncRoot.ExitWriteLock(); + } + } + /// <summary> /// Creates and returns a <see cref="Response"/> in response to the given <paramref name="query"/>. /// </summary> @@ -836,6 +802,53 @@ namespace slskd return null; } + private void SharedFileCacheState_OnChange((SharedFileCacheState Previous, SharedFileCacheState Current) state) + { + if (!state.Previous.Filling && state.Current.Filling) + { + SharesRefreshStarted = DateTime.UtcNow; + + State.SetValue(s => s with { SharedFileCache = state.Current }); + Logger.Information("Scanning shares"); + } + + var lastProgress = Math.Round(state.Previous.FillProgress * 100); + var currentProgress = Math.Round(state.Current.FillProgress * 100); + + if (lastProgress != currentProgress && Math.Round(currentProgress, 0) % 10 == 0) + { + State.SetValue(s => s with { SharedFileCache = state.Current }); + Logger.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, state.Current.Files); + } + + if (state.Previous.Filling && !state.Current.Filling) + { + State.SetValue(s => s with { SharedFileCache = state.Current }); + + if (state.Current.Faulted) + { + Logger.Error("Failed to scan shares."); + } + else + { + State.SetValue(s => s with { PendingShareRescan = false }); + Logger.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", state.Current.Directories, state.Current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds); + + SharesRefreshStarted = default; + + if (Client.State.HasFlag(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn)) + { + _ = SoulseekClient.SetSharedCountsAsync(State.CurrentValue.SharedFileCache.Directories, State.CurrentValue.SharedFileCache.Files); + } + } + } + } + + private void State_OnChange((ApplicationState Previous, ApplicationState Current) state) + { + Logger.Debug("State changed from {Previous} to {Current}", state.Previous.ToJson(), state.Current.ToJson()); + } + /// <summary> /// Creates and returns a <see cref="UserInfo"/> object in response to a remote request. /// </summary> |
