-
Notifications
You must be signed in to change notification settings - Fork 26
Make DNSSD queries cancellable #34
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
610463d
a6299e5
de2fd16
8be4b7d
472d5c0
6ad9bd9
8102fb4
792d8e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,6 +141,10 @@ struct DNSSD { | |
| let serviceRefPtr = UnsafeMutablePointer<DNSServiceRef?>.allocate(capacity: 1) | ||
| defer { serviceRefPtr.deallocate() } | ||
|
|
||
| continuation.onTermination = { _ in | ||
| DNSServiceRefDeallocate(serviceRefPtr.pointee) | ||
| } | ||
|
|
||
| // Run the query | ||
| let _code = DNSServiceQueryRecord( | ||
| serviceRefPtr, | ||
|
|
@@ -158,9 +162,32 @@ struct DNSSD { | |
| return continuation.finish(throwing: AsyncDNSResolver.Error(dnssdCode: _code)) | ||
| } | ||
|
|
||
| let serviceSockFD = DNSServiceRefSockFD(serviceRefPtr.pointee) | ||
| guard serviceSockFD != -1 else { | ||
| return continuation.finish(throwing: AsyncDNSResolver.Error(code: .internalError)) | ||
| } | ||
|
|
||
| var pollFDs = [pollfd(fd: serviceSockFD, events: Int16(POLLIN), revents: 0)] | ||
|
||
| while true { | ||
| guard !Task.isCancelled else { | ||
| return continuation.finish(throwing: CancellationError()) | ||
|
||
| } | ||
|
|
||
| let result = poll(&pollFDs, 1, 0) | ||
| guard result != -1 else { | ||
| return continuation.finish(throwing: AsyncDNSResolver.Error(code: .internalError)) | ||
victorherrerod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if result == 0 { | ||
| continue | ||
| } | ||
| if result == 1 { | ||
|
||
| break | ||
| } | ||
| } | ||
|
|
||
| // Read reply from the socket (blocking) then call reply handler | ||
| DNSServiceProcessResult(serviceRefPtr.pointee) | ||
| DNSServiceRefDeallocate(serviceRefPtr.pointee) | ||
yim-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Streaming done | ||
| continuation.finish() | ||
victorherrerod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.