mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-02-07 13:59:07 +00:00
client: destroy callback isn't called until after cleanup
This commit is contained in:
parent
82aea33dfa
commit
5b79d42dcd
16
client.js
16
client.js
@ -4,6 +4,7 @@ var debug = require('debug')('bittorrent-tracker')
|
|||||||
var EventEmitter = require('events').EventEmitter
|
var EventEmitter = require('events').EventEmitter
|
||||||
var inherits = require('inherits')
|
var inherits = require('inherits')
|
||||||
var once = require('once')
|
var once = require('once')
|
||||||
|
var parallel = require('run-parallel')
|
||||||
var url = require('url')
|
var url = require('url')
|
||||||
|
|
||||||
var common = require('./lib/common')
|
var common = require('./lib/common')
|
||||||
@ -46,8 +47,10 @@ function Client (peerId, port, torrent, opts) {
|
|||||||
self._infoHashHex = self._infoHash.toString('hex')
|
self._infoHashHex = self._infoHash.toString('hex')
|
||||||
self._infoHashBinary = self._infoHash.toString('binary')
|
self._infoHashBinary = self._infoHash.toString('binary')
|
||||||
|
|
||||||
self._port = port
|
|
||||||
self.torrentLength = torrent.length
|
self.torrentLength = torrent.length
|
||||||
|
self.destroyed = false
|
||||||
|
|
||||||
|
self._port = port
|
||||||
|
|
||||||
self._rtcConfig = opts.rtcConfig
|
self._rtcConfig = opts.rtcConfig
|
||||||
self._wrtc = opts.wrtc
|
self._wrtc = opts.wrtc
|
||||||
@ -236,14 +239,19 @@ Client.prototype.setInterval = function (intervalMs) {
|
|||||||
|
|
||||||
Client.prototype.destroy = function (cb) {
|
Client.prototype.destroy = function (cb) {
|
||||||
var self = this
|
var self = this
|
||||||
|
if (self.destroyed) return
|
||||||
|
self.destroyed = true
|
||||||
debug('destroy')
|
debug('destroy')
|
||||||
|
|
||||||
self._trackers.forEach(function (tracker) {
|
var tasks = self._trackers.map(function (tracker) {
|
||||||
tracker.destroy()
|
return function (cb) {
|
||||||
|
tracker.destroy(cb)
|
||||||
tracker.setInterval(0) // stop announcing on intervals
|
tracker.setInterval(0) // stop announcing on intervals
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
parallel(tasks, cb)
|
||||||
self._trackers = []
|
self._trackers = []
|
||||||
if (cb) process.nextTick(function () { cb(null) })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Client.prototype._defaultAnnounceOpts = function (opts) {
|
Client.prototype._defaultAnnounceOpts = function (opts) {
|
||||||
|
@ -88,9 +88,11 @@ HTTPTracker.prototype.setInterval = function (intervalMs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPTracker.prototype.destroy = function () {
|
HTTPTracker.prototype.destroy = function (cb) {
|
||||||
var self = this
|
var self = this
|
||||||
|
if (self.destroyed) return
|
||||||
self.destroyed = true
|
self.destroyed = true
|
||||||
|
cb(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPTracker.prototype._request = function (requestUrl, opts, cb) {
|
HTTPTracker.prototype._request = function (requestUrl, opts, cb) {
|
||||||
|
@ -63,7 +63,7 @@ UDPTracker.prototype.setInterval = function (intervalMs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UDPTracker.prototype.destroy = function () {
|
UDPTracker.prototype.destroy = function (cb) {
|
||||||
var self = this
|
var self = this
|
||||||
if (self.destroyed) return
|
if (self.destroyed) return
|
||||||
self.destroyed = true
|
self.destroyed = true
|
||||||
@ -72,6 +72,7 @@ UDPTracker.prototype.destroy = function () {
|
|||||||
cleanup()
|
cleanup()
|
||||||
})
|
})
|
||||||
self._cleanupFns = []
|
self._cleanupFns = []
|
||||||
|
cb(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
UDPTracker.prototype._request = function (opts) {
|
UDPTracker.prototype._request = function (opts) {
|
||||||
|
@ -87,7 +87,7 @@ WebSocketTracker.prototype.setInterval = function (intervalMs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketTracker.prototype.destroy = function () {
|
WebSocketTracker.prototype.destroy = function (onclose) {
|
||||||
var self = this
|
var self = this
|
||||||
if (self.destroyed) return
|
if (self.destroyed) return
|
||||||
self.destroyed = true
|
self.destroyed = true
|
||||||
@ -95,12 +95,18 @@ WebSocketTracker.prototype.destroy = function () {
|
|||||||
self._socket.removeListener('data', self._onSocketDataBound)
|
self._socket.removeListener('data', self._onSocketDataBound)
|
||||||
self._socket.removeListener('close', self._onSocketCloseBound)
|
self._socket.removeListener('close', self._onSocketCloseBound)
|
||||||
self._socket.removeListener('error', self._onSocketErrorBound)
|
self._socket.removeListener('error', self._onSocketErrorBound)
|
||||||
|
|
||||||
self._onSocketErrorBound = null
|
self._onSocketErrorBound = null
|
||||||
self._onSocketDataBound = null
|
self._onSocketDataBound = null
|
||||||
self._onSocketCloseBound = null
|
self._onSocketCloseBound = null
|
||||||
|
|
||||||
self._socket.on('error', noop) // ignore all future errors
|
self._socket.on('error', noop) // ignore all future errors
|
||||||
try { self._socket.close() } catch (e) {}
|
try {
|
||||||
|
self._socket.destroy(onclose)
|
||||||
|
} catch (err) {
|
||||||
|
if (onclose) onclose()
|
||||||
|
}
|
||||||
|
|
||||||
self._socket = null
|
self._socket = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
"minimist": "^1.1.1",
|
"minimist": "^1.1.1",
|
||||||
"once": "^1.3.0",
|
"once": "^1.3.0",
|
||||||
"random-iterate": "^1.0.1",
|
"random-iterate": "^1.0.1",
|
||||||
|
"run-parallel": "^1.1.2",
|
||||||
"run-series": "^1.0.2",
|
"run-series": "^1.0.2",
|
||||||
"simple-get": "^1.3.0",
|
"simple-get": "^1.3.0",
|
||||||
"simple-peer": "^5.0.0",
|
"simple-peer": "^5.0.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user