Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Pat Hartl
883fa37756 Subscribe to beacon responses, remove use of UdpClient for probe
Some checks failed
LANCommander Development / prep (push) Successful in 11s
LANCommander Development / build_launcher_win_x64 (push) Has been cancelled
LANCommander Development / build_server_linux_x64 (push) Has been cancelled
LANCommander Development / build_server_win_x64 (push) Has been cancelled
LANCommander Development / publish_docker_image (push) Has been cancelled
LANCommander Development / publish_development_release (push) Has been cancelled
2026-01-11 21:39:09 -06:00
2 changed files with 14 additions and 9 deletions

View file

@ -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

View file

@ -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();