Skip to content

Commit

Permalink
Server launch and close fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkno committed Aug 31, 2014
1 parent c700841 commit 163d7f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
11 changes: 8 additions & 3 deletions PCR1000/PcrNetworkComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public PcrNetworkServer(IComm pcrComm, int netport = 4456, bool ssl = false, str
Debug.WriteLine("PcrNetwork Created");
}

private bool _listenContinue = true;
private bool _listenContinue;

/// <summary>
/// Listen for a new incoming connection.
Expand Down Expand Up @@ -477,6 +477,8 @@ private void PcrCommOnDataReceived(IComm sender, DateTime recvTime, string data)
}
}

private Thread _listenThread;

/// <summary>
/// Starts the network server.
/// </summary>
Expand All @@ -493,8 +495,8 @@ public bool Start()

_listenContinue = true;
_tcpListener = new TcpListener(IPAddress.Any, _port);
var listenThread = new Thread(ListenForClients);
listenThread.Start();
_listenThread = new Thread(ListenForClients);
_listenThread.Start();
}
catch (Exception)
{
Expand All @@ -516,6 +518,9 @@ public bool Stop()
{
return false;
}
_listenContinue = false;
_tcpListener.Stop();
_listenThread.Abort();
}
catch (Exception)
{
Expand Down
7 changes: 4 additions & 3 deletions PCRNetworkServer/Gui.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions PCRNetworkServer/Gui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Gui()
private bool CheckSettings()
{
int port;
if (int.TryParse(textBoxNetwork.Text, out port) || port <= 0)
if (!int.TryParse(textBoxNetwork.Text, out port) || port <= 0)
{
return false;
}
Expand All @@ -66,7 +66,10 @@ private void ButtonOnOffClick(object sender, EventArgs e)
{
try
{
_pcrNetworkServer.Stop();
if (!_pcrNetworkServer.Stop())
{
throw new Exception("A fatal error occured while stopping the server. Please check debug messages for details.");
}
}
catch (Exception ex)
{
Expand All @@ -77,6 +80,9 @@ private void ButtonOnOffClick(object sender, EventArgs e)
buttonOnOff.BackColor = Color.FromArgb(255, 128, 128);
textBoxNetwork.Enabled = true;
comboBoxSerialPort.Enabled = true;
checkBoxUseSecurity.Enabled = true;
checkBoxSsl.Enabled = checkBoxUseSecurity.Checked;
textBoxPassword.Enabled = checkBoxUseSecurity.Checked;
_isOn = false;
break;
}
Expand All @@ -99,6 +105,11 @@ private void ButtonOnOffClick(object sender, EventArgs e)
#if DEBUG
_pcrNetworkServer.SetDebugLogger(true);
#endif

if (!_pcrNetworkServer.Start())
{
throw new Exception("Server startup failed. Review debug messages for details.");
}
}
catch (Exception ex)
{
Expand All @@ -111,6 +122,9 @@ private void ButtonOnOffClick(object sender, EventArgs e)
buttonOnOff.BackColor = Color.FromArgb(192, 255, 192);
textBoxNetwork.Enabled = false;
comboBoxSerialPort.Enabled = false;
checkBoxUseSecurity.Enabled = false;
checkBoxSsl.Enabled = false;
textBoxPassword.Enabled = false;
_isOn = true;
break;
}
Expand Down

0 comments on commit 163d7f7

Please sign in to comment.