Apply suggestions from code review

pull/2888/head
Matt Montgomery 5 years ago
parent a40fe86776
commit 0945659cb5

@ -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,12 +274,7 @@ 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;
}
}
foreach (var code in delete)
{
_logger.LogDebug("Removing expired request {code}", code); _logger.LogDebug("Removing expired request {code}", code);
if (!_currentRequests.TryRemove(code, out _)) if (!_currentRequests.TryRemove(code, out _))
@ -287,6 +283,7 @@ namespace Emby.Server.Implementations.QuickConnect
} }
} }
} }
}
private void ReloadConfiguration() private void ReloadConfiguration()
{ {

Loading…
Cancel
Save