cache infohash and peerid

This commit is contained in:
Feross Aboukhadijeh 2015-05-20 06:45:59 -07:00
parent bb145cce14
commit c1bbade6d7
3 changed files with 13 additions and 9 deletions

View File

@ -37,12 +37,16 @@ function Client (peerId, port, torrent, opts) {
self._peerId = Buffer.isBuffer(peerId)
? peerId
: new Buffer(peerId, 'hex')
self._port = port
self._peerIdHex = self._peerId.toString('hex')
self._peerIdBinary = self._peerId.toString('binary')
self._infoHash = Buffer.isBuffer(torrent.infoHash)
? torrent.infoHash
: new Buffer(torrent.infoHash, 'hex')
self._infoHashHex = self._infoHash.toString('hex')
self._infoHashBinary = self._infoHash.toString('binary')
self._port = port
self.torrentLength = torrent.length
self._rtcConfig = opts.rtcConfig
@ -52,7 +56,7 @@ function Client (peerId, port, torrent, opts) {
self._numWant = opts.numWant || common.DEFAULT_ANNOUNCE_PEERS
self._intervalMs = opts.interval || common.DEFAULT_ANNOUNCE_INTERVAL
debug('new client %s', self._infoHash.toString('hex'))
debug('new client %s', self._infoHashHex)
var trackerOpts = { interval: self._intervalMs }
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'

View File

@ -49,8 +49,8 @@ HTTPTracker.prototype.announce = function (opts) {
if (opts.compact == null) opts.compact = 1
if (opts.numwant == null) opts.numwant = self.client._numWant // spec says 'numwant'
opts.info_hash = self.client._infoHash.toString('binary')
opts.peer_id = self.client._peerId.toString('binary')
opts.info_hash = self.client._infoHashBinary
opts.peer_id = self.client._peerIdBinary
opts.port = self.client._port
self._request(self._announceUrl, opts, self._onAnnounceResponse.bind(self))

View File

@ -40,8 +40,8 @@ WebSocketTracker.prototype.announce = function (opts) {
return self._socket.once('connect', self.announce.bind(self, opts))
}
opts.info_hash = self.client._infoHash.toString('binary')
opts.peer_id = self.client._peerId.toString('binary')
opts.info_hash = self.client._infoHashBinary
opts.peer_id = self.client._peerIdBinary
// Limit number of offers (temporarily)
// TODO: remove this when we cleanup old RTCPeerConnections cleanly
@ -112,7 +112,7 @@ WebSocketTracker.prototype._onSocketData = function (data) {
return self.client.emit('warning', new Error('Invalid tracker response'))
}
if (data.info_hash !== self.client._infoHash.toString('binary')) {
if (data.info_hash !== self.client._infoHashBinary) {
return self.client.emit('warning', new Error('Invalid tracker response'))
}
@ -160,8 +160,8 @@ WebSocketTracker.prototype._onSocketData = function (data) {
peer.id = common.binaryToHex(data.peer_id)
peer.once('signal', function (answer) {
var params = {
info_hash: self.client._infoHash.toString('binary'),
peer_id: self.client._peerId.toString('binary'),
info_hash: self.client._infoHashBinary,
peer_id: self.client._peerIdBinary,
to_peer_id: data.peer_id,
answer: answer,
offer_id: data.offer_id