parser: fix slicing

This commit is contained in:
Fedor Indutny 2014-04-28 19:57:40 +04:00
parent b6a572f0c0
commit 106d243873
2 changed files with 12 additions and 9 deletions

View File

@ -52,9 +52,12 @@ Framer.prototype._addr = function addr(buf, off) {
buf[off + 25] = 0;
};
Framer.prototype.version = function version() {
Framer.prototype.version = function version(packet) {
var p = new Array(86);
if (!packet)
packet = {};
// Version
writeU32(p, constants.version, 0);
@ -79,10 +82,10 @@ Framer.prototype.version = function version() {
p[80] = 0;
// Start height
writeU32(p, 0x0, 81);
writeU32(p, packet.height, 81);
// Relay
p[85] = 0;
p[85] = packet.relay ? 1 : 0;
return this.packet('version', p);
};

View File

@ -32,17 +32,17 @@ Parser.prototype.execute = function(data) {
// Concat chunks
var chunk = new Array(this.waiting);
for (var i = 0, off = 0, len = 0; off < chunk.length; i++) {
len = utils.copy(this.pending[i], chunk, off);
len = utils.copy(this.pending[0], chunk, off);
if (len === this.pending[0].length)
this.pending.shift();
else
this.pending[0] = this.pending[0].slice(len);
off += len;
}
assert.equal(off, chunk.length);
// Slice buffers
this.pending = this.pending.slice();
this.pendingTotal -= chunk.length;
if (this.pending.length && len !== this.pending[0].length)
this.pending[0] = this.pending[0].slice(len);
this.parse(chunk);
}
};
@ -89,7 +89,7 @@ Parser.prototype.parsePayload = function parsePayload(cmd, p) {
if (cmd === 'version')
return this.parseVersion(p);
else
return this.emit('error', new Error('Unknown packet: ' + cmd));
return p;
};
Parser.prototype.parseVersion = function parseVersion(p) {