don't error when stopping

This commit is contained in:
Pilzinsel64
2025-01-28 08:47:55 +01:00
parent b92eb24438
commit 49b5ebddb1

View File

@@ -17,6 +17,7 @@ public class ApiServer(string apiUrl) : IApiServer
protected int restartAttempts = 0; protected int restartAttempts = 0;
protected DateTime lastRestartAttempt; protected DateTime lastRestartAttempt;
protected SemaphoreSlim? semaphore; protected SemaphoreSlim? semaphore;
protected bool doListen;
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication; public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
public event OnCheckContextEventHandler? OnCheckContext; public event OnCheckContextEventHandler? OnCheckContext;
@@ -52,6 +53,7 @@ public class ApiServer(string apiUrl) : IApiServer
{ {
Log.Info("Starting listener"); Log.Info("Starting listener");
httpListener.Prefixes.Add(ApiUrl + "/"); httpListener.Prefixes.Add(ApiUrl + "/");
doListen = true;
httpListener.Start(); httpListener.Start();
Log.Info("Started listener"); Log.Info("Started listener");
Receive(); Receive();
@@ -60,6 +62,7 @@ public class ApiServer(string apiUrl) : IApiServer
public virtual void Stop() public virtual void Stop()
{ {
Log.Info("Stopping listener"); Log.Info("Stopping listener");
doListen = false;
httpListener.Stop(); httpListener.Stop();
Thread.Sleep(StopDelay); Thread.Sleep(StopDelay);
httpListener.Close(); httpListener.Close();
@@ -181,7 +184,7 @@ public class ApiServer(string apiUrl) : IApiServer
HttpListenerContext? context; HttpListenerContext? context;
// Skip if not lisstening anymore // Skip if not lisstening anymore
if (!httpListener.IsListening) if (!httpListener.IsListening || !doListen)
return; return;
// Wait for a free slot // Wait for a free slot