From 387c1e536ec8a753ffd30f05490304dfd4319bf4 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 23 May 2014 21:02:09 -0700 Subject: [PATCH] udp client: clean up timer situation --- index.js | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 0dd651f..65545f1 100644 --- a/index.js +++ b/index.js @@ -170,15 +170,12 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { var socket = dgram.createSocket('udp4') var transactionId = new Buffer(hat(32), 'hex') - var timeout = setTimeout(function () { - error('tracker request timed out') - }, 15000) - - if (opts.event !== EVENTS.stopped) { + if (opts.event !== 'stopped') { // if we're sending a stopped message, we don't really care if it arrives, so don't // set a timer var timeout = setTimeout(function () { - try { socket.close() } catch (err) {} + timeout = null + cleanup() error('tracker request timed out') }, 15000) } @@ -217,6 +214,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { return case 1: // announce + cleanup() if (msg.length < 20) { return error('invalid announce message') } @@ -237,46 +235,28 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { compact2string.multi(msg.slice(20)).forEach(function (addr) { self.client.emit('peer', addr) }) - - if (timeout) { - clearTimeout(timeout) - timeout = null - } - try { socket.close() } catch (err) {} - return + break case 2: // scrape + cleanup() if (msg.length < 20) { return error('invalid scrape message') } - self.client.emit('scrape', { announce: self._announceUrl, complete: msg.readUInt32BE(8), downloaded: msg.readUInt32BE(12), incomplete: msg.readUInt32BE(16) }) - - if (timeout) { - clearTimeout(timeout) - timeout = null - } - try { socket.close() } catch (err) {} - return + break case 3: // error + cleanup() if (msg.length < 8) { return error('invalid error message') } - self.client.emit('error', new Error(msg.slice(8).toString())) - - if (timeout) { - clearTimeout(timeout) - timeout = null - } - try { socket.close() } catch (err) {} - return + break } }) @@ -289,11 +269,15 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { function error (message) { self.client.emit('error', new Error(message + ' (connecting to tracker ' + requestUrl + ')')) + cleanup() + } + + function cleanup () { if (timeout) { clearTimeout(timeout) timeout = null } - try { socket.close() } catch (err) { } + try { socket.close() } catch (err) {} } function genTransactionId () {