1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

TCPGet: Separated the node.connected property for each instance (#1062)

* Add additional safety checks to avoid acting on non-existent objects

* TCPGet: yet more checks

* TCPGet: seperate connected properties
This commit is contained in:
Nathanaël Lécaudé 2016-11-19 11:25:54 -05:00 committed by Dave Conway-Jones
parent 74a1713e99
commit 071a04595a

View File

@ -407,7 +407,6 @@ module.exports = function(RED) {
} // jshint ignore:line } // jshint ignore:line
} }
this.connected = false;
var node = this; var node = this;
var clients = {}; var clients = {};
@ -427,8 +426,9 @@ module.exports = function(RED) {
var connection_id = host + ":" + port; var connection_id = host + ":" + port;
clients[connection_id] = clients[connection_id] || {}; clients[connection_id] = clients[connection_id] || {};
clients[connection_id].msg = msg; clients[connection_id].msg = msg;
clients[connection_id].connected = clients[connection_id].connected || false;
if (!node.connected) { if (!clients[connection_id].connected) {
var buf; var buf;
if (this.out == "count") { if (this.out == "count") {
if (this.splitc === 0) { buf = new Buffer(1); } if (this.splitc === 0) { buf = new Buffer(1); }
@ -443,8 +443,8 @@ module.exports = function(RED) {
clients[connection_id].client.connect(port, host, function() { clients[connection_id].client.connect(port, host, function() {
//node.log(RED._("tcpin.errors.client-connected")); //node.log(RED._("tcpin.errors.client-connected"));
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
node.connected = true;
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clients[connection_id].connected = true;
clients[connection_id].client.write(clients[connection_id].msg.payload); clients[connection_id].client.write(clients[connection_id].msg.payload);
} }
}); });
@ -474,13 +474,15 @@ module.exports = function(RED) {
} }
else { else {
clients[connection_id].timeout = setTimeout(function () { clients[connection_id].timeout = setTimeout(function () {
clients[connection_id].timeout = null; if (clients[connection_id]) {
clients[connection_id].msg.payload = new Buffer(i+1); clients[connection_id].timeout = null;
buf.copy(clients[connection_id].msg.payload,0,0,i+1); clients[connection_id].msg.payload = new Buffer(i+1);
node.send(clients[connection_id].msg); buf.copy(clients[connection_id].msg.payload,0,0,i+1);
if (clients[connection_id].client) { node.send(clients[connection_id].msg);
node.status({}); clients[connection_id].client.destroy(); if (clients[connection_id].client) {
delete clients[connection_id]; node.status({}); clients[connection_id].client.destroy();
delete clients[connection_id];
}
} }
}, node.splitc); }, node.splitc);
i = 0; i = 0;
@ -527,37 +529,39 @@ module.exports = function(RED) {
clients[connection_id].client.on('end', function() { clients[connection_id].client.on('end', function() {
//console.log("END"); //console.log("END");
node.connected = false;
node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"}); node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"});
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clients[connection_id].connected = false;
clients[connection_id].client = null; clients[connection_id].client = null;
} }
}); });
clients[connection_id].client.on('close', function() { clients[connection_id].client.on('close', function() {
//console.log("CLOSE"); //console.log("CLOSE");
node.connected = false; if (clients[connection_id]) {
clients[connection_id].connected = false;
}
if (node.done) { node.done(); } if (node.done) { node.done(); }
}); });
clients[connection_id].client.on('error', function() { clients[connection_id].client.on('error', function() {
//console.log("ERROR"); //console.log("ERROR");
node.connected = false;
node.status({fill:"red",shape:"ring",text:"common.status.error"}); node.status({fill:"red",shape:"ring",text:"common.status.error"});
node.error(RED._("tcpin.errors.connect-fail"),msg); node.error(RED._("tcpin.errors.connect-fail") + " " + connection_id, msg);
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clients[connection_id].connected = false;
clients[connection_id].client.destroy(); clients[connection_id].client.destroy();
delete clients[connection_id]; delete clients[connection_id];
} }
}); });
clients[connection_id].client.on('timeout',function() { clients[connection_id].client.on('timeout',function() {
node.connected = false; clients[connection_id].connected = false;
node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"}); node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"});
//node.warn(RED._("tcpin.errors.connect-timeout")); //node.warn(RED._("tcpin.errors.connect-timeout"));
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clients[connection_id].client.connect(port, host, function() { clients[connection_id].client.connect(port, host, function() {
node.connected = true; clients[connection_id].connected = true;
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
}); });
} }
@ -577,7 +581,7 @@ module.exports = function(RED) {
} }
clients = {}; clients = {};
node.status({}); node.status({});
if (!node.connected) { done(); } done();
}); });
} }