Skip to content

Commit

Permalink
Improve network error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EarToEarOak committed Oct 22, 2015
1 parent bf434f3 commit d5a67df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions NetRemote/ControlPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ private void ServerControl()
if (_threadServer == null)
{
_server = new Server(_parser);
_server.ServerError += OnServerError;
_threadServer = new Thread(new ThreadStart(_server.Start));
_threadServer.Start();
}
Expand Down Expand Up @@ -156,5 +157,11 @@ private void OnSerialError(object sender, EventArgs e)
_threadSerial = null;
checkSerial.Checked = false;
}

private void OnServerError(object sender, EventArgs e)
{
_threadServer = null;
checkNetwork.Checked = false;
}
}
}
16 changes: 15 additions & 1 deletion NetRemote/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace SDRSharp.NetRemote

class Server
{
public event EventHandler ServerError;

private const int PORT = 3382;
private const int MAX_CLIENTS = 4;

Expand Down Expand Up @@ -86,10 +88,13 @@ public void Start()
}
catch (SocketException ex)
{
OnServerError();

if (socket.IsBound)
socket.Shutdown(SocketShutdown.Both);

MessageBox.Show(ex.Message, Info.Title(),
string msg = "Network Error:\n" + ex.Message;
MessageBox.Show(msg, Info.Title(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
Expand Down Expand Up @@ -251,6 +256,15 @@ private void OnTimerAlive(object source, ElapsedEventArgs e)
ClientRemove(client);
}
}

protected virtual void OnServerError()
{
EventHandler handler = ServerError;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}


Expand Down

0 comments on commit d5a67df

Please sign in to comment.