Compare commits
1 commit
main
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
883fa37756 |
2 changed files with 14 additions and 9 deletions
|
|
@ -57,6 +57,12 @@ public class BeaconClient(
|
|||
|
||||
await probeClient.BindSocketAsync(port);
|
||||
|
||||
// Subscribe to probe responses and forward them to our event
|
||||
probeClient.OnBeaconResponse += (sender, e) =>
|
||||
{
|
||||
OnBeaconResponse?.Invoke(sender, e);
|
||||
};
|
||||
|
||||
_probeClients.Add(probeClient);
|
||||
}
|
||||
catch
|
||||
|
|
|
|||
|
|
@ -19,9 +19,8 @@ public class DiscoveryProbe : IDisposable
|
|||
|
||||
private bool _disposed = false;
|
||||
private int _port = 35891;
|
||||
private readonly UdpClient _udpClient;
|
||||
private readonly Socket _socket;
|
||||
private readonly IEnumerable<IPEndPoint> _broadcastEndpoints;
|
||||
private IEnumerable<IPEndPoint> _broadcastEndpoints = Enumerable.Empty<IPEndPoint>();
|
||||
private readonly CancellationTokenSource _cancellationTokenSource;
|
||||
private readonly byte[] _probeId;
|
||||
private readonly NetworkInterface _networkInterface;
|
||||
|
|
@ -34,12 +33,10 @@ public class DiscoveryProbe : IDisposable
|
|||
public DiscoveryProbe(NetworkInterface networkInterface)
|
||||
{
|
||||
_networkInterface = networkInterface;
|
||||
_udpClient = new UdpClient(0);
|
||||
_udpClient.EnableBroadcast = true;
|
||||
|
||||
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
_socket.EnableBroadcast = true;
|
||||
|
||||
_broadcastEndpoints = networkInterface.GetBroadcastAddresses().Select(ba => new IPEndPoint(ba, _port));
|
||||
_probeId = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
|
|
@ -50,13 +47,15 @@ public class DiscoveryProbe : IDisposable
|
|||
public async Task SendAsync()
|
||||
{
|
||||
foreach (var endpoint in _broadcastEndpoints)
|
||||
await _udpClient.SendAsync(_probeId, _probeId.Length, endpoint);
|
||||
{
|
||||
await Task.Run(() => _socket.SendTo(_probeId, SocketFlags.None, endpoint));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Listen for responses from beacons
|
||||
/// </summary>
|
||||
/// <param name="port">Port to listen on/param>
|
||||
/// <param name="port">Port to listen on</param>
|
||||
/// <exception cref="NetworkInformationException">Failed to bind to network interface</exception>
|
||||
public async Task BindSocketAsync(int port)
|
||||
{
|
||||
|
|
@ -70,6 +69,8 @@ public class DiscoveryProbe : IDisposable
|
|||
if (addressInformation == null)
|
||||
throw new NetworkInformationException();
|
||||
|
||||
_broadcastEndpoints = _networkInterface.GetBroadcastAddresses().Select(ba => new IPEndPoint(ba, _port));
|
||||
|
||||
EndPoint fromEndpoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
_socket.Bind(new IPEndPoint(addressInformation.Address, _port));
|
||||
|
|
@ -121,8 +122,6 @@ public class DiscoveryProbe : IDisposable
|
|||
{
|
||||
OnBeaconResponse = null;
|
||||
|
||||
_udpClient?.Close();
|
||||
_udpClient?.Dispose();
|
||||
_socket?.Close();
|
||||
_socket?.Dispose();
|
||||
_cancellationTokenSource?.Dispose();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue