refactor parsers.

This commit is contained in:
Christopher Jeffrey 2016-07-27 04:10:08 -07:00
parent 6a2615d3cc
commit 0fa972dcb3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 16 additions and 16 deletions

View File

@ -79,7 +79,7 @@ function BIP151Stream(cipher, key) {
this.lastRekey = 0;
this.pending = [];
this.pendingTotal = 0;
this.total = 0;
this.waiting = 0;
}
@ -277,15 +277,15 @@ BIP151Stream.prototype.feed = function feed(data) {
while (data.length) {
if (!this.waiting) {
this.pendingTotal += data.length;
this.total += data.length;
this.pending.push(data);
if (this.pendingTotal < 4)
if (this.total < 4)
break;
chunk = concat(this.pending);
this.pendingTotal = 0;
this.total = 0;
this.pending.length = 0;
size = this.decryptSize(chunk);
@ -307,10 +307,10 @@ BIP151Stream.prototype.feed = function feed(data) {
continue;
}
this.pendingTotal += data.length;
this.total += data.length;
this.pending.push(data);
if (this.pendingTotal < this.waiting)
if (this.total < this.waiting)
break;
chunk = concat(this.pending);
@ -318,7 +318,7 @@ BIP151Stream.prototype.feed = function feed(data) {
tag = chunk.slice(this.waiting - 16, this.waiting);
data = chunk.slice(this.waiting);
this.pendingTotal = 0;
this.total = 0;
this.pending.length = 0;
this.waiting = 0;

View File

@ -36,7 +36,7 @@ function Parser(options) {
this.bip151 = options.bip151;
this.pending = [];
this.pendingTotal = 0;
this.total = 0;
this.waiting = 24;
this.packet = null;
@ -89,10 +89,10 @@ Parser.prototype.feed = function feed(data) {
if (this.bip151 && this.bip151.handshake)
return this.bip151.feed(data);
this.pendingTotal += data.length;
this.total += data.length;
this.pending.push(data);
while (this.pendingTotal >= this.waiting) {
while (this.total >= this.waiting) {
// Concat chunks
chunk = new Buffer(this.waiting);
@ -111,7 +111,7 @@ Parser.prototype.feed = function feed(data) {
assert.equal(off, chunk.length);
// Slice buffers
this.pendingTotal -= chunk.length;
this.total -= chunk.length;
this.parse(chunk);
}
};

View File

@ -945,7 +945,7 @@ function Parser() {
this.waiting = 12;
this.header = null;
this.pending = [];
this.pendingTotal = 0;
this.total = 0;
}
utils.inherits(Parser, EventEmitter);
@ -954,11 +954,11 @@ Parser.prototype.feed = function feed(data) {
var chunk, header, body;
while (data) {
this.pendingTotal += data.length;
this.total += data.length;
this.pending.push(data);
data = null;
if (this.pendingTotal < this.waiting)
if (this.total < this.waiting)
break;
chunk = concat(this.pending);
@ -972,7 +972,7 @@ Parser.prototype.feed = function feed(data) {
this.header = this.parseHeader(chunk);
this.waiting = this.header.size;
this.pending.length = 0;
this.pendingTotal = 0;
this.total = 0;
if (this.header.magic !== 0xdeadbeef) {
this.header = null;
@ -987,7 +987,7 @@ Parser.prototype.feed = function feed(data) {
header = this.header;
this.pending.length = 0;
this.pendingTotal = 0;
this.total = 0;
this.waiting = 12;
this.header = null;