don't send peer to itself

This commit is contained in:
Feross Aboukhadijeh 2015-12-05 00:41:56 -08:00
parent f56ca3031b
commit e6382c9af6

View File

@ -30,7 +30,7 @@ Swarm.prototype.announce = function (params, cb) {
cb(null, {
complete: self.complete,
incomplete: self.incomplete,
peers: self._getPeers(params.numwant, !!params.socket)
peers: self._getPeers(params.numwant, params.peer_id, !!params.socket)
})
}
@ -97,14 +97,16 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) {
}
}
Swarm.prototype._getPeers = function (numwant, isWebRTC) {
Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) {
var peers = []
var ite = randomIterate(Object.keys(this.peers))
var peerId
while ((peerId = ite()) && peers.length < numwant) {
var peer = this.peers[peerId]
if (!peer) continue
if ((isWebRTC && peer.socket) || (!isWebRTC && peer.ip)) peers.push(peer)
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself
if ((isWebRTC && peer.ip) || (!isWebRTC && peer.socket)) continue // send proper peer type
peers.push(peer)
}
return peers
}