Skip to content

Commit

Permalink
Improvements to min-expires fix
Browse files Browse the repository at this point in the history
- Send new register immediately instead of after immediate timeout.
- Safety improvements to timeout handle usage - make sure we don't
  accidentally clear some other timeout.
  • Loading branch information
Gavin Llewellyn committed Jun 30, 2013
1 parent 4bfc34c commit 5c644a6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Registrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ Registrator.prototype = {
}

// Clear registration timer
window.clearTimeout(this.registrationTimer);
if (this.registrationTimer !== null) {
window.clearTimeout(this.registrationTimer);
this.registrationTimer = null;
}

switch(true) {
case /^1[0-9]{2}$/.test(response.status_code):
Expand Down Expand Up @@ -118,6 +121,7 @@ Registrator.prototype = {
// Re-Register before the expiration interval has elapsed.
// For that, decrease the expires value. ie: 3 seconds
this.registrationTimer = window.setTimeout(function() {
self.registrationTimer = null;
self.register();
}, (expires * 1000) - 3000);

Expand All @@ -140,9 +144,7 @@ Registrator.prototype = {
// Increase our registration interval to the suggested minimum
this.expires = response.getHeader('min-expires');
// Attempt the registration again immediately
this.registrationTimer = window.setTimeout(function() {
self.register();
}, 0);
this.register();
} else { //This response MUST contain a Min-Expires header field
console.warn(LOG_PREFIX +'423 response received for REGISTER without Min-Expires');
this.registrationFailure(response, JsSIP.C.causes.SIP_FAILURE_CODE);
Expand Down Expand Up @@ -188,7 +190,10 @@ Registrator.prototype = {
this.registered = false;

// Clear the registration timer.
window.clearTimeout(this.registrationTimer);
if (this.registrationTimer !== null) {
window.clearTimeout(this.registrationTimer);
this.registrationTimer = null;
}

if(options.all) {
extraHeaders.push('Contact: *');
Expand Down Expand Up @@ -281,7 +286,10 @@ Registrator.prototype = {
*/
onTransportClosed: function() {
this.registered_before = this.registered;
window.clearTimeout(this.registrationTimer);
if (this.registrationTimer !== null) {
window.clearTimeout(this.registrationTimer);
this.registrationTimer = null;
}

if(this.registered) {
this.registered = false;
Expand Down

0 comments on commit 5c644a6

Please sign in to comment.