Skip to content

Commit 6b89865

Browse files
committed
Add support for resolving a service asynchronously (task-based)
Also, BeginInvoke [throws PlatformNotSupportedException on .NET Core][1] [1]: https://stackoverflow.com/questions/45183294/begininvoke-not-supported-on-net-core-platformnotsupported-exception
1 parent 64f95b4 commit 6b89865

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Arkane.Zeroconf/IResolvableService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#region using
99

1010
using System.Net ;
11+
using System.Threading ;
12+
using System.Threading.Tasks ;
1113

1214
#endregion
1315

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

3234
void Resolve () ;
35+
36+
Task ResolveAsync (CancellationToken cancellationToken = default) ;
3337
}

Arkane.Zeroconf/Providers/Bonjour/BrowseService.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using System.Net ;
1313
using System.Runtime.InteropServices ;
1414
using System.Text ;
15+
using System.Threading ;
16+
using System.Threading.Tasks ;
1517

1618
#endregion
1719

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

2931
private Native.DNSServiceQueryRecordReply queryRecordReplyHandler ;
3032

31-
private Action <bool> resolveAction ;
32-
private bool resolvePending ;
33+
private Action <bool, CancellationToken> resolveAction ;
34+
private bool resolvePending ;
3335

3436
private Native.DNSServiceResolveReply resolveReplyHandler ;
3537

@@ -43,7 +45,12 @@ public void Resolve ()
4345
{
4446
// If people call this in a ServiceAdded event handler (which they generally do), we need to
4547
// invoke onto another thread, otherwise we block processing any more results.
46-
this.resolveResult = this.resolveAction.BeginInvoke (false, null, null) ;
48+
this.resolveResult = ResolveAsync () ;
49+
}
50+
51+
public Task ResolveAsync (CancellationToken cancellationToken = default)
52+
{
53+
return Task.Run (() => this.resolveAction (false, cancellationToken), cancellationToken) ;
4754
}
4855

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

6269
public void Resolve (bool requery)
70+
{
71+
Resolve (requery, CancellationToken.None) ;
72+
}
73+
74+
public void Resolve (bool requery, CancellationToken cancellationToken)
6375
{
6476
if (this.resolvePending)
6577
return ;
@@ -82,7 +94,7 @@ public void Resolve (bool requery)
8294
if (error != ServiceError.NoError)
8395
throw new ServiceErrorException (error) ;
8496

85-
sdRef.Process () ;
97+
sdRef.Process (cancellationToken) ;
8698
}
8799

88100
public void RefreshTxtRecord ()

Arkane.Zeroconf/Providers/Bonjour/ServiceRef.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#region using
99

1010
using System ;
11+
using System.Threading ;
1112

1213
#endregion
1314

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

2627
public void Process ()
2728
{
28-
while (this.ProcessSingle () == ServiceError.NoError)
29-
{ }
29+
Process(CancellationToken.None);
30+
}
31+
32+
public void Process (CancellationToken cancellationToken)
33+
{
34+
cancellationToken.ThrowIfCancellationRequested();
35+
while (this.ProcessSingle() == ServiceError.NoError)
36+
{
37+
cancellationToken.ThrowIfCancellationRequested();
38+
}
3039
}
3140

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

0 commit comments

Comments
 (0)