Run a virtual iPhone on an Apple Silicon Mac.
Swift
Poll vphoned's device state only while an event socket is open
Jackyafc62ef0d783
diff --git a/Research/vphoned_http_api.md b/Research/vphoned_http_api.mdindex 236663f2..39319230 100644--- a/Research/vphoned_http_api.md+++ b/Research/vphoned_http_api.md@@ -243,7 +243,9 @@ the same response shape; requests may complete out of order, so clientscorrelate them by `id`. The socket also sends`{ "type": "event", "event": "...", "data": { ... } }`. The initial event is`connected`; changes to screen, frontmost app, or low power mode emit-`device.state`, and completed operations emit `operation.completed`. Ping frames+`device.state` (vphoned checks them every three seconds while an event socket+is open, and not at all otherwise), and completed operations emit+`operation.completed`. Ping framesreceive pong frames. JSON WebSocket frames are limited to 1 MiB afterfragment reassembly.diff --git a/VPhoneDaemon/Daemon/APIWire.swift b/VPhoneDaemon/Daemon/APIWire.swiftindex ad167fa4..5742ba52 100644--- a/VPhoneDaemon/Daemon/APIWire.swift+++ b/VPhoneDaemon/Daemon/APIWire.swift@@ -74,17 +74,24 @@ enum APIWire {final class APIEventHub: @unchecked Sendable {private let lock = NSLock()private var channels: [ObjectIdentifier: Channel] = [:]+ /// Called with true when the first subscriber arrives and false when the+ /// last one leaves, outside the lock. Set once, before any client connects.+ var onSubscribedChange: (@Sendable (Bool) -> Void)?func add(_ channel: Channel) {lock.lock()+ let first = channels.isEmptychannels[ObjectIdentifier(channel)] = channellock.unlock()+ if first { onSubscribedChange?(true) }}func remove(_ channel: Channel) {lock.lock()- channels.removeValue(forKey: ObjectIdentifier(channel))+ let removed = channels.removeValue(forKey: ObjectIdentifier(channel)) != nil+ let last = removed && channels.isEmptylock.unlock()+ if last { onSubscribedChange?(false) }}func broadcast(name: String, data: [String: Any]) {diff --git a/VPhoneDaemon/Daemon/GuestEventPublisher.swift b/VPhoneDaemon/Daemon/GuestEventPublisher.swiftindex 8bd23bb2..21af4220 100644--- a/VPhoneDaemon/Daemon/GuestEventPublisher.swift+++ b/VPhoneDaemon/Daemon/GuestEventPublisher.swift@@ -3,18 +3,40 @@ import IcliKit/// Publishes the state a UI needs without requiring it to poll over HTTP./// State polling runs separately so a stalled powerd read cannot block commands.+/// It runs only while a client is subscribed: with nobody listening the timer+/// used to wake vphoned every three seconds for nothing.final class GuestEventPublisher: @unchecked Sendable {private static let queue = DispatchQueue(label: "vphoned.api.state", qos: .utility)+ private static let interval: DispatchTimeInterval = .seconds(3)private let hub: APIEventHubprivate let timer: DispatchSourceTimer+ /// Owned by `queue`.private var previous: Data?+ private var polling = falseinit(hub: APIEventHub) {self.hub = hubtimer = DispatchSource.makeTimerSource(queue: Self.queue)- timer.schedule(deadline: .now(), repeating: .seconds(3))+ timer.schedule(deadline: .distantFuture)timer.setEventHandler { [weak self] in self?.publishIfChanged() }timer.resume()+ hub.onSubscribedChange = { [weak self] _ in+ Self.queue.async { self?.updatePolling() }+ }+ }++ /// Follows the hub's current state rather than the change that was+ /// reported, so two changes delivered out of order still end right.+ private func updatePolling() {+ let subscribed = hub.hasSubscribers+ guard subscribed != polling else { return }+ polling = subscribed+ if subscribed {+ timer.schedule(deadline: .now(), repeating: Self.interval)+ } else {+ previous = nil+ timer.schedule(deadline: .distantFuture)+ }}private func publishIfChanged() {