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);
|
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);
|
_probeClients.Add(probeClient);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,8 @@ public class DiscoveryProbe : IDisposable
|
||||||
|
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
private int _port = 35891;
|
private int _port = 35891;
|
||||||
private readonly UdpClient _udpClient;
|
|
||||||
private readonly Socket _socket;
|
private readonly Socket _socket;
|
||||||
private readonly IEnumerable<IPEndPoint> _broadcastEndpoints;
|
private IEnumerable<IPEndPoint> _broadcastEndpoints = Enumerable.Empty<IPEndPoint>();
|
||||||
private readonly CancellationTokenSource _cancellationTokenSource;
|
private readonly CancellationTokenSource _cancellationTokenSource;
|
||||||
private readonly byte[] _probeId;
|
private readonly byte[] _probeId;
|
||||||
private readonly NetworkInterface _networkInterface;
|
private readonly NetworkInterface _networkInterface;
|
||||||
|
|
@ -34,12 +33,10 @@ public class DiscoveryProbe : IDisposable
|
||||||
public DiscoveryProbe(NetworkInterface networkInterface)
|
public DiscoveryProbe(NetworkInterface networkInterface)
|
||||||
{
|
{
|
||||||
_networkInterface = networkInterface;
|
_networkInterface = networkInterface;
|
||||||
_udpClient = new UdpClient(0);
|
|
||||||
_udpClient.EnableBroadcast = true;
|
|
||||||
|
|
||||||
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
_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());
|
_probeId = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
}
|
}
|
||||||
|
|
@ -50,13 +47,15 @@ public class DiscoveryProbe : IDisposable
|
||||||
public async Task SendAsync()
|
public async Task SendAsync()
|
||||||
{
|
{
|
||||||
foreach (var endpoint in _broadcastEndpoints)
|
foreach (var endpoint in _broadcastEndpoints)
|
||||||
await _udpClient.SendAsync(_probeId, _probeId.Length, endpoint);
|
{
|
||||||
|
await Task.Run(() => _socket.SendTo(_probeId, SocketFlags.None, endpoint));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Listen for responses from beacons
|
/// Listen for responses from beacons
|
||||||
/// </summary>
|
/// </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>
|
/// <exception cref="NetworkInformationException">Failed to bind to network interface</exception>
|
||||||
public async Task BindSocketAsync(int port)
|
public async Task BindSocketAsync(int port)
|
||||||
{
|
{
|
||||||
|
|
@ -70,6 +69,8 @@ public class DiscoveryProbe : IDisposable
|
||||||
if (addressInformation == null)
|
if (addressInformation == null)
|
||||||
throw new NetworkInformationException();
|
throw new NetworkInformationException();
|
||||||
|
|
||||||
|
_broadcastEndpoints = _networkInterface.GetBroadcastAddresses().Select(ba => new IPEndPoint(ba, _port));
|
||||||
|
|
||||||
EndPoint fromEndpoint = new IPEndPoint(IPAddress.Any, 0);
|
EndPoint fromEndpoint = new IPEndPoint(IPAddress.Any, 0);
|
||||||
|
|
||||||
_socket.Bind(new IPEndPoint(addressInformation.Address, _port));
|
_socket.Bind(new IPEndPoint(addressInformation.Address, _port));
|
||||||
|
|
@ -121,8 +122,6 @@ public class DiscoveryProbe : IDisposable
|
||||||
{
|
{
|
||||||
OnBeaconResponse = null;
|
OnBeaconResponse = null;
|
||||||
|
|
||||||
_udpClient?.Close();
|
|
||||||
_udpClient?.Dispose();
|
|
||||||
_socket?.Close();
|
_socket?.Close();
|
||||||
_socket?.Dispose();
|
_socket?.Dispose();
|
||||||
_cancellationTokenSource?.Dispose();
|
_cancellationTokenSource?.Dispose();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue