Rearrange shutdown code rationally

This commit is contained in:
Davide De Rosa 2018-09-07 22:43:52 +02:00
parent 8adb9871c3
commit 891c72caa1
1 changed files with 15 additions and 4 deletions

View File

@ -422,18 +422,27 @@ extension TunnelKitProvider: GenericSocketDelegate {
log.debug("Link failures so far: \(linkFailures) (max = \(maxLinkFailures))") log.debug("Link failures so far: \(linkFailures) (max = \(maxLinkFailures))")
} }
// neg timeout?
let didTimeoutNegotiation = (proxy.stopError as? SessionError == .negotiationTimeout)
// only try upgrade on network errors // only try upgrade on network errors
var upgradedSocket: GenericSocket? = nil var upgradedSocket: GenericSocket? = nil
if shutdownError as? SessionError == nil { if shutdownError as? SessionError == nil {
upgradedSocket = socket.upgraded() upgradedSocket = socket.upgraded()
} }
// clean up
finishTunnelDisconnection(error: shutdownError)
// treat negotiation timeout as socket timeout, UDP is connection-less // treat negotiation timeout as socket timeout, UDP is connection-less
if proxy.stopError as? SessionError == SessionError.negotiationTimeout { if didTimeoutNegotiation {
socketShouldChangeProtocol(socket) guard socketShouldChangeProtocol(socket) else {
// disposeTunnel
return
}
} }
finishTunnelDisconnection(error: shutdownError) // reconnect?
if reasserting { if reasserting {
guard (linkFailures < maxLinkFailures) else { guard (linkFailures < maxLinkFailures) else {
log.debug("Too many link failures (\(linkFailures)), tunnel will die now") log.debug("Too many link failures (\(linkFailures)), tunnel will die now")
@ -447,6 +456,8 @@ extension TunnelKitProvider: GenericSocketDelegate {
} }
return return
} }
// shut down
disposeTunnel(error: shutdownError) disposeTunnel(error: shutdownError)
} }