From d70bc88f7cbb7b44c49fcfc6dc69ff1433211e71 Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 26 Mar 2019 07:10:18 -0500 Subject: [PATCH] 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) --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8d68804..c66c614 100644 --- a/index.js +++ b/index.js @@ -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; }