// Cotton Calling's engine, made once per process with its live ports: the// call center, CallKit, PushKit and WebRTC.//// The PushKit registry is made at launch, before any view (`attach`), so a// VoIP push that launched the app in the background finds its handler. The// core is opened by whoever asks first (`CottonApp.open()`), and the center// asks for it through the opener it was attached with.#if os(iOS)importCottonKitimportFoundationimport UIKit@MainActorpublicfinalclassCallingEngine {publicstaticlet shared = CallingEngine()publiclet center: CallCenterpubliclet peer: WebRTCPeerprivatelet system: CallKitSystemprivatelet opener = CoreOpener()privatevar registry: VoIPRegistry?privatevar ringFrames: NSObjectProtocol?privateinit() {let log: @Sendable (String) -> Void = { Log.write($0) }let system = CallKitSystem(icon: UIImage(named: "CallKitIcon")?.pngData(), log: log)let peer = WebRTCPeer(log: log)let opener = self.opener #if targetEnvironment(simulator)// CallKit cannot hold an outgoing call on a simulator (SimulatorCallSystem).let shown = SimulatorCallSystem(log: log) #elselet shown = system #endif center = CallCenter( ports: .init(system: shown, socket: WebSocketCallSocket(log: log), peer: peer, audio: CallAudioSession(), tone: RingbackTone(), clock: MainClock()), core: { await opener.core() }, log: log ) system.center = center #if targetEnvironment(simulator) shown.center = center #endifself.system = systemself.peer = peer }/// Start listening for VoIP pushes, the core opened through `open` when/// one is first needed. Once, at launch.publicfunc attach(_ open: @escaping @Sendable () async -> (any CottonCalling)?) {guard registry == nilelse { return }Task { await opener.set(open) } registry = VoIPRegistry(handler: VoIPPushHandler(reporter: system, center: center), center: center)// A ring over the open chat socket (the Worker's way to ring a phone that has Comms open, in place of a push):// the core's ring reports it to the system, as for any ring not pushed (`CallRingFrame`).let center = center ringFrames = NotificationCenter.default.addObserver(forName: CallRingFrame.arrived, object: nil, queue: .main) { note inguardlet ring = note.object as? CallRingFrameelse { return }MainActor.assumeIsolated {switch ring {case .ring(let room, let video, let box, let at): center.incoming(room: room, video: video, ring: box, privateKey: box == nil ? nil : ChatKeychain().key(), at: at)case .stop(let room, let reason, let box, let times), .missed(let room, let reason, let box, let times):// Over the socket: nothing was reported to CallKit, so a stop for a ring never heard rings// nothing; on a missed call it writes the row, at the ring's own times (CALLS FIX 2). center.stop(room: room, reason: reason, ring: box, privateKey: box == nil ? nil : ChatKeychain().key(), times: times) } } } }}/// The core, opened on first ask and kept.private actor CoreOpener {privatevaropen: (@Sendable () async -> (any CottonCalling)?)?privatevar waiting: [CheckedContinuation<Void, Never>] = []privatevar opened: CoreCalls?func set(_ open: @escaping @Sendable () async -> (any CottonCalling)?) {self.open = open waiting.forEach { $0.resume() } waiting.removeAll() }func core() async -> (any CallsCore)? {iflet opened { return opened }ifopen == nil { await withCheckedContinuation { waiting.append($0) } }guardlet core = awaitopen?() else { returnnil }let calls = CoreCalls(core: core) opened = callsreturn calls }}#endif