Skip to content

Commit

Permalink
fixup! fixup! Added sleep prevention during file transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
Polyfish0 committed Oct 15, 2023
1 parent 6735b52 commit 46ae7b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion NearbyShare/InboundNearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -350,7 +351,6 @@ class InboundNearbyConnection: NearbyConnection{
try sendTransferSetupFrame(frame)
}catch{
lastError=error
SleepManager.shared.enableSleep()
protocolError()
}
}
Expand Down
11 changes: 5 additions & 6 deletions NearbyShare/NearbyConnectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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
}
}

50 changes: 25 additions & 25 deletions NearbyShare/SleepManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(){}
}

0 comments on commit 46ae7b1

Please sign in to comment.