From 636e250085619200bac2a40f43023335ec6b546a Mon Sep 17 00:00:00 2001 From: Jesse Date: Thu, 29 May 2025 13:56:37 +0200 Subject: [PATCH] Handle AlreadyRunning with TcpListener to stop tons of exceptions (#302) * Handle AlreadyRunning with TcpListener to stop tons of exceptions * Use same properties as web server --- .../Helpers/HttpServerHelper.cs | 28 +++++++++++-------- Libraries/SPTarkov.Server.Core/Utils/App.cs | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs index ad844256..ceabd17a 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs @@ -1,4 +1,6 @@ -using SPTarkov.DI.Annotations; +using System.Net; +using System.Net.Sockets; +using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Models.Spt.Config; using SPTarkov.Server.Core.Servers; @@ -58,22 +60,24 @@ public class HttpServerHelper(ConfigServer configServer) /// Method to determine if another version of the server is already running /// /// bool isAlreadyRunning - public async Task IsAlreadyRunning() + public bool IsAlreadyRunning() { + TcpListener? listener = null; + try { - var handler = new HttpClientHandler() - { - ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator - }; - var http = new HttpClient(handler); - var res = await http.PostAsync($"{GetBackendUrl()}/launcher/ping", null); - return res.IsSuccessStatusCode; - } - catch (Exception ) - { + listener = new(IPAddress.Parse(_httpConfig.Ip), _httpConfig.Port); + listener.Start(); return false; } + catch (Exception) + { + return true; + } + finally + { + listener?.Stop(); + } } public void SendTextJson(HttpResponse resp, object output) diff --git a/Libraries/SPTarkov.Server.Core/Utils/App.cs b/Libraries/SPTarkov.Server.Core/Utils/App.cs index f20e56cd..c0686642 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/App.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/App.cs @@ -62,7 +62,7 @@ public class App // execute onLoad callbacks _logger.Info(_localisationService.GetText("executing_startup_callbacks")); - var isAlreadyRunning = await _httpServerHelper.IsAlreadyRunning(); + var isAlreadyRunning = _httpServerHelper.IsAlreadyRunning(); if (isAlreadyRunning) { _logger.Critical(_localisationService.GetText("webserver_already_running"));