]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
DNSResolver: Simplify
authorRoopesh Chander <roop@roopc.net>
Thu, 8 Nov 2018 11:10:38 +0000 (16:40 +0530)
committerRoopesh Chander <roop@roopc.net>
Thu, 8 Nov 2018 12:22:11 +0000 (17:52 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuardNetworkExtension/DNSResolver.swift

index d69b96d61c77bd5d57ed811e316fb46a2374480c..0874fd9687bcfdb075f37ff2fe811ba3ec647e75 100644 (file)
@@ -9,18 +9,9 @@ enum DNSResolverError: Error {
 }
 
 class DNSResolver {
-    let endpoints: [Endpoint?]
-    let dispatchGroup: DispatchGroup
-    var dispatchWorkItems: [DispatchWorkItem]
 
-    init(endpoints: [Endpoint?]) {
-        self.endpoints = endpoints
-        self.dispatchWorkItems = []
-        self.dispatchGroup = DispatchGroup()
-    }
-
-    func isAllEndpointsAlreadyResolved() -> Bool {
-        for endpoint in self.endpoints {
+    static func isAllEndpointsAlreadyResolved(endpoints: [Endpoint?]) -> Bool {
+        for endpoint in endpoints {
             guard let endpoint = endpoint else { continue }
             if (!endpoint.hasHostAsIPAddress()) {
                 return false
@@ -29,17 +20,15 @@ class DNSResolver {
         return true
     }
 
-    func resolveSync() throws -> [Endpoint?] {
-        let endpoints = self.endpoints
-        let dispatchGroup = self.dispatchGroup
-        dispatchWorkItems = []
+    static func resolveSync(endpoints: [Endpoint?]) throws -> [Endpoint?] {
+        let dispatchGroup: DispatchGroup = DispatchGroup()
 
-        if (isAllEndpointsAlreadyResolved()) {
+        if (isAllEndpointsAlreadyResolved(endpoints: endpoints)) {
             return endpoints
         }
 
         var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
-        for (i, endpoint) in self.endpoints.enumerated() {
+        for (i, endpoint) in endpoints.enumerated() {
             guard let endpoint = endpoint else { continue }
             if (endpoint.hasHostAsIPAddress()) {
                 resolvedEndpoints[i] = endpoint
@@ -47,7 +36,6 @@ class DNSResolver {
                 let workItem = DispatchWorkItem {
                     resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint)
                 }
-                dispatchWorkItems.append(workItem)
                 DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
             }
         }
@@ -72,16 +60,6 @@ class DNSResolver {
         }
         return resolvedEndpoints
     }
-
-    func cancel() {
-        for workItem in dispatchWorkItems {
-            workItem.cancel()
-        }
-    }
-
-    deinit {
-        cancel()
-    }
 }
 
 extension DNSResolver {