udp client: clean up timer situation

This commit is contained in:
Feross Aboukhadijeh 2014-05-23 21:02:09 -07:00
parent cc4c73ec24
commit 387c1e536e

View File

@ -170,15 +170,12 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
var socket = dgram.createSocket('udp4') var socket = dgram.createSocket('udp4')
var transactionId = new Buffer(hat(32), 'hex') var transactionId = new Buffer(hat(32), 'hex')
var timeout = setTimeout(function () { if (opts.event !== 'stopped') {
error('tracker request timed out')
}, 15000)
if (opts.event !== EVENTS.stopped) {
// if we're sending a stopped message, we don't really care if it arrives, so don't // if we're sending a stopped message, we don't really care if it arrives, so don't
// set a timer // set a timer
var timeout = setTimeout(function () { var timeout = setTimeout(function () {
try { socket.close() } catch (err) {} timeout = null
cleanup()
error('tracker request timed out') error('tracker request timed out')
}, 15000) }, 15000)
} }
@ -217,6 +214,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
return return
case 1: // announce case 1: // announce
cleanup()
if (msg.length < 20) { if (msg.length < 20) {
return error('invalid announce message') return error('invalid announce message')
} }
@ -237,46 +235,28 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
compact2string.multi(msg.slice(20)).forEach(function (addr) { compact2string.multi(msg.slice(20)).forEach(function (addr) {
self.client.emit('peer', addr) self.client.emit('peer', addr)
}) })
break
if (timeout) {
clearTimeout(timeout)
timeout = null
}
try { socket.close() } catch (err) {}
return
case 2: // scrape case 2: // scrape
cleanup()
if (msg.length < 20) { if (msg.length < 20) {
return error('invalid scrape message') return error('invalid scrape message')
} }
self.client.emit('scrape', { self.client.emit('scrape', {
announce: self._announceUrl, announce: self._announceUrl,
complete: msg.readUInt32BE(8), complete: msg.readUInt32BE(8),
downloaded: msg.readUInt32BE(12), downloaded: msg.readUInt32BE(12),
incomplete: msg.readUInt32BE(16) incomplete: msg.readUInt32BE(16)
}) })
break
if (timeout) {
clearTimeout(timeout)
timeout = null
}
try { socket.close() } catch (err) {}
return
case 3: // error case 3: // error
cleanup()
if (msg.length < 8) { if (msg.length < 8) {
return error('invalid error message') return error('invalid error message')
} }
self.client.emit('error', new Error(msg.slice(8).toString())) self.client.emit('error', new Error(msg.slice(8).toString()))
break
if (timeout) {
clearTimeout(timeout)
timeout = null
}
try { socket.close() } catch (err) {}
return
} }
}) })
@ -289,11 +269,15 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
function error (message) { function error (message) {
self.client.emit('error', new Error(message + ' (connecting to tracker ' + requestUrl + ')')) self.client.emit('error', new Error(message + ' (connecting to tracker ' + requestUrl + ')'))
cleanup()
}
function cleanup () {
if (timeout) { if (timeout) {
clearTimeout(timeout) clearTimeout(timeout)
timeout = null timeout = null
} }
try { socket.close() } catch (err) { } try { socket.close() } catch (err) {}
} }
function genTransactionId () { function genTransactionId () {