Send pings at regular schedules

Also fixes coalescing schedules.
This commit is contained in:
Davide De Rosa 2019-12-12 16:20:59 +01:00
parent 2687dcf36e
commit e6f2f3e85a
1 changed files with 6 additions and 17 deletions

View File

@ -160,7 +160,7 @@ public class OpenVPNSession: Session {
private var lastPing: BidirectionalState<Date>
private var isStopping: Bool
private(set) var isStopping: Bool
/// The optional reason why the session stopped.
public private(set) var stopError: Error?
@ -541,30 +541,20 @@ public class OpenVPNSession: Session {
return
}
sendDataPackets(packets)
lastPing.outbound = Date()
}
// Ruby: ping
private func ping() {
guard (currentKey?.controlState == .connected) else {
guard currentKey?.controlState == .connected else {
return
}
let now = Date()
guard (now.timeIntervalSince(lastPing.inbound) <= keepAliveTimeout) else {
guard now.timeIntervalSince(lastPing.inbound) <= keepAliveTimeout else {
deferStop(.shutdown, OpenVPNError.pingTimeout)
return
}
// postpone ping if elapsed less than keep-alive
if let interval = keepAliveInterval {
let elapsed = now.timeIntervalSince(lastPing.outbound)
guard (elapsed >= interval) else {
scheduleNextPing(elapsed: elapsed)
return
}
}
log.debug("Send ping")
sendDataPackets([OpenVPN.DataPacket.pingString])
lastPing.outbound = Date()
@ -572,14 +562,13 @@ public class OpenVPNSession: Session {
scheduleNextPing()
}
private func scheduleNextPing(elapsed: TimeInterval = 0.0) {
private func scheduleNextPing() {
guard let interval = keepAliveInterval else {
log.verbose("Skip ping, keep-alive not set")
return
}
let remaining = min(interval, interval - elapsed)
log.verbose("Schedule ping after \(remaining) seconds (interval=\(interval), elapsed=\(elapsed))")
queue.asyncAfter(deadline: .now() + remaining) { [weak self] in
log.verbose("Schedule ping after \(interval) seconds")
queue.asyncAfter(deadline: .now() + interval) { [weak self] in
log.verbose("Running ping block")
self?.ping()
}