Adding a connection timeout and properly unregister peer event handlers

This commit is contained in:
WolverinDEV 2020-12-04 12:48:36 +01:00
parent 132e2f892e
commit fbb8afcc9e

View file

@ -463,6 +463,7 @@ export class RTCConnection {
private readonly sdpProcessor: SdpProcessor; private readonly sdpProcessor: SdpProcessor;
private connectionState: RTPConnectionState; private connectionState: RTPConnectionState;
private connectTimeout: number;
private failedReason: string; private failedReason: string;
private retryCalculator: RetryTimeCalculator; private retryCalculator: RetryTimeCalculator;
private retryTimestamp: number; private retryTimestamp: number;
@ -541,6 +542,7 @@ export class RTCConnection {
} }
reset(updateConnectionState: boolean) { reset(updateConnectionState: boolean) {
logTrace(LogCategory.WEBRTC, tr("Resetting the RTC connection (Updating connection state: %o)"), updateConnectionState);
if(this.peer) { if(this.peer) {
if(this.getConnection().connected()) { if(this.getConnection().connected()) {
this.getConnection().send_command("rtcsessionreset").catch(error => { this.getConnection().send_command("rtcsessionreset").catch(error => {
@ -548,17 +550,22 @@ export class RTCConnection {
}); });
} }
for(let key in this.peer) { this.peer.onconnectionstatechange = undefined;
if(!key.startsWith("on")) { this.peer.ondatachannel = undefined;
continue; this.peer.onicecandidate = undefined;
} this.peer.onicecandidateerror = undefined;
this.peer.oniceconnectionstatechange = undefined;
this.peer.onicegatheringstatechange = undefined;
this.peer.onnegotiationneeded = undefined;
this.peer.onsignalingstatechange = undefined;
this.peer.onstatsended = undefined;
this.peer.ontrack = undefined;
delete this.peer[key];
}
this.peer.close(); this.peer.close();
this.peer = undefined; this.peer = undefined;
} }
clearTimeout(this.connectTimeout);
Object.keys(this.currentTransceiver).forEach(key => this.currentTransceiver[key] = undefined); Object.keys(this.currentTransceiver).forEach(key => this.currentTransceiver[key] = undefined);
this.sdpProcessor.reset(); this.sdpProcessor.reset();
@ -872,6 +879,10 @@ export class RTCConnection {
if(this.peer !== peer) { return; } if(this.peer !== peer) { return; }
this.peer.onnegotiationneeded = () => this.handleNegotiationNeeded(); this.peer.onnegotiationneeded = () => this.handleNegotiationNeeded();
this.connectTimeout = setTimeout(() => {
this.handleFatalError("Connection initialize timeout", true);
}, 10_000);
/* Nothing left to do. Server should send a notifyrtcsessiondescription with mode answer */ /* Nothing left to do. Server should send a notifyrtcsessiondescription with mode answer */
} }
@ -942,6 +953,7 @@ export class RTCConnection {
break; break;
case "connected": case "connected":
clearTimeout(this.connectTimeout);
this.retryCalculator.reset(); this.retryCalculator.reset();
this.updateConnectionState(RTPConnectionState.CONNECTED); this.updateConnectionState(RTPConnectionState.CONNECTED);
break; break;