Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for resolving a service asynchronously (task-based) #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Arkane.Zeroconf/IResolvableService.cs
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
#region using

using System.Net ;
using System.Threading ;
using System.Threading.Tasks ;

#endregion

@@ -30,4 +32,6 @@ public interface IResolvableService : IService
event ServiceResolvedEventHandler Resolved ;

void Resolve () ;

Task ResolveAsync (CancellationToken cancellationToken = default) ;
}
20 changes: 16 additions & 4 deletions Arkane.Zeroconf/Providers/Bonjour/BrowseService.cs
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@
using System.Net ;
using System.Runtime.InteropServices ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;

#endregion

@@ -28,8 +30,8 @@ public BrowseService (string name, string replyDomain, string regtype) : base (n

private Native.DNSServiceQueryRecordReply queryRecordReplyHandler ;

private Action <bool> resolveAction ;
private bool resolvePending ;
private Action <bool, CancellationToken> resolveAction ;
private bool resolvePending ;

private Native.DNSServiceResolveReply resolveReplyHandler ;

@@ -43,7 +45,12 @@ public void Resolve ()
{
// If people call this in a ServiceAdded event handler (which they generally do), we need to
// invoke onto another thread, otherwise we block processing any more results.
this.resolveResult = this.resolveAction.BeginInvoke (false, null, null) ;
this.resolveResult = ResolveAsync () ;
}

public Task ResolveAsync (CancellationToken cancellationToken = default)
{
return Task.Run (() => this.resolveAction (false, cancellationToken), cancellationToken) ;
}

~BrowseService ()
@@ -60,6 +67,11 @@ private void SetupCallbacks ()
}

public void Resolve (bool requery)
{
Resolve (requery, CancellationToken.None) ;
}

public void Resolve (bool requery, CancellationToken cancellationToken)
{
if (this.resolvePending)
return ;
@@ -82,7 +94,7 @@ public void Resolve (bool requery)
if (error != ServiceError.NoError)
throw new ServiceErrorException (error) ;

sdRef.Process () ;
sdRef.Process (cancellationToken) ;
}

public void RefreshTxtRecord ()
13 changes: 11 additions & 2 deletions Arkane.Zeroconf/Providers/Bonjour/ServiceRef.cs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
#region using

using System ;
using System.Threading ;

#endregion

@@ -25,8 +26,16 @@ public struct ServiceRef

public void Process ()
{
while (this.ProcessSingle () == ServiceError.NoError)
{ }
Process(CancellationToken.None);
}

public void Process (CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
while (this.ProcessSingle() == ServiceError.NoError)
{
cancellationToken.ThrowIfCancellationRequested();
}
}

public int SocketFD => Native.DNSServiceRefSockFD (this.Raw) ;