Skip to content

Commit c1bbade

Browse files
committed
cache infohash and peerid
1 parent bb145cc commit c1bbade

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

client.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ function Client (peerId, port, torrent, opts) {
3737
self._peerId = Buffer.isBuffer(peerId)
3838
? peerId
3939
: new Buffer(peerId, 'hex')
40-
self._port = port
4140
self._peerIdHex = self._peerId.toString('hex')
41+
self._peerIdBinary = self._peerId.toString('binary')
4242

4343
self._infoHash = Buffer.isBuffer(torrent.infoHash)
4444
? torrent.infoHash
4545
: new Buffer(torrent.infoHash, 'hex')
46+
self._infoHashHex = self._infoHash.toString('hex')
47+
self._infoHashBinary = self._infoHash.toString('binary')
48+
49+
self._port = port
4650
self.torrentLength = torrent.length
4751

4852
self._rtcConfig = opts.rtcConfig
@@ -52,7 +56,7 @@ function Client (peerId, port, torrent, opts) {
5256
self._numWant = opts.numWant || common.DEFAULT_ANNOUNCE_PEERS
5357
self._intervalMs = opts.interval || common.DEFAULT_ANNOUNCE_INTERVAL
5458

55-
debug('new client %s', self._infoHash.toString('hex'))
59+
debug('new client %s', self._infoHashHex)
5660

5761
var trackerOpts = { interval: self._intervalMs }
5862
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'

lib/http-tracker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ HTTPTracker.prototype.announce = function (opts) {
4949
if (opts.compact == null) opts.compact = 1
5050
if (opts.numwant == null) opts.numwant = self.client._numWant // spec says 'numwant'
5151

52-
opts.info_hash = self.client._infoHash.toString('binary')
53-
opts.peer_id = self.client._peerId.toString('binary')
52+
opts.info_hash = self.client._infoHashBinary
53+
opts.peer_id = self.client._peerIdBinary
5454
opts.port = self.client._port
5555

5656
self._request(self._announceUrl, opts, self._onAnnounceResponse.bind(self))

lib/websocket-tracker.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ WebSocketTracker.prototype.announce = function (opts) {
4040
return self._socket.once('connect', self.announce.bind(self, opts))
4141
}
4242

43-
opts.info_hash = self.client._infoHash.toString('binary')
44-
opts.peer_id = self.client._peerId.toString('binary')
43+
opts.info_hash = self.client._infoHashBinary
44+
opts.peer_id = self.client._peerIdBinary
4545

4646
// Limit number of offers (temporarily)
4747
// TODO: remove this when we cleanup old RTCPeerConnections cleanly
@@ -112,7 +112,7 @@ WebSocketTracker.prototype._onSocketData = function (data) {
112112
return self.client.emit('warning', new Error('Invalid tracker response'))
113113
}
114114

115-
if (data.info_hash !== self.client._infoHash.toString('binary')) {
115+
if (data.info_hash !== self.client._infoHashBinary) {
116116
return self.client.emit('warning', new Error('Invalid tracker response'))
117117
}
118118

@@ -160,8 +160,8 @@ WebSocketTracker.prototype._onSocketData = function (data) {
160160
peer.id = common.binaryToHex(data.peer_id)
161161
peer.once('signal', function (answer) {
162162
var params = {
163-
info_hash: self.client._infoHash.toString('binary'),
164-
peer_id: self.client._peerId.toString('binary'),
163+
info_hash: self.client._infoHashBinary,
164+
peer_id: self.client._peerIdBinary,
165165
to_peer_id: data.peer_id,
166166
answer: answer,
167167
offer_id: data.offer_id

0 commit comments

Comments
 (0)