         lastError = Int(DNSServiceProcessResult(sd))
     }
 
+    /// Publishing or resolving finished successfully or with an error
+    func finished(domain: String? = nil, error e: Any? = nil) {
+        let dict: DNSSDNetService.ErrorDictionary
+        if let domain = domain, let error = e {
+            dict = [
+                DNSSDNetService.errorDomain : domain,
+                DNSSDNetService.errorCode   : error
+            ]
+        } else {
+            dict = errorDictionary
+        }
+        if isResolving {
+            isResolving = false
+            if e == nil && lastError == kDNSServiceErr_NoError {
+                delegate?.netServiceDidResolveAddress(self)
+            } else {
+                delegate?.netService(self, didNotResolve: dict)
+            }
+        }
+        if isPublishing {
+            isPublishing = false
+            if e == nil && lastError == kDNSServiceErr_NoError {
+                delegate?.netServiceDidPublish(self)
+            } else {
+                delegate?.netService(self, didNotPublish: dict)
+            }
+        }
+    }
+
     /// Publish the net service.  This function returns immediately.
     ///
     /// - Parameter options: publishing options

            let this = Unmanaged<DNSSDNetService>.fromOpaque(context).takeRetainedValue()
            if let name = name, let n = String(validatingUTF8: name) { this._name = n }
            if let host = host, let h = String(validatingUTF8: host) { this.hostName = h }
            this.lastError = Int(err)
            guard this.lastError == kDNSServiceErr_NoError else {
                this.delegate?.netService(this, didNotResolve: this.errorDictionary)
                return
            }
            this.delegate?.netServiceDidResolveAddress(this)
        }, this))
        guard lastError == kDNSServiceErr_NoError else {
            delegate?.netService(self, didNotResolve: errorDictionary)

    /// - Parameters:
    ///   - domain: error domain
    ///   - error: error code
    func cancel(domain d: String = DNSSDNetService.netServiceErrorDomain, error e: Any = DNSSDNetService.ErrorCode.cancelledError) {
        if let sd = sd {
            DNSServiceRefDeallocate(sd)
            self.sd = nil
            finished(domain: d, error: e)
        }
    }

