-
Notifications
You must be signed in to change notification settings - Fork 79
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
Added using of NetworkChange.NetworkAddressChanged instead of Timer #36
Closed
eshvatskyi
wants to merge
1
commit into
richardschneider:master
from
eshvatskyi:milticast-nac-events
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
using Common.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using System.Net.Sockets; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Common.Logging; | ||
|
||
namespace Makaretu.Dns | ||
{ | ||
|
@@ -82,7 +82,8 @@ static MulticastService() | |
/// Always use socketLock to gain access. | ||
/// </remarks> | ||
UdpClient sender; | ||
readonly Object senderLock = new object(); | ||
|
||
readonly object senderLock = new object(); | ||
|
||
/// <summary> | ||
/// Raised when any local MDNS service sends a query. | ||
|
@@ -123,23 +124,10 @@ public MulticastService() | |
throw new InvalidOperationException("No OS support for IPv4 nor IPv6"); | ||
|
||
mdnsEndpoint = new IPEndPoint( | ||
ip6 ? MulticastAddressIp6 : MulticastAddressIp4, | ||
ip6 ? MulticastAddressIp6 : MulticastAddressIp4, | ||
MulticastPort); | ||
} | ||
|
||
/// <summary> | ||
/// The interval for discovering network interfaces. | ||
/// </summary> | ||
/// <value> | ||
/// Default is 2 minutes. | ||
/// </value> | ||
/// <remarks> | ||
/// When the interval is reached a task is started to discover any | ||
/// new network interfaces. | ||
/// </remarks> | ||
/// <seealso cref="NetworkInterfaceDiscovered"/> | ||
public TimeSpan NetworkInterfaceDiscoveryInterval { get; set; } = TimeSpan.FromMinutes(2); | ||
|
||
/// <summary> | ||
/// Get the network interfaces that are useable. | ||
/// </summary> | ||
|
@@ -200,7 +188,7 @@ public void Start() | |
knownNics.Clear(); | ||
|
||
// Start a task to find the network interfaces. | ||
PollNetworkInterfaces(); | ||
FindNetworkInterfaces(); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -235,26 +223,9 @@ public void Stop() | |
} | ||
} | ||
|
||
async void PollNetworkInterfaces() | ||
void OnNetworkAddressChanged(object sender, EventArgs e) | ||
{ | ||
var cancel = serviceCancellation.Token; | ||
try | ||
{ | ||
while (!cancel.IsCancellationRequested) | ||
{ | ||
FindNetworkInterfaces(); | ||
await Task.Delay(NetworkInterfaceDiscoveryInterval, cancel); | ||
} | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// eat it | ||
} | ||
catch (Exception e) | ||
{ | ||
log.Error(e); | ||
// eat it. | ||
} | ||
FindNetworkInterfaces(); | ||
} | ||
|
||
void FindNetworkInterfaces() | ||
|
@@ -282,6 +253,7 @@ void FindNetworkInterfaces() | |
} | ||
} | ||
} | ||
|
||
knownNics = currentNics; | ||
|
||
// If any NIC change, then get new sockets. | ||
|
@@ -297,17 +269,20 @@ void FindNetworkInterfaces() | |
sender.MulticastLoopback = true; | ||
|
||
// Start a task to listen for MDNS messages. | ||
Listener(); | ||
Task.Factory.StartNew(async () => await ListenerAsync()); | ||
} | ||
|
||
// Tell others. | ||
if (newNics.Any()) | ||
{ | ||
{ | ||
NetworkInterfaceDiscovered?.Invoke(this, new NetworkInterfaceEventArgs | ||
{ | ||
NetworkInterfaces = newNics | ||
}); | ||
} | ||
|
||
NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged; | ||
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged; | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -547,7 +522,7 @@ void OnDnsMessage(byte[] datagram, int length) | |
/// A background task to receive DNS messages from this and other MDNS services. It is | ||
/// cancelled via <see cref="Stop"/>. All messages are forwarded to <see cref="OnDnsMessage"/>. | ||
/// </remarks> | ||
async void Listener() | ||
async Task ListenerAsync() | ||
{ | ||
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
|
||
|
@@ -558,25 +533,25 @@ async void Listener() | |
} | ||
|
||
listenerCancellation = new CancellationTokenSource(); | ||
|
||
UdpClient receiver = new UdpClient(mdnsEndpoint.AddressFamily); | ||
if (isWindows) | ||
{ | ||
receiver.ExclusiveAddressUse = false; | ||
} | ||
receiver.Client.SetSocketOption( | ||
SocketOptionLevel.Socket, | ||
SocketOptionName.ReuseAddress, | ||
true); | ||
receiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | ||
if (isWindows) | ||
{ | ||
receiver.ExclusiveAddressUse = false; | ||
} | ||
var endpoint = new IPEndPoint(ip6 ? IPAddress.IPv6Any : IPAddress.Any, MulticastPort); | ||
receiver.Client.Bind(endpoint); | ||
receiver.JoinMulticastGroup(mdnsEndpoint.Address); | ||
receiver.JoinMulticastGroup(mdnsEndpoint.Address, endpoint.Address); | ||
|
||
var cancel = listenerCancellation.Token; | ||
|
||
cancel.Register(() => receiver.Dispose()); | ||
|
||
try | ||
{ | ||
while (!cancel.IsCancellationRequested) | ||
|
@@ -592,11 +567,10 @@ async void Listener() | |
// eat the exception | ||
} | ||
|
||
receiver.Dispose(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the receiver be disposed? |
||
if (listenerCancellation != null) | ||
using (var lc = listenerCancellation) | ||
{ | ||
listenerCancellation.Dispose(); | ||
listenerCancellation = null; | ||
lc?.Cancel(); | ||
} | ||
} | ||
|
||
|
@@ -618,6 +592,5 @@ public void Dispose() | |
} | ||
|
||
#endregion | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,39 +194,35 @@ public void Discover_ServiceInstance_WithAnswersContainingAdditionRecords() | |
{ | ||
var service = new ServiceProfile("y", "_sdtest-2._udp", 1024); | ||
var done = new ManualResetEvent(false); | ||
var mdns = new MulticastService(); | ||
var sd = new ServiceDiscovery(mdns) | ||
{ | ||
AnswersContainsAdditionalRecords = true | ||
}; | ||
Message discovered = null; | ||
mdns.NetworkInterfaceDiscovered += (s, e) => | ||
{ | ||
sd.QueryServiceInstances(service.ServiceName); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not be changing the existing tests. This implies a breaking change. |
||
|
||
sd.ServiceInstanceDiscovered += (s, e) => | ||
using (var mdns = new MulticastService()) | ||
using (var sd = new ServiceDiscovery(mdns) { AnswersContainsAdditionalRecords = true }) | ||
{ | ||
if (e.ServiceInstanceName == service.FullyQualifiedName) | ||
Message discovered = null; | ||
|
||
mdns.NetworkInterfaceDiscovered += (s, e) => | ||
{ | ||
Assert.IsNotNull(e.Message); | ||
discovered = e.Message; | ||
done.Set(); | ||
} | ||
}; | ||
try | ||
{ | ||
sd.QueryServiceInstances(service.ServiceName); | ||
}; | ||
|
||
sd.ServiceInstanceDiscovered += (s, e) => | ||
{ | ||
if (e.ServiceInstanceName == service.FullyQualifiedName) | ||
{ | ||
Assert.IsNotNull(e.Message); | ||
discovered = e.Message; | ||
done.Set(); | ||
} | ||
}; | ||
|
||
sd.Advertise(service); | ||
|
||
mdns.Start(); | ||
|
||
Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "instance not found"); | ||
Assert.AreEqual(0, discovered.AdditionalRecords.Count); | ||
Assert.IsTrue(discovered.Answers.Count > 1); | ||
} | ||
finally | ||
{ | ||
sd.Dispose(); | ||
mdns.Stop(); | ||
} | ||
Assert.IsTrue(discovered.AdditionalRecords.Count > 0); | ||
Assert.IsTrue(discovered.Answers.Count > 0); | ||
} | ||
} | ||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why removing then adding again?