bittorrent-protocol
Advanced tools
Comparing version 0.3.0 to 0.3.1
78
index.js
@@ -45,6 +45,8 @@ module.exports = Wire | ||
self.choking = true | ||
self.interested = false | ||
self.peerChoking = true | ||
self.peerInterested = false | ||
self.amChoking = true // are we choking the peer? | ||
self.amInterested = false // are we interested in the peer? | ||
self.peerChoking = true // is the peer choking us? | ||
self.peerInterested = false // is the peer interested in us? | ||
self.peerPieces = [] | ||
@@ -144,2 +146,13 @@ self.peerExtensions = {} | ||
// | ||
// MESSAGES | ||
// | ||
/** | ||
* Message: "handshake" <pstrlen><pstr><reserved><info_hash><peer_id> | ||
* @param {Buffer|string} infoHash | ||
* @param {Buffer|string} peerId | ||
* @param {Object} extensions | ||
*/ | ||
Wire.prototype.handshake = function (infoHash, peerId, extensions) { | ||
@@ -161,5 +174,4 @@ if (typeof infoHash === 'string') | ||
Wire.prototype.choke = function () { | ||
if (this.choking) return | ||
this.choking = true | ||
if (this.amChoking) return | ||
this.amChoking = true | ||
while (this.peerRequests.length) this.peerRequests.pop() | ||
@@ -170,4 +182,4 @@ this._push(MESSAGE_CHOKE) | ||
Wire.prototype.unchoke = function () { | ||
if (!this.choking) return | ||
this.choking = false | ||
if (!this.amChoking) return | ||
this.amChoking = false | ||
this._push(MESSAGE_UNCHOKE) | ||
@@ -177,4 +189,4 @@ } | ||
Wire.prototype.interested = function () { | ||
if (this.interested) return | ||
this.interested = true | ||
if (this.amInterested) return | ||
this.amInterested = true | ||
this._push(MESSAGE_INTERESTED) | ||
@@ -184,4 +196,4 @@ } | ||
Wire.prototype.uninterested = function () { | ||
if (!this.interested) return | ||
this.interested = false | ||
if (!this.amInterested) return | ||
this.amInterested = false | ||
this._push(MESSAGE_UNINTERESTED) | ||
@@ -219,8 +231,7 @@ } | ||
Wire.prototype.extended = function (ext_number, msg) { | ||
var ext_id = new Buffer(1) | ||
ext_id.writeUInt8(ext_number, 0) | ||
this._message(20, [], Buffer.concat([ext_id, bncode.encode(msg)])) | ||
} | ||
/** | ||
* Message: "port" <len=0003><id=9><listen-port> | ||
* @param {Number} port | ||
*/ | ||
Wire.prototype.port = function (port) { | ||
@@ -232,2 +243,10 @@ var message = new Buffer(MESSAGE_PORT) | ||
Wire.prototype.extended = function (ext_number, msg) { | ||
var ext_id = new Buffer(1) | ||
ext_id.writeUInt8(ext_number, 0) | ||
this._message(20, [], Buffer.concat([ext_id, bncode.encode(msg)])) | ||
} | ||
Wire.prototype.setKeepAlive = function (bool) { | ||
@@ -294,8 +313,9 @@ clearInterval(this._keepAlive) | ||
Wire.prototype._onrequest = function (i, offset, length) { | ||
if (this.choking) return | ||
if (this.amChoking) return | ||
var respond = function (err, buffer) { | ||
if (err || request !== pull(this.peerRequests, i, offset, length)) return | ||
if (err || request !== pull(this.peerRequests, i, offset, length)) | ||
return | ||
this.piece(i, offset, buffer) | ||
} | ||
}.bind(this) | ||
@@ -358,5 +378,9 @@ var request = new Request(i, offset, length, respond) | ||
/** | ||
* Push a message to the remote peer. | ||
* @param {Buffer} data | ||
*/ | ||
Wire.prototype._push = function (data) { | ||
if (this._finished) return | ||
this.push(data) | ||
return this.push(data) | ||
} | ||
@@ -375,3 +399,3 @@ | ||
*/ | ||
Wire.prototype._write = function (data, encoding, cb) { | ||
Wire.prototype._write = function (data, encoding, callback) { | ||
this._bufferSize += data.length | ||
@@ -391,10 +415,10 @@ this._buffer.push(data) | ||
cb(null) // Signal that we're ready for more data | ||
callback(null) // Signal that we're ready for more data | ||
} | ||
/** | ||
* Duplex stream method. Called whenever the downstream wants data. | ||
* No-op since we'll just push data whenever we get it and extra data will be | ||
* buffered in memory. | ||
* Duplex stream method. Called whenever the downstream wants data. No-op | ||
* since we'll just push data whenever we get it. Extra data will be buffered | ||
* in memory (we don't want to apply backpressure to peers!). | ||
*/ | ||
Wire.prototype._read = function () {} |
{ | ||
"name": "bittorrent-protocol", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Simple, robust, BitTorrent peer wire protocol implementation", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"bitfield": "~0.1.0", | ||
"bncode": "~0.3.0" | ||
"bncode": "~0.5.0" | ||
}, | ||
@@ -11,0 +11,0 @@ "devDependencies": { |
@@ -12,3 +12,2 @@ var Protocol = require('../') | ||
wire.on('unchoke', function () { | ||
@@ -15,0 +14,0 @@ var requests = 0 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22706
498
+ Addedbncode@0.5.3(transitive)
- Removedbase64-js@0.0.2(transitive)
- Removedbncode@0.3.0(transitive)
- Removedbops@0.1.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedto-utf8@0.0.1(transitive)
Updatedbncode@~0.5.0