-
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 2 commits
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, message: "Failed to access the DNSSD service socket")) | ||
| } | ||
|
|
||
| var pollFDs = [pollfd(fd: serviceSockFD, events: Int16(POLLIN), revents: 0)] | ||
|
||
| while true { | ||
| guard !Task.isCancelled else { | ||
| return continuation.finish(throwing: AsyncDNSResolver.Error(code: .cancelled)) | ||
| } | ||
|
|
||
| let result = poll(&pollFDs, 1, 0) | ||
| guard result != -1 else { | ||
| return continuation.finish(throwing: AsyncDNSResolver.Error(code: .internalError, message: "Failed to poll the DNSSD service socket")) | ||
| } | ||
|
|
||
| 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
|
||
|
|
@@ -175,6 +202,9 @@ struct DNSSD { | |
| } | ||
| } | ||
|
|
||
| // Needed to remove the Sendable warning when deallocating DNSServiceRef in onTermination callback | ||
| extension UnsafeMutablePointer: @unchecked Sendable where Pointee == DNSServiceRef? {} | ||
|
|
||
| // MARK: - dnssd query reply handler | ||
|
|
||
| @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't we leaking the
serviceRefPointerandreplyHandlerPointerin this branch?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the code so that the deallocates are also called in this branch and the other error branch just above.