Skip to content

Commit

Permalink
WIP: queue next chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 6, 2016
1 parent b85410f commit 1e4fa73
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/protocol/Parser.js
Expand Up @@ -9,6 +9,7 @@ function Parser(options) {

this._supportBigNumbers = options.config && options.config.supportBigNumbers;
this._buffer = new Buffer(0);
this._nextBuffers = [];
this._longPacketBuffers = [];
this._offset = 0;
this._packetEnd = null;
Expand All @@ -21,12 +22,12 @@ function Parser(options) {
this._paused = false;
}

Parser.prototype.write = function(buffer) {
this.append(buffer);
Parser.prototype.write = function write(chunk) {
this._nextBuffers.push(chunk);

while (!this._paused) {
if (!this._packetHeader) {
if (this._bytesRemaining() < 4) {
if (!this._combineNextBuffers(4)) {
break;
}

Expand All @@ -50,7 +51,7 @@ Parser.prototype.write = function(buffer) {
this.incrementPacketNumber();
}

if (this._bytesRemaining() < this._packetHeader.length) {
if (!this._combineNextBuffers(this._packetHeader.length)) {
break;
}

Expand Down Expand Up @@ -141,7 +142,7 @@ Parser.prototype.peak = function() {
return this._buffer[this._offset];
};

Parser.prototype.parseUnsignedNumber = function(bytes) {
Parser.prototype.parseUnsignedNumber = function parseUnsignedNumber(bytes) {
if (bytes === 1) {
return this._buffer[this._offset++];
}
Expand Down Expand Up @@ -362,10 +363,6 @@ Parser.prototype.reachedPacketEnd = function() {
return this._offset === this._packetEnd;
};

Parser.prototype._bytesRemaining = function() {
return this._buffer.length - this._offset;
};

Parser.prototype.incrementPacketNumber = function() {
var currentPacketNumber = this._nextPacketNumber;
this._nextPacketNumber = (this._nextPacketNumber + 1) % 256;
Expand All @@ -383,7 +380,23 @@ Parser.prototype.packetLength = function() {
}, this._packetHeader.length);
};

Parser.prototype._combineLongPacketBuffers = function() {
Parser.prototype._combineNextBuffers = function _combineNextBuffers(bytes) {
if ((this._buffer.length - this._offset) >= bytes) {
return true;
}

if (!this._nextBuffers.length) {
return false;
}

while (this._nextBuffers.length && (this._buffer.length - this._offset) < bytes) {
this.append(this._nextBuffers.shift());
}

return (this._buffer.length - this._offset) >= bytes;
};

Parser.prototype._combineLongPacketBuffers = function _combineLongPacketBuffers() {
if (!this._longPacketBuffers.length) {
return;
}
Expand All @@ -392,7 +405,7 @@ Parser.prototype._combineLongPacketBuffers = function() {

var length = this._longPacketBuffers.reduce(function(length, buffer) {
return length + buffer.length;
}, this._bytesRemaining());
}, (this._buffer.length - this._offset));

var combinedBuffer = new Buffer(length);

Expand Down

0 comments on commit 1e4fa73

Please sign in to comment.