Actually fixed large message handling

We shouldn't be trying to read the message length out of the middle of a message when there are multiple segments.

Fixes https://github.com/parnic/node-screenlogic/issues/5 (for real this time, I hope...I don't have a way to test)
This commit is contained in:
2019-03-26 07:10:18 -05:00
parent 08c5457f2c
commit d70bc88f7c

View File

@ -126,17 +126,21 @@ class UnitConnection extends EventEmitter {
var _this = this;
var buffer = Buffer.alloc(1024);
var bufferIdx = 0;
var expectedMsgLen = 0;
this.client.on('data', function(msg) {
if (buffer.length < msg.length + bufferIdx) {
buffer = Buffer.alloc(msg.length + buffer.length, buffer);
}
if (bufferIdx === 0) {
expectedMsgLen = msg.readInt32LE(4) + 8;
}
msg.copy(buffer, bufferIdx);
bufferIdx = bufferIdx + msg.length;
var expectedLen = msg.readInt32LE(4) + 8;
if (msg.length === expectedLen) {
if (bufferIdx === expectedMsgLen) {
_this.onClientMessage(buffer);
bufferIdx = 0;
}