Skip to content

Commit af70432

Browse files
committed
Fixes from code review
1 parent 055d4fe commit af70432

4 files changed

Lines changed: 51 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ NetworkHelper.SetupNetworkHelper(requiresDateTime: true);
6363

6464
`SetupNetworkHelper` throws `InvalidOperationException` if called a second time without a prior `Reset()`. Token-based methods (`SetupAndConnectNetwork`) do not have this restriction and are always retryable.
6565

66-
##
66+
## Feedback and documentation
6767

6868
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).
6969

‎Tests/NetworkHelperTests/ConnectToEthernetTests.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ public void TestRetryAfterTimeout()
107107

108108
// If there is a network, second attempt should succeed;
109109
// if not, it will time out again — either way, it must NOT throw
110-
Assert.IsTrue(NetworkHelper.Status != NetworkHelperStatus.None);
110+
Assert.IsTrue(
111+
NetworkHelper.Status == NetworkHelperStatus.NetworkIsReady ||
112+
NetworkHelper.Status == NetworkHelperStatus.TokenExpiredWaitingIPAddress ||
113+
NetworkHelper.Status == NetworkHelperStatus.TokenExpiredWaitingDateTime,
114+
"Expected a terminal status after the second attempt");
111115

112116
NetworkHelper.Reset();
113117
}

‎nanoFramework.System.Net/NetworkHelper/NetworkHelper.cs‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public static class NetworkHelper
2727

2828
private static IPConfiguration _ipConfiguration;
2929

30+
private static Thread _workerThread;
31+
private static volatile bool _stopRequested;
32+
3033
/// <summary>
3134
/// This flag will make sure there is only one and only one call to the event-based helper methods.
3235
/// </summary>
@@ -69,7 +72,8 @@ public static void SetupNetworkHelper(bool requiresDateTime = false)
6972
SetupHelper(true);
7073

7174
// fire working thread
72-
new Thread(WorkingThread).Start();
75+
_workerThread = new Thread(WorkingThread);
76+
_workerThread.Start();
7377
}
7478

7579
/// <summary>
@@ -90,7 +94,8 @@ public static void SetupNetworkHelper(
9094
SetupHelper(true);
9195

9296
// fire working thread
93-
new Thread(WorkingThread).Start();
97+
_workerThread = new Thread(WorkingThread);
98+
_workerThread.Start();
9499
}
95100

96101
/// <summary>
@@ -143,6 +148,22 @@ public static void Reset()
143148
// deregister event handler to prevent a handler leak
144149
NetworkChange.NetworkAddressChanged -= AddressChangedCallback;
145150

151+
// signal the worker thread to stop and unblock it if it is waiting for an IP address
152+
_stopRequested = true;
153+
154+
if (_ipAddressAvailable != null)
155+
{
156+
_ipAddressAvailable.Set();
157+
}
158+
159+
if (_workerThread != null)
160+
{
161+
// give the thread a moment to exit cleanly before clearing shared state
162+
_workerThread.Join(1000);
163+
_workerThread = null;
164+
}
165+
166+
_stopRequested = false;
146167
_helperInstanciated = false;
147168
_ipAddressAvailable = null;
148169
_networkReady = new(false);
@@ -228,16 +249,28 @@ private static void WorkingThread()
228249
_workingNetworkInterface,
229250
_ipConfiguration))
230251
{
231-
// wait here until we have an IP address
252+
// wait here until we have an IP address or until Reset() unblocks us
232253
_ipAddressAvailable.WaitOne();
233254
}
234255

256+
// bail out if Reset() was called while we were waiting
257+
if (_stopRequested)
258+
{
259+
return;
260+
}
261+
235262
if (_requiresDateTime)
236263
{
237264
// wait until there is a valid DateTime
238265
NetworkHelperInternal.WaitForValidDateTime();
239266
}
240267

268+
// bail out if Reset() was called during the DateTime wait
269+
if (_stopRequested)
270+
{
271+
return;
272+
}
273+
241274
// all conditions met
242275
_networkReady.Set();
243276

@@ -247,6 +280,11 @@ private static void WorkingThread()
247280

248281
private static void AddressChangedCallback(object sender, EventArgs e)
249282
{
283+
if (_stopRequested)
284+
{
285+
return;
286+
}
287+
250288
if (NetworkHelperInternal.CheckIP(
251289
_workingNetworkInterface,
252290
_ipConfiguration))

‎nanoFramework.System.Net/NetworkHelper/NetworkHelperStatus.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public enum NetworkHelperStatus
4343
ErrorConnetingToWiFiWhileScanning,
4444

4545
/// <summary>
46-
/// The network was previously ready but the IP address was lost. Waiting for the connection to be restored.
46+
/// An exception occurred with waiting for the network to become ready. Check HelperException property to find the <see cref="Exception"/> that was thrown.
4747
/// </summary>
48-
Reconnecting,
48+
ExceptionOccurred,
4949

5050
/// <summary>
51-
/// An exception occurred with waiting for the network to become ready. Check HelperException property to find the <see cref="Exception"/> that was thrown.
51+
/// The network was previously ready but the IP address was lost. Waiting for the connection to be restored.
5252
/// </summary>
53-
ExceptionOccurred
53+
Reconnecting
5454
}
5555
}

0 commit comments

Comments
 (0)