Skip to content

Commit d5a67df

Browse files
committed
Improve network error handling
1 parent bf434f3 commit d5a67df

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

NetRemote/ControlPanel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private void ServerControl()
102102
if (_threadServer == null)
103103
{
104104
_server = new Server(_parser);
105+
_server.ServerError += OnServerError;
105106
_threadServer = new Thread(new ThreadStart(_server.Start));
106107
_threadServer.Start();
107108
}
@@ -156,5 +157,11 @@ private void OnSerialError(object sender, EventArgs e)
156157
_threadSerial = null;
157158
checkSerial.Checked = false;
158159
}
160+
161+
private void OnServerError(object sender, EventArgs e)
162+
{
163+
_threadServer = null;
164+
checkNetwork.Checked = false;
165+
}
159166
}
160167
}

NetRemote/Server.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ namespace SDRSharp.NetRemote
4242

4343
class Server
4444
{
45+
public event EventHandler ServerError;
46+
4547
private const int PORT = 3382;
4648
private const int MAX_CLIENTS = 4;
4749

@@ -86,10 +88,13 @@ public void Start()
8688
}
8789
catch (SocketException ex)
8890
{
91+
OnServerError();
92+
8993
if (socket.IsBound)
9094
socket.Shutdown(SocketShutdown.Both);
9195

92-
MessageBox.Show(ex.Message, Info.Title(),
96+
string msg = "Network Error:\n" + ex.Message;
97+
MessageBox.Show(msg, Info.Title(),
9398
MessageBoxButtons.OK, MessageBoxIcon.Error);
9499
}
95100
finally
@@ -251,6 +256,15 @@ private void OnTimerAlive(object source, ElapsedEventArgs e)
251256
ClientRemove(client);
252257
}
253258
}
259+
260+
protected virtual void OnServerError()
261+
{
262+
EventHandler handler = ServerError;
263+
if (handler != null)
264+
{
265+
handler(this, EventArgs.Empty);
266+
}
267+
}
254268
}
255269

256270

0 commit comments

Comments
 (0)