aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Network/NetworkClient.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-11-06 16:57:43 -0600
committerJP Dillingham <jpdillingham@gmail.com>2022-11-06 16:57:43 -0600
commit9cd9bf8ef6d924ee95192f5c6dc91dae981e1eef (patch)
tree1c3d0f05b6531ea94616873bf55b15ee1d5f3f54 /src/slskd/Network/NetworkClient.cs
parent40bd18b1421fb3fe6c63fc761d87fe395bb02089 (diff)
downloadslskd-9cd9bf8ef6d924ee95192f5c6dc91dae981e1eef.tar.gz
implement IDisposable, track state
Diffstat (limited to 'src/slskd/Network/NetworkClient.cs')
-rw-r--r--src/slskd/Network/NetworkClient.cs58
1 files changed, 53 insertions, 5 deletions
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,9 +32,14 @@ namespace slskd.Network
/// <summary>
/// Network client (agent).
/// </summary>
- public interface INetworkClient
+ public interface INetworkClient : IDisposable
{
/// <summary>
+ /// Gets the client state.
+ /// </summary>
+ IStateMonitor<NetworkClientState> StateMonitor { get; }
+
+ /// <summary>
/// Starts the client and connects to the controller.
/// </summary>
/// <param name="cancellationToken"></param>
@@ -52,7 +57,7 @@ namespace slskd.Network
/// Synchronizes state with the controller.
/// </summary>
/// <returns>The operation context.</returns>
- Task SynchronizeAsync();
+ Task SynchronizeAsync(CancellationToken cancellationToken = default);
}
/// <summary>
@@ -75,12 +80,20 @@ namespace slskd.Network
HttpClientFactory = httpClientFactory;
+ StateMonitor = State;
+
OptionsMonitor = optionsMonitor;
OptionsMonitor.OnChange(options => Configure(options));
Configure(OptionsMonitor.CurrentValue);
}
+ /// <summary>
+ /// Gets the client state.
+ /// </summary>
+ public IStateMonitor<NetworkClientState> StateMonitor { get; }
+
+ private ManagedState<NetworkClientState> 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<NetworkClient>();
private bool LoggedIn { get; set; }
private TaskCompletionSource LoggedInTaskCompletionSource { get; set; }
+ private bool Disposed { get; set; }
/// <summary>
/// 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.
/// </summary>
/// <returns>The operation context.</returns>
- 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);
+ }
}
}