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 fa4d042
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 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
1 change: 0 additions & 1 deletion 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
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 fa4d042

Please sign in to comment.