Refactor duplicate keep-alive code
This commit is contained in:
parent
3a02557b5e
commit
4af0ce8739
@ -595,13 +595,11 @@ public class SessionProxy {
|
||||
return
|
||||
}
|
||||
|
||||
if let interval = configuration.keepAliveInterval, interval > 0 {
|
||||
// postpone ping if elapsed less than keep-alive
|
||||
if let interval = keepAliveInterval {
|
||||
let elapsed = now.timeIntervalSince(lastPingOut)
|
||||
guard (elapsed >= interval) else {
|
||||
let remaining = min(interval, interval - elapsed)
|
||||
queue.asyncAfter(deadline: .now() + remaining) { [weak self] in
|
||||
self?.ping()
|
||||
}
|
||||
scheduleNextPing(elapsed: elapsed)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -610,10 +608,16 @@ public class SessionProxy {
|
||||
sendDataPackets([DataPacket.pingString])
|
||||
lastPingOut = Date()
|
||||
|
||||
if let interval = configuration.keepAliveInterval, interval > 0 {
|
||||
queue.asyncAfter(deadline: .now() + interval) { [weak self] in
|
||||
self?.ping()
|
||||
scheduleNextPing()
|
||||
}
|
||||
|
||||
private func scheduleNextPing(elapsed: TimeInterval = 0.0) {
|
||||
guard let interval = keepAliveInterval else {
|
||||
return
|
||||
}
|
||||
let remaining = min(interval, interval - elapsed)
|
||||
queue.asyncAfter(deadline: .now() + remaining) { [weak self] in
|
||||
self?.ping()
|
||||
}
|
||||
}
|
||||
|
||||
@ -916,11 +920,7 @@ public class SessionProxy {
|
||||
}
|
||||
delegate?.sessionDidStart(self, remoteAddress: remoteAddress, reply: reply)
|
||||
|
||||
if let interval = configuration.keepAliveInterval, interval > 0 {
|
||||
queue.asyncAfter(deadline: .now() + interval) { [weak self] in
|
||||
self?.ping()
|
||||
}
|
||||
}
|
||||
scheduleNextPing()
|
||||
}
|
||||
|
||||
// Ruby: transition_keys
|
||||
|
Loading…
Reference in New Issue
Block a user