From c1bbade6d73cc5e035901463d0d09f51a57ba9a6 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 20 May 2015 06:45:59 -0700 Subject: [PATCH] cache infohash and peerid --- client.js | 8 ++++++-- lib/http-tracker.js | 4 ++-- lib/websocket-tracker.js | 10 +++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/client.js b/client.js index daf3224..7d4e004 100644 --- a/client.js +++ b/client.js @@ -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' diff --git a/lib/http-tracker.js b/lib/http-tracker.js index 665a2af..8d221a9 100644 --- a/lib/http-tracker.js +++ b/lib/http-tracker.js @@ -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)) diff --git a/lib/websocket-tracker.js b/lib/websocket-tracker.js index f2d98aa..59a8f94 100644 --- a/lib/websocket-tracker.js +++ b/lib/websocket-tracker.js @@ -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