mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-19 04:31:36 +00:00
swarm.announce() should always call callback
It’s important that calls to swarm.announce() always calls the callback that it’s passed otherwise http announce/scrape requests will hang (because res.end() is never called). Also, since this swarm implementation is in-memory, we can get rid of the callbacks to internal functions. Lastly, fixed references to non-existent start() function.
This commit is contained in:
parent
edb5c6e854
commit
c75ca05479
43
lib/swarm.js
43
lib/swarm.js
@ -19,30 +19,27 @@ Swarm.prototype.announce = function (params, cb) {
|
|||||||
if (!params.event || params.event === 'empty') params.event = 'update'
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
||||||
var fn = '_onAnnounce_' + params.event
|
var fn = '_onAnnounce_' + params.event
|
||||||
if (self[fn]) {
|
if (self[fn]) {
|
||||||
self[fn](params, peer, function (err) {
|
self[fn](params, peer) // process event
|
||||||
// event processed, prepare response:
|
|
||||||
|
|
||||||
if (params.left === 0 && peer) peer.complete = true
|
if (params.left === 0 && peer) peer.complete = true
|
||||||
|
|
||||||
// send peers
|
cb(null, {
|
||||||
var peers = self._getPeers(params.numwant)
|
complete: self.complete,
|
||||||
|
incomplete: self.incomplete,
|
||||||
cb(null, {
|
peers: self._getPeers(params.numwant)
|
||||||
complete: self.complete,
|
|
||||||
incomplete: self.incomplete,
|
|
||||||
peers: peers
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('invalid event'))
|
cb(new Error('invalid event'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_started = function (params, peer) {
|
||||||
if (peer) {
|
if (peer) {
|
||||||
debug('unexpected `started` event from peer that is already in swarm')
|
debug('unexpected `started` event from peer that is already in swarm')
|
||||||
return this._onAnnounce_update() // treat as an update
|
return this._onAnnounce_update(params, peer) // treat as an update
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.left === 0) this.complete += 1
|
if (params.left === 0) this.complete += 1
|
||||||
else this.incomplete += 1
|
else this.incomplete += 1
|
||||||
peer = this.peers[params.addr] = {
|
peer = this.peers[params.addr] = {
|
||||||
@ -51,48 +48,42 @@ Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
|
|||||||
peerId: params.peer_id
|
peerId: params.peer_id
|
||||||
}
|
}
|
||||||
this.emit('start', params.addr)
|
this.emit('start', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_stopped = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_stopped = function (params, peer) {
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
debug('unexpected `stopped` event from peer that is not in swarm')
|
debug('unexpected `stopped` event from peer that is not in swarm')
|
||||||
return // do nothing
|
return // do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
if (peer.complete) this.complete -= 1
|
if (peer.complete) this.complete -= 1
|
||||||
else this.incomplete -= 1
|
else this.incomplete -= 1
|
||||||
this.peers[params.addr] = null
|
this.peers[params.addr] = null
|
||||||
this.emit('stop', params.addr)
|
this.emit('stop', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_completed = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_completed = function (params, peer) {
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
debug('unexpected `completed` event from peer that is not in swarm')
|
debug('unexpected `completed` event from peer that is not in swarm')
|
||||||
return start() // treat as a start
|
return this._onAnnounce_started(params, peer) // treat as a start
|
||||||
}
|
}
|
||||||
if (peer.complete) {
|
if (peer.complete) {
|
||||||
debug('unexpected `completed` event from peer that is already marked as completed')
|
debug('unexpected `completed` event from peer that is already marked as completed')
|
||||||
return // do nothing
|
return // do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
this.complete += 1
|
this.complete += 1
|
||||||
this.incomplete -= 1
|
this.incomplete -= 1
|
||||||
peer.complete = true
|
peer.complete = true
|
||||||
this.emit('complete', params.addr)
|
this.emit('complete', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._onAnnounce_update = function (params, peer, cb) {
|
Swarm.prototype._onAnnounce_update = function (params, peer) {
|
||||||
if (!peer) {
|
if (!peer) {
|
||||||
debug('unexpected `update` event from peer that is not in swarm')
|
debug('unexpected `update` event from peer that is not in swarm')
|
||||||
return start() // treat as a start
|
return this._onAnnounce_started(params, peer) // treat as a start
|
||||||
}
|
}
|
||||||
this.emit('update', params.addr)
|
this.emit('update', params.addr)
|
||||||
|
|
||||||
cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._getPeers = function (numwant) {
|
Swarm.prototype._getPeers = function (numwant) {
|
||||||
|
Loading…
Reference in New Issue
Block a user