diff --git a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs index 822954ab..59911333 100644 --- a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs +++ b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs @@ -7,20 +7,17 @@ namespace SPTarkov.Server.Core.Context; [Injectable(InjectionType.Singleton)] public class ApplicationContext { - protected const short MaxSavedValues = 10; + private const short MaxSavedValues = 10; private static ApplicationContext? _applicationContext; private readonly ISptLogger _logger; - protected readonly ConcurrentDictionary> variables = new(); + private readonly ConcurrentDictionary values)> _variables = new(); /// /// When ApplicationContext gets created by the DI container we store the singleton reference so we can provide it /// statically for harmony patches! /// - public ApplicationContext - ( - ISptLogger logger - ) + public ApplicationContext(ISptLogger logger) { _logger = logger; _applicationContext = this; @@ -33,9 +30,12 @@ public class ApplicationContext public ContextVariable? GetLatestValue(ContextVariableType type) { - if (variables.TryGetValue(type, out var savedValues)) + if (_variables.TryGetValue(type, out var savedValues)) { - return savedValues.Last!.Value; + lock (savedValues.lockObject) + { + return savedValues.values.Last!.Value; + } } return null; @@ -44,9 +44,12 @@ public class ApplicationContext public ICollection GetValues(ContextVariableType type) { var values = new List(); - if (variables.TryGetValue(type, out var savedValues)) + if (_variables.TryGetValue(type, out var savedValues)) { - values.AddRange(savedValues); + lock (savedValues.lockObject) + { + values.AddRange(savedValues.Item2); + } } return values; @@ -54,26 +57,30 @@ public class ApplicationContext public void AddValue(ContextVariableType type, object value) { - if (!variables.TryGetValue(type, out var savedValues)) + if (!_variables.TryGetValue(type, out var savedValues)) { - savedValues = []; - if (!variables.TryAdd(type, savedValues)) + savedValues = new ValueTuple>(); + savedValues.lockObject = new Lock(); + savedValues.values = []; + if (!_variables.TryAdd(type, savedValues)) { _logger.Error($"Unable to add context variable type: {type}"); } } - if (savedValues.Count >= MaxSavedValues) + lock (savedValues.lockObject) { - savedValues.RemoveFirst(); + if (savedValues.values.Count >= MaxSavedValues) + { + savedValues.values.RemoveFirst(); + } + savedValues.values.AddLast(new ContextVariable(value, type)); } - - savedValues.AddLast(new ContextVariable(value, type)); } public void ClearValues(ContextVariableType type) { - if (!variables.Remove(type, out _)) + if (!_variables.Remove(type, out _)) { _logger.Error($"Unable to clear context variable type: {type}"); } diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs index 65b6ca36..1fcfa90e 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs @@ -88,7 +88,7 @@ public class SptHttpListener : IHttpListener while (readTask.Result != 0) { readBytes += readTask.Result; - totalRead.AddRange(memory.ToArray()); + totalRead.AddRange(memory[..readTask.Result].ToArray()); memory = new Memory(new byte[BodyReadBufferSize]); readTask = req.Body.ReadAsync(memory).AsTask(); readTask.Wait(); diff --git a/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs b/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs index 5bf36d92..de07a291 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs @@ -88,7 +88,16 @@ public class HttpServer( context.Request.Cookies.TryGetValue("PHPSESSID", out var sessionId); if (sessionId != null) { - _applicationContext.AddValue(ContextVariableType.SESSION_ID, sessionId); + try + { + _applicationContext.AddValue(ContextVariableType.SESSION_ID, sessionId); + } + catch (Exception ex) + { + _logger.Debug("Error while adding context value: " + ex.Message); + _logger.Critical(ex.StackTrace); + throw; + } } // Extract header for original IP detection @@ -106,6 +115,7 @@ public class HttpServer( } catch (Exception ex) { + _logger.Debug("Error handling request: " + context.Request.Path); _logger.Critical(ex.Message); _logger.Critical(ex.StackTrace); }