Skip to content

Commit

Permalink
Prevent probing shared services
Browse files Browse the repository at this point in the history
  • Loading branch information
jdomnitz committed Aug 8, 2024
1 parent 4360a64 commit dc80e4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/IServiceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public interface IServiceDiscovery : IDisposable
/// </summary>
/// <param name="profile"></param>
/// <returns>True if this service conflicts with an existing network service</returns>
/// <exception cref="InvalidOperationException">Thrown if a shared profile is probed</exception>
public bool Probe(ServiceProfile profile);

/// <summary>
Expand Down
30 changes: 15 additions & 15 deletions src/MulticastClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,25 @@ 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
// ReceiveAsync does not support cancellation. So the receiver is disposed
// to stop it. See https://github.com/dotnet/corefx/issues/9848
Task.Run(async () =>
{
var task = receiver.ReceiveAsync();
try
{
var task = receiver.ReceiveAsync();

_ = task.ContinueWith(x => Listen(receiver), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.RunContinuationsAsynchronously);
_ = task.ContinueWith(x => Listen(receiver), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.RunContinuationsAsynchronously);

_ = task.ContinueWith(x => MessageReceived?.Invoke(this, x.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.RunContinuationsAsynchronously);
_ = task.ContinueWith(x => MessageReceived?.Invoke(this, x.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.RunContinuationsAsynchronously);

await task.ConfigureAwait(false);
}
catch
{
return;
}
});
await task.ConfigureAwait(false);
}
catch
{
return;
}
});
}

IEnumerable<IPAddress> GetNetworkInterfaceLocalAddresses(NetworkInterface nic)
Expand Down
6 changes: 5 additions & 1 deletion src/ServiceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,16 @@ public void Advertise(ServiceProfile service)
}

/// <summary>
/// Probe the network to ensure the service is unique.
/// Probe the network to ensure the service is unique. Shared profiles should skip this step.
/// </summary>
/// <param name="profile"></param>
/// <returns>True if this service conflicts with an existing network service</returns>
/// <exception cref="InvalidOperationException">Thrown if a shared profile is probed</exception>
public bool Probe(ServiceProfile profile)
{
if (profile.SharedProfile)
throw new InvalidOperationException("Shared profiles should not be probed");

bool conflict = false;
EventHandler<MessageEventArgs> handler = (s, e) =>
{
Expand Down

0 comments on commit dc80e4a

Please sign in to comment.