Skip to content

Commit

Permalink
fix(MulticastClient): catch and eat sender errors
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Dec 16, 2018
1 parent 8ae7ae1 commit 400e1d0
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/MulticastClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public MulticastClient(bool useIPv4, bool useIpv6, IEnumerable<NetworkInterface>
}

var localEndpoint = new IPEndPoint(address, MulticastPort);
log.Debug($"Will send to {localEndpoint}");
var sender = new UdpClient(address.AddressFamily);
try
{
Expand All @@ -89,6 +88,7 @@ public MulticastClient(bool useIPv4, bool useIpv6, IEnumerable<NetworkInterface>
throw new NotSupportedException($"Address family {address.AddressFamily}.");
}

log.Debug($"Will send via {localEndpoint}");
if (!senders.TryAdd(address, sender)) // Should not fail
{
sender.Dispose();
Expand All @@ -115,7 +115,22 @@ public MulticastClient(bool useIPv4, bool useIpv6, IEnumerable<NetworkInterface>

public async Task SendAsync(byte[] message)
{
await Task.WhenAll(senders.Select(x => x.Value.SendAsync(message, message.Length, x.Key.AddressFamily == AddressFamily.InterNetwork ? MdnsEndpointIp4 : MdnsEndpointIp6))).ConfigureAwait(false);
foreach (var sender in senders)
{
try
{
var endpoint = sender.Key.AddressFamily == AddressFamily.InterNetwork ? MdnsEndpointIp4 : MdnsEndpointIp6;
await sender.Value.SendAsync(
message, message.Length,
endpoint)
.ConfigureAwait(false);
}
catch (Exception e)
{
log.Error($"Sender {sender.Key} failure: {e.Message}");
// eat it.
}
}
}

void Listen(UdpClient receiver)
Expand Down

0 comments on commit 400e1d0

Please # to comment.