2020namespace Shoko . Server . Providers . AniDB . UDP ;
2121
2222#nullable enable
23- public class AniDBUDPConnectionHandler : ConnectionHandler , IUDPConnectionHandler
23+ public partial class AniDBUDPConnectionHandler : ConnectionHandler , IUDPConnectionHandler
2424{
2525 /****
2626 * From Anidb wiki:
@@ -35,7 +35,11 @@ public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandle
3535 private readonly IConnectivityService _connectivityService ;
3636 private AniDBSocketHandler ? _socketHandler ;
3737 private readonly object _socketHandlerLock = new ( ) ;
38- private static readonly Regex s_logMask = new ( "(?<=(\\ bpass=|&pass=\\ bs=|&s=))[^&]+" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
38+ // IDK Rider said to use a GeneratedRegex attribute
39+ private static readonly Regex s_logMask = GetLogRegex ( ) ;
40+
41+ [ GeneratedRegex ( "(?<=(\\ bpass=|&pass=\\ bs=|&s=))[^&]+" , RegexOptions . IgnoreCase | RegexOptions . Compiled , "en-US" ) ]
42+ private static partial Regex GetLogRegex ( ) ;
3943
4044 public event EventHandler ? LoginFailed ;
4145
@@ -150,7 +154,7 @@ private void InitInternal()
150154 }
151155
152156 _socketHandler = new AniDBSocketHandler ( _loggerFactory , settings . AniDb . UDPServerAddress , settings . AniDb . UDPServerPort , settings . AniDb . ClientPort ) ;
153- IsNetworkAvailable = _socketHandler . TryConnection ( ) . Result ;
157+ IsNetworkAvailable = _socketHandler . TryConnection ( ) ;
154158 }
155159
156160 _isLoggedOn = false ;
@@ -214,32 +218,32 @@ private void LogoutTimerElapsed(object? sender, ElapsedEventArgs e)
214218 /// <returns></returns>
215219 public string Send ( string command , bool needsUnicode = true )
216220 {
217- lock ( _socketHandlerLock )
218- {
219- // Steps:
220- // 1. Check Ban state and throw if Banned
221- // 2. Check Login State and Login if needed
222- // 3. Actually Call AniDB
221+ // Steps:
222+ // 1. Check Ban state and throw if Banned
223+ // 2. Check Login State and Login if needed
224+ // 3. Actually Call AniDB
223225
224- // Check Ban State
225- // Ideally, this will never happen, as we stop the queue and attempt a graceful rollback of the command
226- if ( IsBanned )
226+ // Check Ban State
227+ // Ideally, this will never happen, as we stop the queue and attempt a graceful rollback of the command
228+ if ( IsBanned )
229+ {
230+ throw new AniDBBannedException
227231 {
228- throw new AniDBBannedException
229- {
230- BanType = UpdateType . UDPBan , BanExpires = BanTime ? . AddHours ( BanTimerResetLength )
231- } ;
232- }
233- // 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
234- // It wasn't handled before, and it's not caused serious problems
232+ BanType = UpdateType . UDPBan , BanExpires = BanTime ? . AddHours ( BanTimerResetLength )
233+ } ;
234+ }
235+ // 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
236+ // It wasn't handled before, and it's not caused serious problems
235237
236- // login doesn't use this method, so this check won't interfere with it
237- // if we got here, and it's invalid session, then it already failed to re-log
238- if ( IsInvalidSession )
239- {
240- throw new NotLoggedInException ( ) ;
241- }
238+ // login doesn't use this method, so this check won't interfere with it
239+ // if we got here, and it's invalid session, then it already failed to re-log
240+ if ( IsInvalidSession )
241+ {
242+ throw new NotLoggedInException ( ) ;
243+ }
242244
245+ lock ( _socketHandlerLock )
246+ {
243247 // Check Login State
244248 if ( ! Login ( ) )
245249 {
@@ -296,7 +300,7 @@ private string SendInternal(string command, bool needsUnicode = true, bool isPin
296300
297301 var start = DateTime . Now ;
298302 Logger . LogTrace ( "AniDB UDP Call: (Using {Unicode}) {Command}" , needsUnicode ? "Unicode" : "ASCII" , MaskLog ( command ) ) ;
299- var byReceivedAdd = _socketHandler . Send ( sendByteAdd ) . Result ;
303+ var byReceivedAdd = _socketHandler . Send ( sendByteAdd ) ;
300304
301305 if ( byReceivedAdd . All ( a => a == 0 ) )
302306 {
@@ -344,31 +348,35 @@ private void StopPinging()
344348
345349 public void ForceReconnection ( )
346350 {
347- try
348- {
349- ForceLogout ( ) ;
350- }
351- catch ( Exception ex )
351+ lock ( _socketHandlerLock )
352352 {
353- Logger . LogError ( ex , "Failed to logout" ) ;
354- }
355353
356- try
357- {
358- CloseConnections ( ) ;
359- }
360- catch ( Exception ex )
361- {
362- Logger . LogError ( ex , "Failed to close socket " ) ;
363- }
354+ try
355+ {
356+ ForceLogout ( ) ;
357+ }
358+ catch ( Exception ex )
359+ {
360+ Logger . LogError ( ex , "Failed to logout " ) ;
361+ }
364362
365- try
366- {
367- InitInternal ( ) ;
368- }
369- catch ( Exception ex )
370- {
371- Logger . LogError ( ex , "Failed to reinitialize socket" ) ;
363+ try
364+ {
365+ CloseConnections ( ) ;
366+ }
367+ catch ( Exception ex )
368+ {
369+ Logger . LogError ( ex , "Failed to close socket" ) ;
370+ }
371+
372+ try
373+ {
374+ InitInternal ( ) ;
375+ }
376+ catch ( Exception ex )
377+ {
378+ Logger . LogError ( ex , "Failed to reinitialize socket" ) ;
379+ }
372380 }
373381 }
374382
@@ -387,7 +395,7 @@ public void ForceLogout()
387395 Logger . LogTrace ( "Logging Out" ) ;
388396 try
389397 {
390- _requestFactory . Create < RequestLogout > ( ) . Send ( ) ;
398+ lock ( _socketHandlerLock ) _requestFactory . Create < RequestLogout > ( ) . Send ( ) ;
391399 }
392400 catch
393401 {
@@ -437,8 +445,11 @@ public bool Login()
437445 {
438446 if ( IsBanned ) return false ;
439447 Logger . LogTrace ( "Failed to login to AniDB. Issuing a Logout command and retrying" ) ;
440- ForceLogout ( ) ;
441- return Login ( settings . AniDb . Username , settings . AniDb . Password ) ;
448+ lock ( _socketHandlerLock )
449+ {
450+ ForceLogout ( ) ;
451+ return Login ( settings . AniDb . Username , settings . AniDb . Password ) ;
452+ }
442453 }
443454 catch ( Exception e )
444455 {
@@ -551,13 +562,16 @@ public bool TestLogin(string username, string password)
551562 return false ;
552563 }
553564
554- var result = Login ( username , password ) ;
555- if ( result )
565+ lock ( _socketHandlerLock )
556566 {
557- ForceLogout ( ) ;
558- }
567+ var result = Login ( username , password ) ;
568+ if ( result )
569+ {
570+ ForceLogout ( ) ;
571+ }
559572
560- return result ;
573+ return result ;
574+ }
561575 }
562576
563577 public bool SetCredentials ( string username , string password )
0 commit comments