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:
@@ -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)
|
||||
|
||||
@@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user