mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-11 02:51:11 +00:00
Add data transfer timer timeout
This commit is contained in:
1 parent
6985d4b1ba
commit
de7cd2bf98
3 files changed
+31
-4
No files matched your search
@@ -512,6 +512,7 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
frame.v1.type = .response
|
||||
frame.v1.connectionResponse.status = .accept
|
||||
currentState = .receivingFiles
|
||||
isTransferring = true
|
||||
try sendTransferSetupFrame(frame)
|
||||
} catch {
|
||||
lastError = error
|
||||
|
||||
@@ -27,6 +27,12 @@ class NearbyConnection {
|
||||
var lastError: Error?
|
||||
var bytesTransferred: Int64 = 0
|
||||
var cancelled: Bool = false
|
||||
var isTransferring: Bool = false {
|
||||
didSet {
|
||||
log("[NearbyConnection] Now transferring. Setting up data transfer inactivity timer.")
|
||||
startDataTransferTimer(previousBytesTransferred: bytesTransferred)
|
||||
}
|
||||
}
|
||||
|
||||
private var payloadBuffers: [Int64: NSMutableData] = [:]
|
||||
private var connectionClosed: Bool = false
|
||||
@@ -171,7 +177,7 @@ class NearbyConnection {
|
||||
return
|
||||
}
|
||||
|
||||
self.startInactivityTimer()
|
||||
self.startAndResetHeartbeatTimer()
|
||||
self.receiveFrameAsync(length: frameLength)
|
||||
}
|
||||
}
|
||||
@@ -193,7 +199,7 @@ class NearbyConnection {
|
||||
return
|
||||
}
|
||||
|
||||
self.startInactivityTimer()
|
||||
self.startAndResetHeartbeatTimer()
|
||||
self.processReceivedFrame(frameData: content)
|
||||
self.receiveFrameAsync()
|
||||
}
|
||||
@@ -556,7 +562,7 @@ class NearbyConnection {
|
||||
}
|
||||
|
||||
|
||||
func startInactivityTimer() {
|
||||
func startAndResetHeartbeatTimer() {
|
||||
|
||||
// Cancel previous timer if any
|
||||
inactivityTimer?.cancel()
|
||||
@@ -578,6 +584,25 @@ class NearbyConnection {
|
||||
}
|
||||
|
||||
|
||||
func startDataTransferTimer(previousBytesTransferred: Int64) {
|
||||
NearbyConnection.dispatchQueue.asyncAfter(deadline: .now() + timeoutInterval) {
|
||||
|
||||
if previousBytesTransferred < self.bytesTransferred {
|
||||
// everything good, transferred more than last check, schedule next check
|
||||
self.startDataTransferTimer(previousBytesTransferred: self.bytesTransferred)
|
||||
}
|
||||
else {
|
||||
// connection stale, need to abort
|
||||
if !self.connectionClosed {
|
||||
log("[NearbyConnection] Connection timeout: No more data received in \(self.timeoutInterval) seconds")
|
||||
self.lastError = NearbyError.canceled(reason: .timedOut)
|
||||
self.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// -- MARK: - Internal Data Model
|
||||
|
||||
struct InternalFileInfo {
|
||||
|
||||
@@ -362,6 +362,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
switch frame.v1.connectionResponse.status {
|
||||
case .accept:
|
||||
currentState = .sendingFiles
|
||||
isTransferring = true
|
||||
delegate?.outboundConnectionTransferAccepted(connection: self)
|
||||
|
||||
if let textToSend = textToSend {
|
||||
@@ -457,7 +458,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
// only for logging
|
||||
self.bytesTransferred += Int64(fileBuffer.count)
|
||||
|
||||
startInactivityTimer()
|
||||
startAndResetHeartbeatTimer()
|
||||
delegate?.outboundConnection(connection: self, transferProgress: Double(totalBytesSent) / Double(totalBytesToSend))
|
||||
|
||||
if currentTransfer!.currentOffset == currentTransfer!.totalBytes {
|
||||
|
||||
Reference in new issue
Block a user