Skip to content

Commit 2832044

Browse files
author
Chris Jakeman
committed
adds check for webserver port in use
Also adds missing save of webserver port value
1 parent 9ace46b commit 2832044

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

Source/Menu/Options.Designer.cs

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Menu/Options.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ void buttonOK_Click(object sender, EventArgs e)
434434
Settings.PressureUnit = comboPressureUnit.SelectedValue.ToString();
435435
Settings.Units = comboOtherUnits.SelectedValue.ToString();
436436
Settings.DisableTCSScripts = checkDisableTCSScripts.Checked;
437+
Settings.WebServerPort = (int)numericWebServerPort.Value;
437438

438439
// Audio tab
439440
Settings.SoundVolumePercent = (int)numericSoundVolumePercent.Value;

Source/RunActivity/Viewer3D/Processes/WebServerProcess.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
using System.IO;
2727
using System.Windows.Forms;
2828
using CancellationTokenSource = System.Threading.CancellationTokenSource;
29+
using System.Net.Sockets;
30+
using System.Diagnostics;
31+
using System;
2932

3033
namespace Orts.Viewer3D.Processes
3134
{
@@ -63,8 +66,22 @@ void WebServerThread()
6366

6467
string myWebContentPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Content\\Web");
6568
EndPointManager.UseIpv6 = true;
66-
using (EmbedIO.WebServer server = WebServer.CreateWebServer($"http://*:{Game.Settings.WebServerPort}", myWebContentPath))
67-
server.RunAsync(StopServer.Token).Wait();
69+
try
70+
{
71+
using (EmbedIO.WebServer server = WebServer.CreateWebServer($"http://*:{Game.Settings.WebServerPort}", myWebContentPath))
72+
server.RunAsync(StopServer.Token).Wait();
73+
}
74+
catch(AggregateException ex)
75+
{
76+
if (ex.InnerException is SocketException)
77+
{
78+
Trace.TraceWarning($"Port {Game.Settings.WebServerPort} is already in use. Continuing without webserver");
79+
}
80+
else
81+
{
82+
throw ex;
83+
}
84+
}
6885
}
6986
}
7087
}

0 commit comments

Comments
 (0)