Refactor duplicate keep-alive code

This commit is contained in:
Davide De Rosa 2018-09-08 20:29:51 +02:00
parent 3a02557b5e
commit 4af0ce8739

View File

@ -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