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
This commit is contained in:
Jesse
2025-05-29 13:56:37 +02:00
committed by GitHub
parent 7ec684aa7e
commit 636e250085
2 changed files with 17 additions and 13 deletions
@@ -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
/// </summary>
/// <returns>bool isAlreadyRunning</returns>
public async Task<bool> 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)
+1 -1
View File
@@ -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"));