Skip to content

Commit

Permalink
Merge pull request #81 from richardschneider/cancel-udp
Browse files Browse the repository at this point in the history
ignore receive error.  Fixes #79
  • Loading branch information
richardschneider authored Oct 5, 2019
2 parents 597bc97 + 94e3416 commit d16cff2
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/MulticastClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ await sender.Value.SendAsync(

void Listen(UdpClient receiver)
{
// ReceiveAsync does not support cancellation. So the receiver is disposed
// to stop it. See https://github.com/dotnet/corefx/issues/9848
Task.Run(async () =>
{
try
Expand All @@ -173,6 +175,10 @@ void Listen(UdpClient receiver)

await task.ConfigureAwait(false);
}
catch (NullReferenceException)
{
return;
}
catch (ObjectDisposedException)
{
return;
Expand Down Expand Up @@ -204,15 +210,29 @@ protected virtual void Dispose(bool disposing)

foreach (var receiver in receivers)
{
receiver.Dispose();
try
{
receiver.Dispose();
}
catch
{
// eat it.
}
}
receivers.Clear();

foreach (var address in senders.Keys)
{
if (senders.TryRemove(address, out var sender))
{
sender.Dispose();
try
{
sender.Dispose();
}
catch
{
// eat it.
}
}
}
senders.Clear();
Expand Down

0 comments on commit d16cff2

Please sign in to comment.