Better scope handling

This commit is contained in:
Nathanaël Lécaudé 2016-11-10 17:35:44 -05:00
parent 869ae01da9
commit d853eca489
1 changed files with 33 additions and 34 deletions

View File

@ -420,7 +420,7 @@ module.exports = function(RED) {
var host = node.server || msg.host; var host = node.server || msg.host;
var port = node.port || msg.port; var port = node.port || msg.port;
node.connection_id = host + ":" + port; var connection_id = host + ":" + port;
if (!node.connected) { if (!node.connected) {
var buf; var buf;
@ -430,17 +430,16 @@ module.exports = function(RED) {
} }
else { buf = new Buffer(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully else { buf = new Buffer(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
clients[node.connection_id] = net.Socket(); clients[connection_id] = net.Socket();
if (socketTimeout !== null) { clients[node.connection_id].setTimeout(socketTimeout); } if (socketTimeout !== null) { clients[connection_id].setTimeout(socketTimeout);}
if (host && port) { if (host && port) {
var cid = node.connection_id; clients[connection_id].connect(port, host, function() {
clients[cid].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; node.connected = true;
if (clients[cid]) { if (clients[connection_id]) {
clients[cid].write(msg.payload); clients[connection_id].write(msg.payload);
} }
}); });
} }
@ -448,7 +447,7 @@ module.exports = function(RED) {
node.warn(RED._("tcpin.errors.no-host")); node.warn(RED._("tcpin.errors.no-host"));
} }
clients[node.connection_id].on('data', function(data) { clients[connection_id].on('data', function(data) {
if (node.out == "sit") { // if we are staying connected just send the buffer if (node.out == "sit") { // if we are staying connected just send the buffer
msg.payload = data; msg.payload = data;
node.send(RED.util.cloneMessage(msg)); node.send(RED.util.cloneMessage(msg));
@ -461,19 +460,19 @@ module.exports = function(RED) {
for (var j = 0; j < data.length; j++ ) { for (var j = 0; j < data.length; j++ ) {
if (node.out === "time") { if (node.out === "time") {
// do the timer thing // do the timer thing
if (clients[cid] && clients[cid].timeout) { if (clients[connection_id] && clients[connection_id].timeout) {
i += 1; i += 1;
buf[i] = data[j]; buf[i] = data[j];
} }
else { else {
clients[cid].timeout = setTimeout(function () { clients[connection_id].timeout = setTimeout(function () {
clients[cid].timeout = null; clients[connection_id].timeout = null;
msg.payload = new Buffer(i+1); msg.payload = new Buffer(i+1);
buf.copy(msg.payload,0,0,i+1); buf.copy(msg.payload,0,0,i+1);
node.send(msg); node.send(msg);
if (clients[cid]) { if (clients[connection_id]) {
node.status({}); clients[cid].destroy(); node.status({}); clients[connection_id].destroy();
delete clients[cid]; delete clients[connection_id];
} }
}, node.splitc); }, node.splitc);
i = 0; i = 0;
@ -488,9 +487,9 @@ module.exports = function(RED) {
msg.payload = new Buffer(i); msg.payload = new Buffer(i);
buf.copy(msg.payload,0,0,i); buf.copy(msg.payload,0,0,i);
node.send(msg); node.send(msg);
if (clients[cid]) { if (clients[connection_id]) {
node.status({}); clients[cid].destroy(); node.status({}); clients[connection_id].destroy();
delete clients[cid]; delete clients[connection_id];
} }
i = 0; i = 0;
} }
@ -503,9 +502,9 @@ module.exports = function(RED) {
msg.payload = new Buffer(i); msg.payload = new Buffer(i);
buf.copy(msg.payload,0,0,i); buf.copy(msg.payload,0,0,i);
node.send(msg); node.send(msg);
if (clients[cid]) { if (clients[connection_id]) {
node.status({}); clients[cid].destroy(); node.status({}); clients[connection_id].destroy();
delete clients[cid]; delete clients[connection_id];
} }
i = 0; i = 0;
} }
@ -514,36 +513,36 @@ module.exports = function(RED) {
} }
}); });
clients[node.connection_id].on('end', function() { clients[connection_id].on('end', function() {
//console.log("END"); //console.log("END");
node.connected = false; node.connected = false;
node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"}); node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"});
clients[cid] = null; clients[connection_id] = null;
}); });
clients[node.connection_id].on('close', function() { clients[connection_id].on('close', function() {
//console.log("CLOSE"); //console.log("CLOSE");
node.connected = false; node.connected = false;
if (node.done) { node.done(); } if (node.done) { node.done(); }
}); });
clients[node.connection_id].on('error', function() { clients[connection_id].on('error', function() {
//console.log("ERROR"); //console.log("ERROR");
node.connected = false; 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"),msg);
if (clients[cid]) { if (clients[connection_id]) {
clients[cid].destroy(); clients[connection_id].destroy();
delete clients[cid]; delete clients[connection_id];
} }
}); });
clients[node.connection_id].on('timeout',function() { clients[connection_id].on('timeout',function() {
node.connected = false; node.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[cid]) { if (clients[connection_id]) {
clients[cid].connect(port, host, function() { clients[connection_id].connect(port, host, function() {
node.connected = true; node.connected = true;
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
}); });
@ -551,16 +550,16 @@ module.exports = function(RED) {
}); });
} }
else { else {
clients[node.connection_id].write(msg.payload); clients[connection_id].write(msg.payload);
} }
}); });
this.on("close", function(done) { this.on("close", function(done) {
node.done = done; node.done = done;
if (clients[node.connection_id]) { for (var client in clients) {
clients[node.connection_id].destroy(); clients[client].destroy();
delete clients[node.connection_id];
} }
clients = {};
node.status({}); node.status({});
if (!node.connected) { done(); } if (!node.connected) { done(); }
}); });