From fa4d0422115f917651a93827ae58737d624378c6 Mon Sep 17 00:00:00 2001 From: Mika Westphal Date: Sun, 15 Oct 2023 23:30:49 +0200 Subject: [PATCH] fixup! fixup! Added sleep prevention during file transfer --- NearbyShare/InboundNearbyConnection.swift | 2 +- NearbyShare/NearbyConnectionManager.swift | 1 - NearbyShare/SleepManager.swift | 50 +++++++++++------------ 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index 4dd73d6..a632058 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -329,6 +329,7 @@ class InboundNearbyConnection: NearbyConnection{ private func acceptTransfer(){ do{ for (id, file) in transferredFiles{ + SleepManager.shared.disableSleep() FileManager.default.createFile(atPath: file.destinationURL.path, contents: nil) let handle=try FileHandle(forWritingTo: file.destinationURL) transferredFiles[id]!.fileHandle=handle @@ -350,7 +351,6 @@ class InboundNearbyConnection: NearbyConnection{ try sendTransferSetupFrame(frame) }catch{ lastError=error - SleepManager.shared.enableSleep() protocolError() } } diff --git a/NearbyShare/NearbyConnectionManager.swift b/NearbyShare/NearbyConnectionManager.swift index e526f55..cb106af 100644 --- a/NearbyShare/NearbyConnectionManager.swift +++ b/NearbyShare/NearbyConnectionManager.swift @@ -183,7 +183,6 @@ public class NearbyConnectionManager : NSObject, NetServiceDelegate, InboundNear let id=UUID().uuidString let conn=InboundNearbyConnection(connection: connection, id: id) self.activeConnections[id]=conn - SleepManager.shared.disableSleep() conn.delegate=self conn.start() } diff --git a/NearbyShare/SleepManager.swift b/NearbyShare/SleepManager.swift index dccfb5b..b0bff95 100644 --- a/NearbyShare/SleepManager.swift +++ b/NearbyShare/SleepManager.swift @@ -10,30 +10,30 @@ import IOKit import IOKit.pwr_mgt class SleepManager{ - public static let shared=SleepManager() - private var assertionID: IOPMAssertionID=0 - - public func disableSleep(){ - if(assertionID != 0){ - return - } + public static let shared=SleepManager() + private var assertionID: IOPMAssertionID=0 - IOPMAssertionCreateWithName( - kIOPMAssertionTypePreventUserIdleSystemSleep as CFString, - IOPMAssertionLevel(kIOPMAssertionLevelOn), - "Data transfer over NearDrop" as CFString, - &assertionID - ) - } - - public func enableSleep(){ - if(assertionID == 0 || NearbyConnectionManager.shared.getActiveConnections() != 0){ - return - } - - IOPMAssertionRelease(assertionID) - assertionID = 0 - } - - private init(){} + public func disableSleep(){ + if(assertionID != 0){ + return + } + + IOPMAssertionCreateWithName( + kIOPMAssertionTypePreventUserIdleSystemSleep as CFString, + IOPMAssertionLevel(kIOPMAssertionLevelOn), + "Data transfer over NearDrop" as CFString, + &assertionID + ) + } + + public func enableSleep(){ + if(assertionID == 0 || NearbyConnectionManager.shared.getActiveConnections() != 0){ + return + } + + IOPMAssertionRelease(assertionID) + assertionID = 0 + } + + private init(){} }