Apply suggestions from code review

This commit is contained in:
Matt Montgomery 2020-07-26 18:14:35 -05:00
parent a40fe86776
commit 0945659cb5
2 changed files with 12 additions and 15 deletions

View File

@ -149,15 +149,16 @@ namespace Emby.Server.Implementations.QuickConnect
/// <inheritdoc/> /// <inheritdoc/>
public string GenerateCode() public string GenerateCode()
{ {
Span<byte> raw = stackalloc byte[4];
int min = (int)Math.Pow(10, CodeLength - 1); int min = (int)Math.Pow(10, CodeLength - 1);
int max = (int)Math.Pow(10, CodeLength); int max = (int)Math.Pow(10, CodeLength);
uint scale = uint.MaxValue; uint scale = uint.MaxValue;
while (scale == uint.MaxValue) while (scale == uint.MaxValue)
{ {
byte[] raw = new byte[4];
_rng.GetBytes(raw); _rng.GetBytes(raw);
scale = BitConverter.ToUInt32(raw, 0); scale = BitConverter.ToUInt32(raw);
} }
int code = (int)(min + ((max - min) * (scale / (double)uint.MaxValue))); int code = (int)(min + ((max - min) * (scale / (double)uint.MaxValue)));
@ -247,7 +248,7 @@ namespace Emby.Server.Implementations.QuickConnect
private string GenerateSecureRandom(int length = 32) private string GenerateSecureRandom(int length = 32)
{ {
var bytes = new byte[length]; Span<byte> bytes = stackalloc byte[length];
_rng.GetBytes(bytes); _rng.GetBytes(bytes);
return Hex.Encode(bytes); return Hex.Encode(bytes);
@ -265,7 +266,7 @@ namespace Emby.Server.Implementations.QuickConnect
} }
// Expire stale connection requests // Expire stale connection requests
var delete = new List<string>(); var code = string.Empty;
var values = _currentRequests.Values.ToList(); var values = _currentRequests.Values.ToList();
for (int i = 0; i < values.Count; i++) for (int i = 0; i < values.Count; i++)
@ -273,17 +274,13 @@ namespace Emby.Server.Implementations.QuickConnect
var added = values[i].DateAdded ?? DateTime.UnixEpoch; var added = values[i].DateAdded ?? DateTime.UnixEpoch;
if (DateTime.Now > added.AddMinutes(Timeout) || expireAll) if (DateTime.Now > added.AddMinutes(Timeout) || expireAll)
{ {
delete.Add(values[i].Code); code = values[i].Code;
} _logger.LogDebug("Removing expired request {code}", code);
}
foreach (var code in delete) if (!_currentRequests.TryRemove(code, out _))
{ {
_logger.LogDebug("Removing expired request {code}", code); _logger.LogWarning("Request {code} already expired", code);
}
if (!_currentRequests.TryRemove(code, out _))
{
_logger.LogWarning("Request {code} already expired", code);
} }
} }
} }

View File

@ -110,7 +110,7 @@ namespace MediaBrowser.Api.QuickConnect
public object Post(Activate request) public object Post(Activate request)
{ {
if(_quickConnect.State == QuickConnectState.Unavailable) if (_quickConnect.State == QuickConnectState.Unavailable)
{ {
return false; return false;
} }