Skip to content

Commit

Permalink
2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Apr 11, 2020
1 parent 4ee594c commit 18b2bfa
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 142 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ Please send me an info on devices where you have used the library successfully a

## Changelog

### v2.1.2 (2020-04-12)
* catch errors when no memory is available anymore and stop processing

### v2.1.1 (2020-03-11)
* handle stopping of process better
* TCP Transport optimization
Expand Down
8 changes: 7 additions & 1 deletion lib/transports/HttpRequestTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ HttpRequestTransport.prototype.process = function process() {
var self = this;
request({'url': this.options.transportHttpRequestUrl, 'timeout': this.options.transportHttpRequestTimeout}, function (error, response, body) {
if (!error && response && response.statusCode === 200) {
body = Buffer.from(body);
try {
body = Buffer.from(body);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}
var counter = 0;
while (body && body.length > 0 && (counter === 0 || !self.protocol.isProcessComplete())) {
if (self.protocol.checkMessage(body)) {
Expand Down
18 changes: 16 additions & 2 deletions lib/transports/SerialRequestResponseTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ SerialRequestResponseTransport.prototype.init = function init() {
return;
}
if (! self.currentData) {
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize);
try {
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}
}
if (data.length > 0) {
data.copy(self.currentData, self.currentDataOffset);
Expand Down Expand Up @@ -93,7 +99,15 @@ SerialRequestResponseTransport.prototype.init = function init() {

var addData = self.protocol.handleMessage(self.currentData.slice(0, self.currentDataOffset));
if (self.options.debug === 2) self.options.logger('LEFT AFTER HANDLE-MESSAGE ' + (addData ? addData.length : 0));
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize);

try {
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}

self.currentDataOffset = 0;
if (addData && addData.length > 0) {
addData.copy(self.currentData, 0);
Expand Down
18 changes: 16 additions & 2 deletions lib/transports/SerialResponseTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ SerialResponseTransport.prototype.init = function init() {
}

if (! self.currentData) {
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize, 0);
try {
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize, 0);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}
}
if (data.length > 0) {
if (self.options.debug === 2) self.options.logger('ADD NEW DATA (' + self.currentDataOffset + ' + NEW ' + data.length + ')');
Expand Down Expand Up @@ -140,7 +146,15 @@ SerialResponseTransport.prototype.processData = function processData() {
if (self.stopRequests || !self.serialComm) return false;
self.serialComm.pause();
var addData = self.protocol.handleMessage(self.currentData.slice(0, self.currentDataOffset));
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize, 0);

try {
self.currentData = Buffer.alloc(self.options.transportSerialMaxBufferSize, 0);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}

self.currentDataOffset = 0;
if (addData && addData.length > 0) {
addData.copy(self.currentData, 0);
Expand Down
18 changes: 16 additions & 2 deletions lib/transports/StdInTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ StdInTransport.prototype.init = function init() {
}

if (! self.currentData) {
self.currentData = Buffer.alloc(self.options.transportStdInMaxBufferSize, 0);
try {
self.currentData = Buffer.alloc(self.options.transportStdInMaxBufferSize, 0);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}
}
if (data.length > 0) {
if (self.options.debug === 2) self.options.logger('ADD NEW DATA (' + self.currentDataOffset + ' + NEW ' + data.length + ')');
Expand Down Expand Up @@ -105,7 +111,15 @@ StdInTransport.prototype.processData = function processData() {
if (self.options.debug === 2) self.options.logger('PAUSE READING STDIN TO HANDLE MESSAGE');
self.stdInStream.pause();
var addData = self.protocol.handleMessage(self.currentData.slice(0, self.currentDataOffset));
self.currentData = Buffer.alloc(self.options.transportStdInMaxBufferSize, 0);

try {
self.currentData = Buffer.alloc(self.options.transportStdInMaxBufferSize, 0);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}

self.currentDataOffset = 0;
if (addData && addData.length > 0) {
addData.copy(self.currentData, 0);
Expand Down
16 changes: 14 additions & 2 deletions lib/transports/TCPTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ TCPTransport.prototype.init = function init() {
}

if (! self.currentData) {
self.currentData = Buffer.alloc(self.options.transportTcpMaxBufferSize, 0);
try {
self.currentData = Buffer.alloc(self.options.transportTcpMaxBufferSize, 0);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}
}

if (data.length > 0) {
Expand Down Expand Up @@ -113,7 +119,13 @@ TCPTransport.prototype.processData = function processData() {
self.socket.pause();
var addData = self.protocol.handleMessage(self.currentData.slice(0, self.currentDataOffset));

self.currentData = Buffer.alloc(self.options.transportTcpMaxBufferSize, 0);
try {
self.currentData = Buffer.alloc(self.options.transportTcpMaxBufferSize, 0);
} catch (e) {
if (self.options.debug === 2) self.options.logger('CAN NOT ALLOCATE MEMORY! STOPPING');
self.stop();
return;
}

self.currentDataOffset = 0;
if (addData && addData.length > 0) {
Expand Down
Loading

0 comments on commit 18b2bfa

Please sign in to comment.