mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-19 04:46:08 +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.type = .response
|
||||||
frame.v1.connectionResponse.status = .accept
|
frame.v1.connectionResponse.status = .accept
|
||||||
currentState = .receivingFiles
|
currentState = .receivingFiles
|
||||||
|
isTransferring = true
|
||||||
try sendTransferSetupFrame(frame)
|
try sendTransferSetupFrame(frame)
|
||||||
} catch {
|
} catch {
|
||||||
lastError = error
|
lastError = error
|
||||||
|
|||||||
@@ -27,6 +27,12 @@ class NearbyConnection {
|
|||||||
var lastError: Error?
|
var lastError: Error?
|
||||||
var bytesTransferred: Int64 = 0
|
var bytesTransferred: Int64 = 0
|
||||||
var cancelled: Bool = false
|
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 payloadBuffers: [Int64: NSMutableData] = [:]
|
||||||
private var connectionClosed: Bool = false
|
private var connectionClosed: Bool = false
|
||||||
@@ -171,7 +177,7 @@ class NearbyConnection {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.startInactivityTimer()
|
self.startAndResetHeartbeatTimer()
|
||||||
self.receiveFrameAsync(length: frameLength)
|
self.receiveFrameAsync(length: frameLength)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -193,7 +199,7 @@ class NearbyConnection {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.startInactivityTimer()
|
self.startAndResetHeartbeatTimer()
|
||||||
self.processReceivedFrame(frameData: content)
|
self.processReceivedFrame(frameData: content)
|
||||||
self.receiveFrameAsync()
|
self.receiveFrameAsync()
|
||||||
}
|
}
|
||||||
@@ -556,7 +562,7 @@ class NearbyConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func startInactivityTimer() {
|
func startAndResetHeartbeatTimer() {
|
||||||
|
|
||||||
// Cancel previous timer if any
|
// Cancel previous timer if any
|
||||||
inactivityTimer?.cancel()
|
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
|
// -- MARK: - Internal Data Model
|
||||||
|
|
||||||
struct InternalFileInfo {
|
struct InternalFileInfo {
|
||||||
|
|||||||
@@ -362,6 +362,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
|||||||
switch frame.v1.connectionResponse.status {
|
switch frame.v1.connectionResponse.status {
|
||||||
case .accept:
|
case .accept:
|
||||||
currentState = .sendingFiles
|
currentState = .sendingFiles
|
||||||
|
isTransferring = true
|
||||||
delegate?.outboundConnectionTransferAccepted(connection: self)
|
delegate?.outboundConnectionTransferAccepted(connection: self)
|
||||||
|
|
||||||
if let textToSend = textToSend {
|
if let textToSend = textToSend {
|
||||||
@@ -457,7 +458,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
|||||||
// only for logging
|
// only for logging
|
||||||
self.bytesTransferred += Int64(fileBuffer.count)
|
self.bytesTransferred += Int64(fileBuffer.count)
|
||||||
|
|
||||||
startInactivityTimer()
|
startAndResetHeartbeatTimer()
|
||||||
delegate?.outboundConnection(connection: self, transferProgress: Double(totalBytesSent) / Double(totalBytesToSend))
|
delegate?.outboundConnection(connection: self, transferProgress: Double(totalBytesSent) / Double(totalBytesToSend))
|
||||||
|
|
||||||
if currentTransfer!.currentOffset == currentTransfer!.totalBytes {
|
if currentTransfer!.currentOffset == currentTransfer!.totalBytes {
|
||||||
|
|||||||
Reference in new issue
Block a user