Skip to content

Commit f1669a5

Browse files
author
Yoann Ciabaud
committed
Provide a way to give updated announce opts via callback on setInterval
1 parent 1246567 commit f1669a5

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function Client (peerId, port, torrent, opts) {
5353

5454
self._rtcConfig = opts.rtcConfig
5555
self._wrtc = opts.wrtc
56+
self._getAnnounceOpts = opts.getAnnounceOpts
5657

5758
debug('new client %s', self.infoHash)
5859

@@ -268,5 +269,10 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
268269
if (opts.left == null && self.torrentLength != null) {
269270
opts.left = self.torrentLength - opts.downloaded
270271
}
272+
273+
if (opts.getAnnounceOpts == null) {
274+
opts.getAnnounceOpts = self._getAnnounceOpts
275+
}
276+
271277
return opts
272278
}

lib/client/http-tracker.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,26 @@ HTTPTracker.prototype.announce = function (opts) {
4040
var self = this
4141
if (self.destroyed) return
4242

43+
// Refresh opts if the callback is provided
44+
var cbopts
45+
if (opts.getAnnounceOpts) {
46+
cbopts = opts.getAnnounceOpts()
47+
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
48+
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
49+
if (cbopts.left) opts.left = cbopts.left
50+
}
51+
4352
var params = {
4453
numwant: opts.numwant,
4554
uploaded: opts.uploaded,
4655
downloaded: opts.downloaded,
56+
left: opts.left,
4757
event: opts.event,
4858
compact: (opts.compact == null) ? 1 : opts.compact,
4959
info_hash: self.client._infoHashBinary,
5060
peer_id: self.client._peerIdBinary,
51-
port: self.client._port
61+
port: self.client._port,
62+
extras: cbopts && cbopts.extraAnnounceOpts
5263
}
5364
if (self._trackerId) params.trackerid = self._trackerId
5465

lib/client/udp-tracker.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ UDPTracker.prototype._request = function (opts) {
189189
function announce (connectionId, opts) {
190190
transactionId = genTransactionId()
191191

192+
// Refresh opts if the callback is provided
193+
var cbopts
194+
if (opts.getAnnounceOpts) {
195+
cbopts = opts.getAnnounceOpts()
196+
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
197+
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
198+
if (cbopts.left) opts.left = cbopts.left
199+
}
200+
192201
send(Buffer.concat([
193202
connectionId,
194203
common.toUInt32(common.ACTIONS.ANNOUNCE),

lib/client/websocket-tracker.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,26 @@ WebSocketTracker.prototype.announce = function (opts) {
4646
// Limit the number of offers that are generated, since it can be slow
4747
var numwant = Math.min(opts.numwant, 10)
4848

49+
// Refresh opts if the callback is provided
50+
var cbopts
51+
if (opts.getAnnounceOpts) {
52+
cbopts = opts.getAnnounceOpts()
53+
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
54+
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
55+
if (cbopts.left) opts.left = cbopts.left
56+
}
57+
4958
self._generateOffers(numwant, function (offers) {
5059
var params = {
5160
numwant: numwant,
5261
uploaded: opts.uploaded || 0,
5362
downloaded: opts.downloaded,
63+
left: opts.left,
5464
event: opts.event,
5565
info_hash: self.client._infoHashBinary,
5666
peer_id: self.client._peerIdBinary,
57-
offers: offers
67+
offers: offers,
68+
extras: cbopts && cbopts.extraAnnounceOpts
5869
}
5970
if (self._trackerId) params.trackerid = self._trackerId
6071

0 commit comments

Comments
 (0)