Skip to content

Commit

Permalink
Fixed bug where server would not change port, via the numeric control.
Browse files Browse the repository at this point in the history
  • Loading branch information
PapyrusCompendium committed Jul 21, 2019
1 parent c904eef commit a684e68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 9 additions & 2 deletions MiniWebServer/MiniWebServer/WebListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ public WebListener(string prefix)
listener.Prefixes.Add(prefix);
}

public void SetPrefix(string prefix)
{
listener.Prefixes.Clear();
listener.Prefixes.Add(prefix);
}

public void Start()
{
listenerThread = new Thread(() =>
{
listener.Start();
Log.Info($"Http Listener started - Prefix: {listener.Prefixes.First()}");
Log.Info($"Http listener started - Prefix: {listener.Prefixes.First()}.");

while (listener.IsListening)
Listen();
Expand All @@ -43,7 +49,7 @@ public void Start()
listenerThread.Start();

if (WebServerStarted.GetInvocationList().Count() > 0)
WebServerStarted.Invoke();
WebServerStarted.Invoke();
}

public void Listen()
Expand Down Expand Up @@ -76,6 +82,7 @@ public void Stop()
listenerThread.Interrupt();
listenerThread.Abort();
listenerThread = null;
Log.Info("Stopped Http Listener.");
}
catch (ThreadAbortException e)
{
Expand Down
8 changes: 6 additions & 2 deletions MiniWebServer/MiniWebServer/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public class WebServer

private void WebListener_WebServerStarted() => ServerInterface.SetServerStatus(true);

public void Start() => webListener.Start();
public void Start()
{
webListener.SetPrefix($"http://*:{ServerInterface.GetPortNumber()}/");
webListener.Start();
}

public void Stop() => webListener.Stop();

Expand All @@ -28,7 +32,7 @@ public WebServer(WebServerInterface serverInterface)
return;

ServerInterface = serverInterface;
webListener = new WebListener("http://*:" + ServerInterface.GetPortNumber + "/");
webListener = new WebListener($"http://*:{ServerInterface.GetPortNumber()}/");
webListener.HttpCall += WebListener_HttpCall;
webListener.WebServerStarted += WebListener_WebServerStarted;
webListener.WebServerStopped += WebListener_WebServerStopped;
Expand Down
6 changes: 5 additions & 1 deletion MiniWebServer/MiniWebServer/WebServerInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ namespace MiniWebServer
{
public partial class WebServerInterface : Form
{
public int GetPortNumber { get { return (int)webServerPort.Value; } }
private WebServer webServer;

public WebServerInterface()
{
InitializeComponent();
}

public int GetPortNumber()
{
return (int)webServerPort.Value;
}

public void SetServerStatus(bool online)
{
serverStatusLabel.Text = online ? "Online" : "Offline";
Expand Down

0 comments on commit a684e68

Please sign in to comment.