From 08c5457f2cfdfcbc866989948c5f45201a2889ce Mon Sep 17 00:00:00 2001 From: Parnic Date: Mon, 25 Mar 2019 20:02:58 -0500 Subject: [PATCH] Fixed handling of messages larger than 1024 bytes Fixes https://github.com/parnic/node-screenlogic/issues/5 --- CHANGELOG.md | 4 ++++ index.js | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa5b436..64eeede 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Fixed +* Messages larger than 1024 bytes are now handled properly. + ## v1.2.0 - 2019-02-22 ### Added * Remote connection through Pentair servers diff --git a/index.js b/index.js index 8ff686a..8d68804 100644 --- a/index.js +++ b/index.js @@ -124,8 +124,22 @@ class UnitConnection extends EventEmitter { this.password = password; this.client = new net.Socket(); var _this = this; + var buffer = Buffer.alloc(1024); + var bufferIdx = 0; + this.client.on('data', function(msg) { - _this.onClientMessage(msg); + if (buffer.length < msg.length + bufferIdx) { + buffer = Buffer.alloc(msg.length + buffer.length, buffer); + } + + msg.copy(buffer, bufferIdx); + bufferIdx = bufferIdx + msg.length; + + var expectedLen = msg.readInt32LE(4) + 8; + if (msg.length === expectedLen) { + _this.onClientMessage(buffer); + bufferIdx = 0; + } }).on('close', function(had_error) { // console.log('unit connection closed'); });