From a07188f87a4acb7f8b2338c3d41da1b57cb9878c Mon Sep 17 00:00:00 2001 From: KayleePop <34007889+KayleePop@users.noreply.github.com> Date: Mon, 25 May 2020 17:52:12 -0500 Subject: fix: not setting initial wire interest https://github.com/webtorrent/webtorrent/issues/1864 --- lib/torrent.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/torrent.js b/lib/torrent.js index 1a765a1..4ac71a0 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -1084,7 +1084,7 @@ class Torrent extends EventEmitter { }) wire.bitfield(this.bitfield) // always send bitfield (required) - wire.uninterested() // always start out uninterested (as per protocol) + this._updateWireInterest(wire) // set inital interest // Send PORT message to peers that support DHT if (wire.peerExtensions.dht && this.client.dht && this.client.dht.listening) { @@ -1147,24 +1147,26 @@ class Torrent extends EventEmitter { const prev = this._amInterested this._amInterested = !!this._selections.length - this.wires.forEach(wire => { - let interested = false - for (let index = 0; index < this.pieces.length; ++index) { - if (this.pieces[index] && wire.peerPieces.get(index)) { - interested = true - break - } - } - - if (interested) wire.interested() - else wire.uninterested() - }) + this.wires.forEach(wire => this._updateWireInterest(wire)) if (prev === this._amInterested) return if (this._amInterested) this.emit('interested') else this.emit('uninterested') } + _updateWireInterest (wire) { + let interested = false + for (let index = 0; index < this.pieces.length; ++index) { + if (this.pieces[index] && wire.peerPieces.get(index)) { + interested = true + break + } + } + + if (interested) wire.interested() + else wire.uninterested() + } + /** * Heartbeat to update all peers and their requests. */ -- cgit v1.2.3