Fixed handling of messages larger than 1024 bytes

Fixes https://github.com/parnic/node-screenlogic/issues/5
This commit is contained in:
2019-03-25 20:02:58 -05:00
parent 398a687cc6
commit 08c5457f2c
2 changed files with 19 additions and 1 deletions

View File

@ -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

View File

@ -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');
});