From f3529715849c1116d98f6b6496fe253b5e15363f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 16:44:44 -0600 Subject: add network state --- src/slskd/Core/State.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src') diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index f3fbf03e..8077f685 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -21,6 +21,7 @@ namespace slskd using System.Collections.Generic; using System.Net; using System.Text.Json.Serialization; + using slskd.Network; using slskd.Users; using Soulseek; @@ -62,6 +63,19 @@ namespace slskd public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); } + public record NetworkState + { + public OperationMode Mode { get; init; } + public NetworkControllerState Controller { get; init; } + public IReadOnlyCollection Agents { get; init; } + } + + public record NetworkControllerState + { + public string Address { get; init; } + public NetworkClientState State { get; init; } + } + public record UserState { public string Username { get; init; } -- cgit v1.2.3 From 8811bd5f08cbc83129ef1b692cad64b10cde36fb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 16:45:29 -0600 Subject: remove network client from DI and instead access it through service --- src/slskd/Application.cs | 13 ++++++------- src/slskd/Program.cs | 1 - 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 8edacac6..5e4faad4 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -88,8 +88,8 @@ namespace slskd IRoomService roomService, IUserService userService, IShareService shareService, - INetworkClient networkClient, IPushbulletService pushbulletService, + INetworkService networkService, IHubContext applicationHub, IHubContext logHub) { @@ -134,12 +134,11 @@ namespace slskd RoomService = roomService; Users = userService; ApplicationHub = applicationHub; + Network = networkService; LogHub = logHub; Program.LogEmitted += (_, log) => LogHub.EmitLogAsync(log); - NetworkClient = networkClient; - Client = soulseekClient; Client.DiagnosticGenerated += Client_DiagnosticGenerated; @@ -179,7 +178,6 @@ namespace slskd private static bool ShuttingDown { get; set; } = false; private ISoulseekClient Client { get; set; } - private INetworkClient NetworkClient { get; set; } private IRoomService RoomService { get; set; } private IBrowseTracker BrowseTracker { get; set; } private IConnectionWatchdog ConnectionWatchdog { get; } @@ -199,6 +197,7 @@ namespace slskd private IHubContext LogHub { get; set; } private IUserService Users { get; set; } private IShareService Shares { get; set; } + private INetworkService Network { get; set; } private IMemoryCache Cache { get; set; } = new MemoryCache(new MemoryCacheOptions()); private IEnumerable CompiledSearchResponseFilters { get; set; } private IEnumerable ActiveDownloadIdsAtPreviousShutdown { get; set; } = Enumerable.Empty(); @@ -400,13 +399,13 @@ namespace slskd else if (OptionsAtStartup.Network.Mode.ToEnum() == OperationMode.Agent && !OptionsAtStartup.Flags.DualNetworkMode) { Log.Information("Running in Agent mode; not connecting to the Soulseek server."); - await NetworkClient.StartAsync(cancellationToken); + await Network.Client.StartAsync(cancellationToken); } else { if (OptionsAtStartup.Flags.DualNetworkMode) { - _ = NetworkClient.StartAsync(cancellationToken); + _ = Network.Client.StartAsync(cancellationToken); } await Client.ConnectAsync(OptionsAtStartup.Soulseek.Username, OptionsAtStartup.Soulseek.Password).ConfigureAwait(false); @@ -1081,7 +1080,7 @@ namespace slskd if (rebuildBrowseCache) { _ = CacheBrowseResponse(); - _ = NetworkClient.SynchronizeAsync(); + _ = Network.Client.SynchronizeAsync(); } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 5335990a..3a4e923f 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -536,7 +536,6 @@ namespace slskd services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3 From 40bd18b1421fb3fe6c63fc761d87fe395bb02089 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 16:45:46 -0600 Subject: add NetworkClientState enum --- src/slskd/Network/NetworkClientState.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/slskd/Network/NetworkClientState.cs (limited to 'src') diff --git a/src/slskd/Network/NetworkClientState.cs b/src/slskd/Network/NetworkClientState.cs new file mode 100644 index 00000000..ba1c1369 --- /dev/null +++ b/src/slskd/Network/NetworkClientState.cs @@ -0,0 +1,27 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Network +{ + public enum NetworkClientState + { + Disconnected, + Connected, + Connecting, + Reconnecting, + } +} -- cgit v1.2.3 From 9cd9bf8ef6d924ee95192f5c6dc91dae981e1eef Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 16:57:43 -0600 Subject: implement IDisposable, track state --- src/slskd/Network/NetworkClient.cs | 58 ++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index bf79a790..6e121e9b 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -32,8 +32,13 @@ namespace slskd.Network /// /// Network client (agent). /// - public interface INetworkClient + public interface INetworkClient : IDisposable { + /// + /// Gets the client state. + /// + IStateMonitor StateMonitor { get; } + /// /// Starts the client and connects to the controller. /// @@ -52,7 +57,7 @@ namespace slskd.Network /// Synchronizes state with the controller. /// /// The operation context. - Task SynchronizeAsync(); + Task SynchronizeAsync(CancellationToken cancellationToken = default); } /// @@ -75,12 +80,20 @@ namespace slskd.Network HttpClientFactory = httpClientFactory; + StateMonitor = State; + OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); Configure(OptionsMonitor.CurrentValue); } + /// + /// Gets the client state. + /// + public IStateMonitor StateMonitor { get; } + + private ManagedState State { get; } = new(); private IShareService Shares { get; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); private string LastOptionsHash { get; set; } @@ -92,6 +105,7 @@ namespace slskd.Network private ILogger Log { get; } = Serilog.Log.ForContext(); private bool LoggedIn { get; set; } private TaskCompletionSource LoggedInTaskCompletionSource { get; set; } + private bool Disposed { get; set; } /// /// Starts the client and connects to the controller. @@ -111,6 +125,8 @@ namespace slskd.Network Log.Information("Attempting to connect to the network controller {Address}", OptionsMonitor.CurrentValue.Network.Controller.Address); + State.SetValue(_ => TranslateState(HubConnectionState.Connecting)); + // retry indefinitely await Retry.Do( task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token), @@ -120,6 +136,7 @@ namespace slskd.Network maxDelayInMilliseconds: 30000, StartCancellationTokenSource.Token); + State.SetValue(_ => TranslateState(HubConnection.State)); Log.Information("Network controller connection established. Awaiting authentication..."); await LoggedInTaskCompletionSource.Task; @@ -166,12 +183,21 @@ namespace slskd.Network /// Synchronizes state with the controller. /// /// The operation context. - public Task SynchronizeAsync() + public Task SynchronizeAsync(CancellationToken cancellationToken = default) { return UploadSharesAsync(); } - private async Task UploadSharesAsync() + private NetworkClientState TranslateState(HubConnectionState hub) => hub switch + { + HubConnectionState.Disconnected => NetworkClientState.Disconnected, + HubConnectionState.Connected => NetworkClientState.Connected, + HubConnectionState.Connecting => NetworkClientState.Connecting, + HubConnectionState.Reconnecting => NetworkClientState.Reconnecting, + _ => throw new ArgumentException($"Unexpected HubConnectionState {hub}"), + }; + + private async Task UploadSharesAsync(CancellationToken cancellationToken = default) { if (!LoggedIn) { @@ -207,7 +233,7 @@ namespace slskd.Network request.Content = content; Log.Information("Beginning upload of shares"); - var response = await CreateHttpClient().SendAsync(request); + var response = await CreateHttpClient().SendAsync(request, cancellationToken); if (!response.IsSuccessStatusCode) { @@ -338,6 +364,7 @@ namespace slskd.Network { Log.Warning("Network controller connection closed: {Message}", arg.Message); LoggedIn = false; + State.SetValue(_ => TranslateState(HubConnection.State)); return Task.CompletedTask; } @@ -345,6 +372,7 @@ namespace slskd.Network { Log.Warning("Network controller connection reconnecting: {Message}", arg.Message); LoggedIn = false; + State.SetValue(_ => TranslateState(HubConnection.State)); return Task.CompletedTask; } @@ -353,6 +381,7 @@ namespace slskd.Network Log.Warning("Network controller connection reconnected"); // todo: does this need to log in again? does it retain the same connection id? LoggedIn = true; + State.SetValue(_ => TranslateState(HubConnection.State)); await UploadSharesAsync(); } @@ -461,5 +490,24 @@ namespace slskd.Network return TimeSpan.FromSeconds(Intervals[Math.Min(retryContext.PreviousRetryCount, Intervals.Length - 1)]); } } + + protected virtual void Dispose(bool disposing) + { + if (!Disposed) + { + if (disposing) + { + _ = HubConnection?.DisposeAsync(); + } + + Disposed = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } } -- cgit v1.2.3 From 53fed00f3deb8f0f712f770fec08947c85be2bf5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 17:16:06 -0600 Subject: add Network --- src/slskd/Core/State.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index 8077f685..c2039522 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -34,6 +34,7 @@ namespace slskd public bool PendingReconnect { get; init; } public bool PendingRestart { get; init; } public ServerState Server { get; init; } = new ServerState(); + public NetworkState Network { get; init; } = new NetworkState(); public UserState User { get; init; } = new UserState(); public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); public ShareState Shares { get; init; } = new ShareState(); -- cgit v1.2.3 From 802483716d99aec0e6dc180879022ff00fb0d7e3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 17:16:23 -0600 Subject: add DummyNetworkClient --- src/slskd/Network/DummyNetworkClient.cs | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/slskd/Network/DummyNetworkClient.cs (limited to 'src') diff --git a/src/slskd/Network/DummyNetworkClient.cs b/src/slskd/Network/DummyNetworkClient.cs new file mode 100644 index 00000000..f592f492 --- /dev/null +++ b/src/slskd/Network/DummyNetworkClient.cs @@ -0,0 +1,35 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Network +{ + using System.Threading; + using System.Threading.Tasks; + + public class DummyNetworkClient : INetworkClient + { + public IStateMonitor StateMonitor => new ManagedState(); + + public void Dispose() { } + + public Task StartAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; + + public Task StopAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; + + public Task SynchronizeAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; + } +} -- cgit v1.2.3 From dd6c371ba4a5011d853a355ea6081a9bde92307b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Nov 2022 17:16:36 -0600 Subject: handle Network state changes --- src/slskd/Application.cs | 2 + src/slskd/Network/NetworkService.cs | 102 ++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 5e4faad4..33ed2f29 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -134,7 +134,9 @@ namespace slskd RoomService = roomService; Users = userService; ApplicationHub = applicationHub; + Network = networkService; + Network.StateMonitor.OnChange(networkState => State.SetValue(state => state with { Network = networkState.Current })); LogHub = logHub; Program.LogEmitted += (_, log) => LogHub.EmitLogAsync(log); diff --git a/src/slskd/Network/NetworkService.cs b/src/slskd/Network/NetworkService.cs index 56777f61..a0bbe85f 100644 --- a/src/slskd/Network/NetworkService.cs +++ b/src/slskd/Network/NetworkService.cs @@ -25,6 +25,7 @@ namespace slskd.Network using System.Collections.ObjectModel; using System.IO; using System.Linq; + using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; @@ -38,11 +39,21 @@ namespace slskd.Network /// public interface INetworkService { + /// + /// Gets the network client (agent). + /// + INetworkClient Client { get; } + /// /// Gets the collection of registered Agents. /// ReadOnlyCollection RegisteredAgents { get; } + /// + /// Gets the state monitor for the service. + /// + IStateMonitor StateMonitor { get; } + /// /// Generates a random authentication challenge token for the specified . /// @@ -243,21 +254,41 @@ namespace slskd.Network IShareService shareService, IShareRepositoryFactory shareRepositoryFactory, IOptionsMonitor optionsMonitor, - IHubContext networkHub) + IHubContext networkHub, + IHttpClientFactory httpClientFactory, + INetworkClient networkClient = null) { Shares = shareService; ShareRepositoryFactory = shareRepositoryFactory; Waiter = waiter; NetworkHub = networkHub; + // wire up a dummy client so callers don't need to handle nulls + Client = networkClient ?? new DummyNetworkClient(); + + StateMonitor = State; + OptionsMonitor = optionsMonitor; + OptionsMonitor.OnChange(options => Configure(options)); + Configure(OptionsMonitor.CurrentValue); } + /// + /// Gets the network client (agent). + /// + public INetworkClient Client { get; private set; } + /// /// Gets the collection of registered Agents. /// public ReadOnlyCollection RegisteredAgents => RegisteredAgentDictionary.Values.Select(v => v.Agent).ToList().AsReadOnly(); + /// + /// Gets the state monitor for the service. + /// + public IStateMonitor StateMonitor { get; } + + private IHttpClientFactory HttpClientFactory { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); private MemoryCache MemoryCache { get; } = new MemoryCache(new MemoryCacheOptions()); private IHubContext NetworkHub { get; set; } @@ -267,7 +298,11 @@ namespace slskd.Network private IShareRepositoryFactory ShareRepositoryFactory { get; } private IShareService Shares { get; } private IWaiter Waiter { get; } + private IManagedState State { get; } = new ManagedState(); private ConcurrentDictionary WaitIdDictionary { get; } = new(); + private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + private string LastOptionsHash { get; set; } + private string LastControllerOptionsHash { get; set; } /// /// Generates a random authentication challenge token for the specified . @@ -553,7 +588,10 @@ namespace slskd.Network /// The ID of the connection. /// The agent. public void RegisterAgent(string connectionId, Agent agent) - => RegisteredAgentDictionary.AddOrUpdate(agent.Name, addValue: (connectionId, agent), updateValueFactory: (k, v) => (connectionId, agent)); + { + RegisteredAgentDictionary.AddOrUpdate(agent.Name, addValue: (connectionId, agent), updateValueFactory: (k, v) => (connectionId, agent)); + State.SetValue(state => state with { Agents = RegisteredAgents }); + } /// /// Safely attempts to close a stream obtained with . @@ -582,14 +620,17 @@ namespace slskd.Network /// A value indicating whether a registration was removed. public bool TryDeregisterAgent(string connectionId, out (string ConnectionId, Agent Agent) record) { + record = default; + var removed = false; + if (TryGetAgentRegistration(connectionId, out var found)) { Shares.TryRemoveHost(found.Agent.Name); - return RegisteredAgentDictionary.TryRemove(found.Agent.Name, out record); + removed = RegisteredAgentDictionary.TryRemove(found.Agent.Name, out record); } - record = default; - return false; + State.SetValue(state => state with { Agents = RegisteredAgents }); + return removed; } /// @@ -659,6 +700,57 @@ namespace slskd.Network return TryValidateCredential(token.ToString(), agentName, credential, GetShareTokenCacheKey(token)); } + private void Configure(Options options) + { + SyncRoot.Wait(); + + try + { + var optionsHash = Compute.Sha1Hash(options.Network.ToJson()); + var controllerOptionsHash = Compute.Sha1Hash(options.Network.Controller.ToJson()); + + if (optionsHash == LastOptionsHash || controllerOptionsHash == LastControllerOptionsHash) + { + return; + } + + var mode = options.Network.Mode.ToEnum(); + + if (mode == OperationMode.Controller) + { + State.SetValue(state => state with + { + Mode = mode, + Agents = RegisteredAgents, + }); + } + else + { + // the controller changed. disconnect and throw away the client and create a new one + Client = new NetworkClient(Shares, OptionsMonitor, HttpClientFactory); + Client.StateMonitor.OnChange(clientState + => State.SetValue(state => state with { Controller = state.Controller with { State = clientState.Current } })); + + State.SetValue(state => state with + { + Mode = mode, + Controller = new NetworkControllerState() + { + Address = options.Network.Controller.Address, + State = Client.StateMonitor.CurrentValue, + }, + }); + } + + LastOptionsHash = optionsHash; + LastControllerOptionsHash = controllerOptionsHash; + } + finally + { + SyncRoot.Release(); + } + } + private string GetAuthTokenCacheKey(string connectionId) => $"{connectionId}.auth"; private string GetFileTokenCacheKey(string filename, Guid token) => $"{filename}.{token}.file"; -- cgit v1.2.3 From e39c7c192b104c21b6b732ae7c996b257cc7a1f9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Nov 2022 08:26:51 -0600 Subject: remove dual network mode flag --- config/slskd.example.yml | 3 +-- src/slskd/Application.cs | 5 +++-- src/slskd/Core/Options.cs | 9 --------- 3 files changed, 4 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 10c59288..13352fd4 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -10,9 +10,8 @@ # force_share_scan: false # no_version_check: false # log_sql: false -# dual_network_mode: false # network: -# mode: controller +# mode: controller # controller (default), agent, or debug (for local development) # # controller config is required when running in 'agent' mode # # this specifies the network controller that will be controlling this agent # controller: diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 33ed2f29..86427e12 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -398,15 +398,16 @@ namespace slskd { Log.Warning($"Not connecting to the Soulseek server; username and/or password invalid. Specify valid credentials and manually connect, or update config and restart."); } - else if (OptionsAtStartup.Network.Mode.ToEnum() == OperationMode.Agent && !OptionsAtStartup.Flags.DualNetworkMode) + else if (OptionsAtStartup.Network.Mode.ToEnum() == OperationMode.Agent) { Log.Information("Running in Agent mode; not connecting to the Soulseek server."); await Network.Client.StartAsync(cancellationToken); } else { - if (OptionsAtStartup.Flags.DualNetworkMode) + if (OptionsAtStartup.Network.Mode.ToEnum() == OperationMode.Debug) { + Log.Warning("Running in Debug network mode; connecting to controller"); _ = Network.Client.StartAsync(cancellationToken); } diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index a1591923..8d529f03 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -372,15 +372,6 @@ namespace slskd [Description("log SQL queries generated by Entity Framework")] [RequiresRestart] public bool LogSQL { get; init; } = false; - - /// - /// Gets a value indicating whether to run in both controller and agent network modes. - /// - [Argument(default, "dual-network-mode")] - [EnvironmentVariable("DUAL_NETWORK_MODE")] - [Description("run in both controller and agent network modes")] - [RequiresRestart] - public bool DualNetworkMode { get; init; } = false; } /// -- cgit v1.2.3 From 1a5d1cfcbb79c603bd9a79a5a6e05fc5b536ef4d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Nov 2022 08:27:11 -0600 Subject: defaults for network state --- src/slskd/Core/State.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index c2039522..df670893 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -19,6 +19,7 @@ namespace slskd { using System; using System.Collections.Generic; + using System.Linq; using System.Net; using System.Text.Json.Serialization; using slskd.Network; @@ -67,14 +68,14 @@ namespace slskd public record NetworkState { public OperationMode Mode { get; init; } - public NetworkControllerState Controller { get; init; } - public IReadOnlyCollection Agents { get; init; } + public NetworkControllerState Controller { get; init; } = new NetworkControllerState(); + public IReadOnlyCollection Agents { get; init; } = Enumerable.Empty().ToList().AsReadOnly(); } public record NetworkControllerState { public string Address { get; init; } - public NetworkClientState State { get; init; } + public NetworkClientState State { get; init; } = NetworkClientState.Disconnected; } public record UserState -- cgit v1.2.3 From e703a8586c6ca6261e2797ad0554fcadf16f3d7a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Nov 2022 08:52:23 -0600 Subject: fix bug --- src/slskd/Network/NetworkClient.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index 6e121e9b..207494ff 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -114,7 +114,9 @@ namespace slskd.Network /// public async Task StartAsync(CancellationToken cancellationToken = default) { - if (OptionsMonitor.CurrentValue.Network.Mode.ToEnum() != OperationMode.Agent && !OptionsMonitor.CurrentValue.Flags.DualNetworkMode) + var mode = OptionsMonitor.CurrentValue.Network.Mode.ToEnum(); + + if (mode != OperationMode.Agent && mode != OperationMode.Debug) { throw new InvalidOperationException($"Network client can only be started when operation mode is {OperationMode.Agent}"); } @@ -131,7 +133,11 @@ namespace slskd.Network await Retry.Do( task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token), isRetryable: (attempts, ex) => true, - onFailure: (attempts, ex) => Log.Warning("Failed attempt #{Attempts} to connect to network controller: {Message}", attempts, ex.Message), + onFailure: (attempts, ex) => + { + Log.Debug(ex, "Network hub connection failure"); + Log.Warning("Failed attempt #{Attempts} to connect to network controller: {Message}", attempts, ex.Message); + }, maxAttempts: int.MaxValue, maxDelayInMilliseconds: 30000, StartCancellationTokenSource.Token); @@ -410,7 +416,9 @@ namespace slskd.Network private void Configure(Options options) { - if (options.Network.Mode.ToEnum() != OperationMode.Agent && !options.Flags.DualNetworkMode) + var mode = options.Network.Mode.ToEnum(); + + if (mode != OperationMode.Agent && mode != OperationMode.Debug) { return; } -- cgit v1.2.3 From 00e7787d08a134d2b78658d164abbf89fe3ee1df Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Nov 2022 08:52:50 -0600 Subject: remove ConnectedAt --- src/slskd/Network/Types/Agent.cs | 3 --- 1 file changed, 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Network/Types/Agent.cs b/src/slskd/Network/Types/Agent.cs index 999243c6..8c392210 100644 --- a/src/slskd/Network/Types/Agent.cs +++ b/src/slskd/Network/Types/Agent.cs @@ -17,12 +17,9 @@ namespace slskd.Network { - using System; - public record Agent { public string Name { get; init; } public string IPAddress { get; init; } - public DateTime ConnectedAt { get; init; } = DateTime.UtcNow; } } -- cgit v1.2.3 From 74dbb69973035a93e2eda532bd6afe7e2f89d741 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Nov 2022 08:53:23 -0600 Subject: remove connectedAt --- src/slskd/Network/API/Hubs/NetworkHub.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Network/API/Hubs/NetworkHub.cs b/src/slskd/Network/API/Hubs/NetworkHub.cs index 6e46a78b..9a54dde5 100644 --- a/src/slskd/Network/API/Hubs/NetworkHub.cs +++ b/src/slskd/Network/API/Hubs/NetworkHub.cs @@ -116,7 +116,7 @@ namespace slskd.Network } var remoteIp = Context.Features.Get().RemoteIpAddress.ToString(); - var record = new Agent { Name = agent, ConnectedAt = DateTime.UtcNow, IPAddress = remoteIp }; + var record = new Agent { Name = agent, IPAddress = remoteIp }; Log.Information("Agent connection {Id} ({IP}) authenticated as agent {Agent}", Context.ConnectionId, remoteIp, agent); Network.RegisterAgent(Context.ConnectionId, record); -- cgit v1.2.3