From 46ae7b16569c29465aad2cedfc56143ed9e75242 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 | 11 +++-- NearbyShare/SleepManager.swift | 50 +++++++++++------------ 3 files changed, 31 insertions(+), 32 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..932cf84 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() } @@ -227,7 +226,7 @@ public class NearbyConnectionManager : NSObject, NetServiceDelegate, InboundNear guard let delegate=mainAppDelegate else {return} delegate.incomingTransfer(id: connection.id, didFinishWith: error) activeConnections.removeValue(forKey: connection.id) - SleepManager.shared.enableSleep() + SleepManager.shared.enableSleep() } public func submitUserConsent(transferID:String, accept:Bool){ @@ -387,9 +386,9 @@ public class NearbyConnectionManager : NSObject, NetServiceDelegate, InboundNear } outgoingTransfers.removeValue(forKey: connection.id) } - - public func getActiveConnections() -> Int { - return activeConnections.count - } + + public func getActiveConnections() -> Int { + return activeConnections.count + } } 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(){} }