From 0945659cb572be510c7bdd30315f23b0e3c9a8f3 Mon Sep 17 00:00:00 2001
From: Matt Montgomery <33811686+ConfusedPolarBear@users.noreply.github.com>
Date: Sun, 26 Jul 2020 18:14:35 -0500
Subject: [PATCH] Apply suggestions from code review
---
.../QuickConnect/QuickConnectManager.cs | 25 ++++++++-----------
.../QuickConnect/QuickConnectService.cs | 2 +-
2 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs b/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
index 263556e9d7..a69ea2267b 100644
--- a/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
+++ b/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
@@ -149,15 +149,16 @@ namespace Emby.Server.Implementations.QuickConnect
///
public string GenerateCode()
{
+ Span raw = stackalloc byte[4];
+
int min = (int)Math.Pow(10, CodeLength - 1);
int max = (int)Math.Pow(10, CodeLength);
uint scale = uint.MaxValue;
while (scale == uint.MaxValue)
{
- byte[] raw = new byte[4];
_rng.GetBytes(raw);
- scale = BitConverter.ToUInt32(raw, 0);
+ scale = BitConverter.ToUInt32(raw);
}
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)
{
- var bytes = new byte[length];
+ Span bytes = stackalloc byte[length];
_rng.GetBytes(bytes);
return Hex.Encode(bytes);
@@ -265,7 +266,7 @@ namespace Emby.Server.Implementations.QuickConnect
}
// Expire stale connection requests
- var delete = new List();
+ var code = string.Empty;
var values = _currentRequests.Values.ToList();
for (int i = 0; i < values.Count; i++)
@@ -273,17 +274,13 @@ namespace Emby.Server.Implementations.QuickConnect
var added = values[i].DateAdded ?? DateTime.UnixEpoch;
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)
- {
- _logger.LogDebug("Removing expired request {code}", code);
-
- if (!_currentRequests.TryRemove(code, out _))
- {
- _logger.LogWarning("Request {code} already expired", code);
+ if (!_currentRequests.TryRemove(code, out _))
+ {
+ _logger.LogWarning("Request {code} already expired", code);
+ }
}
}
}
diff --git a/MediaBrowser.Api/QuickConnect/QuickConnectService.cs b/MediaBrowser.Api/QuickConnect/QuickConnectService.cs
index 9047a1e957..6298f66e59 100644
--- a/MediaBrowser.Api/QuickConnect/QuickConnectService.cs
+++ b/MediaBrowser.Api/QuickConnect/QuickConnectService.cs
@@ -110,7 +110,7 @@ namespace MediaBrowser.Api.QuickConnect
public object Post(Activate request)
{
- if(_quickConnect.State == QuickConnectState.Unavailable)
+ if (_quickConnect.State == QuickConnectState.Unavailable)
{
return false;
}