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 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) let elapsed = now.timeIntervalSince(lastPingOut)
guard (elapsed >= interval) else { guard (elapsed >= interval) else {
let remaining = min(interval, interval - elapsed) scheduleNextPing(elapsed: elapsed)
queue.asyncAfter(deadline: .now() + remaining) { [weak self] in
self?.ping()
}
return return
} }
} }
@ -610,10 +608,16 @@ public class SessionProxy {
sendDataPackets([DataPacket.pingString]) sendDataPackets([DataPacket.pingString])
lastPingOut = Date() lastPingOut = Date()
if let interval = configuration.keepAliveInterval, interval > 0 { scheduleNextPing()
queue.asyncAfter(deadline: .now() + interval) { [weak self] in }
self?.ping()
} 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) delegate?.sessionDidStart(self, remoteAddress: remoteAddress, reply: reply)
if let interval = configuration.keepAliveInterval, interval > 0 { scheduleNextPing()
queue.asyncAfter(deadline: .now() + interval) { [weak self] in
self?.ping()
}
}
} }
// Ruby: transition_keys // Ruby: transition_keys