From c6bf0a91a4ddff9ad3d14cd992abe7fa3ec81cc4 Mon Sep 17 00:00:00 2001 From: hidden4003 Date: Fri, 19 Jul 2024 17:34:21 +0300 Subject: [PATCH 1/2] Do not return null if exception happened --- Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs b/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs index 3fe4d8666..cac36dc35 100644 --- a/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs +++ b/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs @@ -20,6 +20,7 @@ namespace Shoko.Server.Providers.AniDB.UDP; +#nullable enable public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandler { // 10 minutes @@ -305,6 +306,7 @@ private async Task SendInternal(string command, bool needsUnicode = true if (result.FinalException != null) { Logger.LogError(result.FinalException, "Failed to send AniDB message"); + throw result.FinalException; } return result.Result; From d1e192584ad18e9163568a18d62fc1ebdb51534d Mon Sep 17 00:00:00 2001 From: hidden4003 Date: Fri, 19 Jul 2024 17:59:29 +0300 Subject: [PATCH 2/2] Fix nullable warnings --- .../AniDB/UDP/AniDBUDPConnectionHandler.cs | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs b/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs index cac36dc35..0ac56a59b 100644 --- a/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs +++ b/Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs @@ -29,16 +29,16 @@ public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandle private const int PingFrequency = 45 * 1000; private readonly IRequestFactory _requestFactory; private readonly IConnectivityService _connectivityService; - private IAniDBSocketHandler _socketHandler; + private IAniDBSocketHandler? _socketHandler; private static readonly Regex s_logMask = new("(?<=(\\bpass=|&pass=\\bs=|&s=))[^&]+", RegexOptions.Compiled | RegexOptions.IgnoreCase); - public event EventHandler LoginFailed; + public event EventHandler? LoginFailed; public override double BanTimerResetLength => 1.5D; public override string Type => "UDP"; protected override UpdateType BanEnum => UpdateType.UDPBan; - public string SessionID { get; private set; } + public string? SessionID { get; private set; } public bool IsAlive { get; private set; } private string _cdnDomain = Constants.URLS.AniDB_Images_Domain; @@ -47,8 +47,8 @@ public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandle private ISettingsProvider SettingsProvider { get; set; } - private Timer _pingTimer; - private Timer _logoutTimer; + private Timer? _pingTimer; + private Timer? _logoutTimer; private bool _isLoggedOn; private bool _isInvalidSession; @@ -62,7 +62,9 @@ public bool IsInvalidSession _isInvalidSession = value; UpdateState(new AniDBStateUpdate { - UpdateType = UpdateType.InvalidSession, UpdateTime = DateTime.Now, Value = value + UpdateType = UpdateType.InvalidSession, + UpdateTime = DateTime.Now, + Value = value }); } } @@ -111,7 +113,7 @@ public async Task Init() await InitInternal(); return true; } - + public async Task Init(string username, string password, string serverName, ushort serverPort, ushort clientPort) { var settings = SettingsProvider.GetSettings(); @@ -151,12 +153,12 @@ private async Task InitInternal() IsAlive = true; } - private void PingTimerElapsed(object sender, ElapsedEventArgs e) + private void PingTimerElapsed(object? sender, ElapsedEventArgs e) { try { if (!_isLoggedOn) return; - if (_socketHandler.IsLocked || !_socketHandler.IsConnected) return; + if (_socketHandler == null || _socketHandler.IsLocked || !_socketHandler.IsConnected) return; if (IsBanned || BackoffSecs.HasValue) return; var ping = _requestFactory.Create(); @@ -164,11 +166,11 @@ private void PingTimerElapsed(object sender, ElapsedEventArgs e) } catch (UnexpectedUDPResponseException) { - _pingTimer.Stop(); + _pingTimer?.Stop(); } catch (AniDBBannedException) { - _pingTimer.Stop(); + _pingTimer?.Stop(); } catch (Exception exception) { @@ -176,12 +178,12 @@ private void PingTimerElapsed(object sender, ElapsedEventArgs e) } } - private void LogoutTimerElapsed(object sender, ElapsedEventArgs e) + private void LogoutTimerElapsed(object? sender, ElapsedEventArgs e) { try { if (!_isLoggedOn) return; - if (_socketHandler.IsLocked || !_socketHandler.IsConnected) return; + if (_socketHandler == null || _socketHandler.IsLocked || !_socketHandler.IsConnected) return; if (IsBanned || BackoffSecs.HasValue) return; ForceLogout(); @@ -211,7 +213,8 @@ public async Task Send(string command, bool needsUnicode = true) { throw new AniDBBannedException { - BanType = UpdateType.UDPBan, BanExpires = BanTime?.AddHours(BanTimerResetLength) + BanType = UpdateType.UDPBan, + BanExpires = BanTime?.AddHours(BanTimerResetLength) }; } // TODO Low Priority: We need to handle Login Attempt Decay, so that we can try again if it's not just a bad user/pass @@ -263,7 +266,7 @@ private async Task SendInternal(string command, bool needsUnicode = true var sendByteAdd = encoding.GetBytes(command); var timeoutPolicy = Policy - .Handle(e => e is { SocketErrorCode: SocketError.TimedOut}) + .Handle(e => e is { SocketErrorCode: SocketError.TimedOut }) .Or() .RetryAsync(async (_, _) => { @@ -289,7 +292,8 @@ private async Task SendInternal(string command, bool needsUnicode = true IsBanned = true; throw new AniDBBannedException { - BanType = UpdateType.UDPBan, BanExpires = BanTime?.AddHours(BanTimerResetLength) + BanType = UpdateType.UDPBan, + BanExpires = BanTime?.AddHours(BanTimerResetLength) }; }