From 4ae786d0a21682732c815d279659d4fb144e51bb Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 3 Feb 2017 20:17:42 -0500 Subject: [PATCH] Ignore CallService events related to obsolete calls. // FREEBIE --- Signal/src/call/CallService.swift | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Signal/src/call/CallService.swift b/Signal/src/call/CallService.swift index 1ef4cb966..2694607d3 100644 --- a/Signal/src/call/CallService.swift +++ b/Signal/src/call/CallService.swift @@ -75,6 +75,7 @@ enum CallError: Error { case disconnected case externalError(underlyingError: Error) case timeout(description: String) + case unexpected(description: String) } // FIXME TODO do we need to timeout? @@ -123,7 +124,7 @@ protocol CallServiceObserver: class { didSet { AssertIsOnMainThread() - Logger.debug("\(self.TAG) .peerConnectionClient setter: \(oldValue != nil) -> \(peerConnectionClient != nil)") + Logger.debug("\(self.TAG) .peerConnectionClient setter: \(oldValue != nil) -> \(peerConnectionClient != nil) \(peerConnectionClient)") } } @@ -138,6 +139,8 @@ protocol CallServiceObserver: class { updateIsVideoEnabled() + Logger.debug("\(self.TAG) .call setter: \(oldValue != nil) -> \(call != nil) \(call)") + for observer in observers { observer.value?.didUpdateCall(call:call) } @@ -424,6 +427,9 @@ protocol CallServiceObserver: class { let backgroundTask = UIApplication.shared.beginBackgroundTask { let timeout = CallError.timeout(description: "background task time ran out before call connected.") DispatchQueue.main.async { + guard self.call == newCall else { + return + } self.handleFailedCall(error: timeout) } } @@ -433,6 +439,9 @@ protocol CallServiceObserver: class { }.then { (iceServers: [RTCIceServer]) -> Promise in // FIXME for first time call recipients I think we'll see mic/camera permission requests here, // even though, from the users perspective, no incoming call is yet visible. + guard self.call == newCall else { + throw CallError.unexpected(description: "getIceServers() response for obsolete call") + } assert(self.peerConnectionClient == nil, "Unexpected PeerConnectionClient instance") Logger.debug("\(self.self.TAG) setting peerConnectionClient in \(#function)") self.peerConnectionClient = PeerConnectionClient(iceServers: iceServers, delegate: self, callType: .Incoming) @@ -443,6 +452,9 @@ protocol CallServiceObserver: class { // Find a sessionDescription compatible with my constraints and the remote sessionDescription return self.peerConnectionClient!.negotiateSessionDescription(remoteDescription: offerSessionDescription, constraints: constraints) }.then { (negotiatedSessionDescription: HardenedRTCSessionDescription) in + guard self.call == newCall else { + throw CallError.unexpected(description: "negotiateSessionDescription() response for obsolete call") + } Logger.debug("\(self.TAG) set the remote description") let answerMessage = OWSCallAnswerMessage(callId: newCall.signalingId, sessionDescription: negotiatedSessionDescription.sdp) @@ -450,6 +462,9 @@ protocol CallServiceObserver: class { return self.messageSender.sendCallMessage(callAnswerMessage) }.then { + guard self.call == newCall else { + throw CallError.unexpected(description: "sendCallMessage() response for obsolete call") + } Logger.debug("\(self.TAG) successfully sent callAnswerMessage") let (promise, fulfill, _) = Promise.pending() @@ -464,6 +479,10 @@ protocol CallServiceObserver: class { return race(promise, timeout) }.catch { error in + guard self.call == newCall else { + Logger.debug("\(self.TAG) error for obsolete call: \(error)") + return + } if let callError = error as? CallError { self.handleFailedCall(error: callError) } else {