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

View File

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