Comparing version 0.0.5 to 0.0.6
@@ -25,2 +25,6 @@ /*jshint node: true */ | ||
}, | ||
_msgQueue: { | ||
value: [], | ||
writable: true | ||
}, | ||
handshakeReceived: { | ||
@@ -93,12 +97,27 @@ value: false, | ||
} | ||
var cursor = 0, msgLen, part; | ||
// rebuild the message from two or more packets | ||
data = Buffer.concat(this._msgQueue.concat([data])); | ||
// reset the msg queue | ||
this._msgQueue = []; | ||
// validate the lengths of the data | ||
if (!this.isValidData(data)) { | ||
// if not valid, push into queue | ||
this._msgQueue.push(data); | ||
// exit | ||
return; | ||
} | ||
// start at position 0 | ||
var cursor = 0; | ||
// array of msgs | ||
var msgs = []; | ||
do { | ||
// read the length again | ||
msgLen = data.readInt32BE(cursor); | ||
var msgLen = data.readInt32BE(cursor); | ||
// slice the data | ||
part = data.slice(cursor, cursor + msgLen + 4); | ||
var part = data.slice(cursor, cursor + msgLen + 4); | ||
@@ -110,3 +129,3 @@ // fire the event for the part | ||
cursor += msgLen + 4; | ||
} while (cursor < data.length - 4); | ||
@@ -117,2 +136,15 @@ | ||
PeerConnection.prototype.isValidData = function(data) { | ||
var cursor = 0; | ||
var lenTotal = 0; | ||
var count = 0; | ||
do { | ||
var len = data.readInt32BE(cursor); | ||
lenTotal += len; | ||
count++; | ||
cursor += len + 4; | ||
} while(cursor < data.length - 4); | ||
return ((count * 4) + lenTotal) === data.length; | ||
}; | ||
PeerConnection.prototype.fireEvent = function(part) { | ||
@@ -119,0 +151,0 @@ var msg = new Messages.KeepAlive(part); |
{ | ||
"name": "peerface", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "bittorrent peer communication library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -402,3 +402,3 @@ /*jshint node: true */ | ||
t.plan(1); | ||
peer.routeData(messages.unchoke); | ||
@@ -529,2 +529,4 @@ | ||
t.test("all the messages", function(t) { | ||
@@ -591,3 +593,5 @@ peer.once('keep-alive', function(e) { | ||
mockSocket.emit('data', bytes); | ||
// send it in 2 frames | ||
mockSocket.emit('data', bytes.slice(0, bytes.length/2)); | ||
mockSocket.emit('data', bytes.slice(bytes.length/2)); | ||
mockSocket.emit('error', "error test"); | ||
@@ -608,3 +612,3 @@ mockSocket.emit('close', "close test"); | ||
t.plan(12); | ||
t.plan(13); | ||
@@ -727,8 +731,16 @@ var defered = []; | ||
}).then(function(){ | ||
var msg = new lib.Messages.Port(messages.port); | ||
var msg = new lib.Messages.Port(messages.port).init({ | ||
ignore: 'ignored' | ||
}); | ||
remote.port(msg.listenPort); | ||
return next(); | ||
}).then(function(){ | ||
t.throws(function() { | ||
remote.write(""); | ||
}, new TypeError(), "writing a string should throw an error"); | ||
remote.close(); | ||
server.stop(); | ||
// dunno why server isn't closing | ||
@@ -743,3 +755,3 @@ server.server.unref(); | ||
test('firing error and stop', function(t) { | ||
test('testing error, stop', function(t) { | ||
@@ -746,0 +758,0 @@ var server = lib.listen(65002); |
49355
1502