File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed
Sources/AsyncDNSResolver/dnssd Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -141,6 +141,10 @@ struct DNSSD {
141
141
let serviceRefPtr = UnsafeMutablePointer< DNSServiceRef?> . allocate( capacity: 1 )
142
142
defer { serviceRefPtr. deallocate ( ) }
143
143
144
+ continuation. onTermination = { _ in
145
+ DNSServiceRefDeallocate ( serviceRefPtr. pointee)
146
+ }
147
+
144
148
// Run the query
145
149
let _code = DNSServiceQueryRecord (
146
150
serviceRefPtr,
@@ -158,9 +162,32 @@ struct DNSSD {
158
162
return continuation. finish ( throwing: AsyncDNSResolver . Error ( dnssdCode: _code) )
159
163
}
160
164
165
+ let serviceSockFD = DNSServiceRefSockFD ( serviceRefPtr. pointee)
166
+ guard serviceSockFD != - 1 else {
167
+ return continuation. finish ( throwing: AsyncDNSResolver . Error ( code: . internalError) )
168
+ }
169
+
170
+ var pollFDs = [ pollfd ( fd: serviceSockFD, events: Int16 ( POLLIN) , revents: 0 ) ]
171
+ while true {
172
+ guard !Task. isCancelled else {
173
+ return continuation. finish ( throwing: CancellationError ( ) )
174
+ }
175
+
176
+ let result = poll ( & pollFDs, 1 , 0 )
177
+ guard result != - 1 else {
178
+ return continuation. finish ( throwing: AsyncDNSResolver . Error ( code: . internalError) )
179
+ }
180
+
181
+ if result == 0 {
182
+ continue
183
+ }
184
+ if result == 1 {
185
+ break
186
+ }
187
+ }
188
+
161
189
// Read reply from the socket (blocking) then call reply handler
162
190
DNSServiceProcessResult ( serviceRefPtr. pointee)
163
- DNSServiceRefDeallocate ( serviceRefPtr. pointee)
164
191
165
192
// Streaming done
166
193
continuation. finish ( )
You can’t perform that action at this time.
0 commit comments