diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-01-31 10:56:08 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-01-31 10:56:08 -0600 |
| commit | 3ccc75495e9c9217b6055042a83f47250a6edb24 (patch) | |
| tree | 85e5889b02920fbca744a8cb9ec0aa2655d55ae5 /src | |
| parent | 5d4c41af560d1ab7ee452c8630e3ecbb5c940ce5 (diff) | |
| download | slskd-3ccc75495e9c9217b6055042a83f47250a6edb24.tar.gz | |
add retries, re-enable connect
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Startup.cs | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 3ed22f1b..b0305744 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -35,6 +35,9 @@ public class Startup { + private readonly int MaxReconnectAttempts = 3; + private int CurrentReconnectAttempts = 0; + internal static string OutputDirectory { get; set; } internal static string SharedDirectory { get; set; } internal static long SharedCacheTTL { get; set; } @@ -407,21 +410,34 @@ Client.Disconnected += async (e, args) => { - logger.Warning("Disconnected from Soulseek server: {Message}", args.Message, args.Exception); + Console.WriteLine($"Disconnected from Soulseek server: {args.Message}"); + + // don't reconnect if the disconnecting Exception is either of these types. + // if KickedFromServerException, another client was most likely signed in, and retrying will cause a connect loop. + // if ObjectDisposedException, the client is shutting down. + if (!(args.Exception is KickedFromServerException || args.Exception is ObjectDisposedException)) + { + Interlocked.Increment(ref CurrentReconnectAttempts); + + if (CurrentReconnectAttempts <= MaxReconnectAttempts) + { + var wait = CurrentReconnectAttempts ^ 3; + Console.WriteLine($"Waiting {wait} second(s) before reconnect..."); + await Task.Delay(wait); - //// don't reconnect if the disconnecting Exception is either of these types. - //// if KickedFromServerException, another client was most likely signed in, and retrying will cause a connect loop. - //// if ObjectDisposedException, the client is shutting down. - //if (!(args.Exception is KickedFromServerException || args.Exception is ObjectDisposedException)) - //{ - // logger.Warning("Attepting to reconnect..."); - // await Client.ConnectAsync(username, password); - //} + Console.WriteLine($"Attepting to reconnect..."); + await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); + } + else + { + Console.WriteLine($"Unable to reconnect after {CurrentReconnectAttempts} tries."); + } + } }; Task.Run(async () => { - // await Client.ConnectAsync("vps2.slsknet.org", 2242, "praetor-2", "Jyi98uas"); + await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); }).GetAwaiter().GetResult(); logger.Information("Connected and logged in as {Username}", username); |
