xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Installation / HeartbeatHandler.swift
HeartbeatHandler.swift113 lines · 3.4 KiBRaw
| 1 | // |
| 2 | // HeartbeatHandler.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 14/11/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import SwiftyMobileDevice |
| 11 | import ConcurrencyExtras |
| 12 | |
| 13 | public actor HeartbeatHandler { |
| 14 | private struct Error: Swift.Error {} |
| 15 | |
| 16 | private struct ReceivedPacket: Decodable { |
| 17 | enum Command: String, Decodable { |
| 18 | case marco = "Marco" |
| 19 | } |
| 20 | |
| 21 | let command: Command |
| 22 | let interval: Int |
| 23 | |
| 24 | private enum CodingKeys: String, CodingKey { |
| 25 | case command = "Command" |
| 26 | case interval = "Interval" |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | private struct SentPacket: Encodable { |
| 31 | enum Command: String, Encodable { |
| 32 | case polo = "Polo" |
| 33 | } |
| 34 | |
| 35 | let command: Command |
| 36 | |
| 37 | private enum CodingKeys: String, CodingKey { |
| 38 | case command = "Command" |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // the duration to wait after a failed heartbeat, before restarting |
| 43 | private static let restartInterval: TimeInterval = 1 |
| 44 | // the timeout when receiving the first heartbeat packet |
| 45 | private static let initialTimeout: TimeInterval = 30 |
| 46 | // the timeout when receiving subsequent heartbeat packets |
| 47 | private static let repeatedTimeout: TimeInterval = 5 |
| 48 | |
| 49 | private let device: Device |
| 50 | private let client: LockdownClient |
| 51 | private var task: Task<Void, Never>? |
| 52 | |
| 53 | public init(device: Device, client: LockdownClient) async throws { |
| 54 | self.device = device |
| 55 | self.client = client |
| 56 | |
| 57 | let initialClient = try await createHeartbeatClient() |
| 58 | |
| 59 | task = Task { [weak self] in |
| 60 | var client = initialClient |
| 61 | for iteration in 0... { |
| 62 | // only retains `self` for this iteration. This way, if the reference to |
| 63 | // the handler is dropped, the heartbeat stops |
| 64 | guard let self = self, !Task.isCancelled else { break } |
| 65 | |
| 66 | do { |
| 67 | try await beat(client: client, iteration: iteration) |
| 68 | } catch { |
| 69 | NSLog("%@", "Heartbeat failed: \(error)" as NSString) |
| 70 | do { |
| 71 | try await Task.sleep(seconds: Self.restartInterval) |
| 72 | client = try await createHeartbeatClient() |
| 73 | } catch { |
| 74 | return // cancelled |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private func createHeartbeatClient() async throws -> HeartbeatClient { |
| 82 | while true { |
| 83 | do { |
| 84 | return try HeartbeatClient( |
| 85 | device: device, |
| 86 | service: .init(client: client, type: HeartbeatClient.self) |
| 87 | ) |
| 88 | } catch { |
| 89 | try await Task.sleep(seconds: Self.restartInterval) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // no need for a custom deinit because the `guard let self = self` in the loop stops it |
| 95 | // when the handler deinits |
| 96 | |
| 97 | private func beat(client: HeartbeatClient, iteration: Int) async throws { |
| 98 | // allow a 30 second timeout for the first heartbeat |
| 99 | let received = try client.receive( |
| 100 | ReceivedPacket.self, |
| 101 | timeout: iteration == 0 ? Self.initialTimeout : Self.repeatedTimeout |
| 102 | ) |
| 103 | guard received.command == .marco else { throw Error() } |
| 104 | |
| 105 | try client.send(SentPacket(command: .polo)) |
| 106 | |
| 107 | try? await Task.sleep(seconds: Double(received.interval)) |
| 108 | } |
| 109 | |
| 110 | nonisolated public func stop() { |
| 111 | Task { await task?.cancel() } |
| 112 | } |
| 113 | } |