handle case where parsedTorrent.length is undefined

- Expose `torrentLength` so the user can set it when they know the
torrent length. (With a magnet uri, they won’t know the length at the
time the Client is instantiated)
- UDP Client: Send FFFFFFFFFFFFFFFF for ‘left’ param when we don’t know
the size. (This is what Transmission does)

Fixes #15.
This commit is contained in:
Feross Aboukhadijeh 2014-05-14 12:49:15 -07:00
parent 2983811a71
commit cc80088038

View File

@ -71,7 +71,7 @@ Tracker.prototype.complete = function (opts) {
var self = this
opts = opts || {}
opts.event = 'completed'
opts.downloaded = self._torrentLength
opts.downloaded = opts.downloaded || self.torrentLength || 0
self._request(opts)
}
@ -126,13 +126,16 @@ Tracker.prototype._request = function (opts) {
info_hash: bytewiseEncodeURIComponent(self.client._infoHash),
peer_id: bytewiseEncodeURIComponent(self.client._peerId),
port: self.client._port,
left: self.client._torrentLength - (opts.downloaded || 0),
compact: 1,
numwant: self.client._numWant,
uploaded: 0, // default, user should provide real value
downloaded: 0 // default, user should provide real value
}, opts)
if (self.client.torrentLength !== undefined) {
opts.left = self.client.torrentLength - (opts.downloaded || 0)
}
if (self._trackerId) {
opts.trackerid = self._trackerId
}
@ -269,7 +272,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
self.client._infoHash,
self.client._peerId,
toUInt64(opts.downloaded || 0),
toUInt64(opts.left || 0),
opts.left ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
toUInt64(opts.uploaded || 0),
toUInt32(EVENTS[opts.event] || 0),
toUInt32(0), // ip address (optional)
@ -392,7 +395,7 @@ function Client (peerId, port, torrent, opts) {
self._infoHash = Buffer.isBuffer(torrent.infoHash)
? torrent.infoHash
: new Buffer(torrent.infoHash, 'hex')
self._torrentLength = torrent.length
self.torrentLength = torrent.length
self._announce = torrent.announce
// optional