Postpone shutdown until notification is written

Otherwise socket might be force-closed while sending the packet.
This commit is contained in:
Davide De Rosa 2019-03-20 17:54:59 +01:00
parent c93461b153
commit a5b8907918
1 changed files with 24 additions and 10 deletions

View File

@ -1177,17 +1177,31 @@ public class SessionProxy {
private func deferStop(_ method: StopMethod, _ error: Error?) {
isStopping = true
// send exit notification if socket is unreliable (normally UDP)
if let link = link, !link.isReliable {
sendDataPackets([OCCPacket.exit.serialized()])
let completion = { [weak self] in
switch method {
case .shutdown:
self?.doShutdown(error: error)
case .reconnect:
self?.doReconnect(error: error)
}
}
switch method {
case .shutdown:
doShutdown(error: error)
case .reconnect:
doReconnect(error: error)
// shut down after sending exit notification if socket is unreliable (normally UDP)
if let link = link, !link.isReliable {
do {
guard let packets = try currentKey?.encrypt(packets: [OCCPacket.exit.serialized()]) else {
completion()
return
}
link.writePackets(packets) { (error) in
completion()
}
} catch {
completion()
}
} else {
completion()
}
}