Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network watcher #329

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions nanoFramework.Tools.DebugLibrary.Shared/PortTcpIp/NetworkWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NetworkWatcher : IDisposable
private const char TokenSeparator = ':';
private const string CommandDeviceStart = "+";
private const string CommandDeviceStop = "-";

private readonly int _discoveryPort;
private bool _started = false;
private Thread _threadWatch = null;
Expand Down Expand Up @@ -61,30 +61,53 @@ public void Start()
{
if (!_started)
{
_udpClient = new UdpClient(_discoveryPort);
_threadWatch = new Thread(async () =>
{
_udpClient = new UdpClient();
_udpClient.ExclusiveAddressUse = false;
_udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

IPEndPoint listeningPort = new IPEndPoint(IPAddress.Any, _discoveryPort);
IPEndPoint listeningPort = new IPEndPoint(IPAddress.Any, _discoveryPort);

_udpClient.Client.Bind(listeningPort);

_threadWatch = new Thread(() =>
{
_started = true;

Status = DeviceWatcherStatus.Started;

while (_started)
{
var message = Encoding.ASCII.GetString(_udpClient.Receive(ref listeningPort));
ProcessDiscoveryMessage(message);
try
{
var discoveryPacket = await _udpClient.ReceiveAsync();

// get address from device
// TODO
// discoveryPacket.RemoteEndPoint;

var message = Encoding.ASCII.GetString(discoveryPacket.Buffer);

ProcessDiscoveryMessage(message);
}
#if DEBUG
catch (Exception ex)
#else
catch
#endif
{
// catch all so the listener can be always listening
// on exception caused by the socket being closed, the thread will exit on the while loop condition
}
}

Status = DeviceWatcherStatus.Stopped;

// signal watcher stopped
_watcherStopped.Set();
// signal watcher stopped
_watcherStopped.Set();

})
{
Priority = ThreadPriority.Lowest
IsBackground = true
};

_threadWatch.Start();
Expand All @@ -98,7 +121,7 @@ private void ProcessDiscoveryMessage(string message)
return;
}

var tokens = message.Split(new[] {TokenSeparator});
var tokens = message.Split(new[] { TokenSeparator });

if (tokens.Length < 3)
{
Expand All @@ -112,7 +135,7 @@ private void ProcessDiscoveryMessage(string message)
{
return;
}

switch (command)
{
case CommandDeviceStart:
Expand All @@ -130,7 +153,6 @@ public void Dispose()
// try stop the watcher
Stop();


// wait 3 seconds for the watcher to be stopped
_watcherStopped.WaitOne(TimeSpan.FromSeconds(3));

Expand Down