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

TCPget can now handle concurrent sessions (#1042)

* First release of multi connection tcpget

* Works when connection is left open

* Change scope of clients object

* Fix comparison to "" in tcpin

* Add security checks

* Better scope handling
This commit is contained in:
Nathanaël Lécaudé 2016-11-11 17:01:22 +08:00 committed by Dave Conway-Jones
parent 9cc1b03c56
commit 7ef153756b

View File

@ -83,7 +83,7 @@ module.exports = function(RED) {
} }
}); });
client.on('end', function() { client.on('end', function() {
if (!node.stream || (node.datatype == "utf8" && node.newline != "" && buffer.length > 0)) { if (!node.stream || (node.datatype == "utf8" && node.newline !== "" && buffer.length > 0)) {
var msg = {topic:node.topic, payload:buffer}; var msg = {topic:node.topic, payload:buffer};
msg._session = {type:"tcp",id:id}; msg._session = {type:"tcp",id:id};
if (buffer.length !== 0) { if (buffer.length !== 0) {
@ -407,66 +407,73 @@ module.exports = function(RED) {
} // jshint ignore:line } // jshint ignore:line
} }
var buf;
if (this.out == "count") {
if (this.splitc === 0) { buf = new Buffer(1); }
else { buf = new Buffer(this.splitc); }
}
else { buf = new Buffer(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
this.connected = false; this.connected = false;
var node = this; var node = this;
var client;
var m; var clients = {};
this.on("input", function(msg) { this.on("input", function(msg) {
m = msg;
var i = 0; var i = 0;
if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) { if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) {
msg.payload = msg.payload.toString(); msg.payload = msg.payload.toString();
} }
var host = node.server || msg.host;
var port = node.port || msg.port;
var connection_id = host + ":" + port;
if (!node.connected) { if (!node.connected) {
client = net.Socket(); var buf;
if (socketTimeout !== null) { client.setTimeout(socketTimeout); } if (this.out == "count") {
var host = node.server || msg.host; if (this.splitc === 0) { buf = new Buffer(1); }
var port = node.port || msg.port; else { buf = new Buffer(this.splitc); }
}
else { buf = new Buffer(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
clients[connection_id] = net.Socket();
if (socketTimeout !== null) { clients[connection_id].setTimeout(socketTimeout);}
if (host && port) { if (host && port) {
client.connect(port, host, function() { clients[connection_id].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;
client.write(msg.payload); if (clients[connection_id]) {
clients[connection_id].write(msg.payload);
}
}); });
} }
else { else {
node.warn(RED._("tcpin.errors.no-host")); node.warn(RED._("tcpin.errors.no-host"));
} }
client.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
m.payload = data; msg.payload = data;
node.send(m); node.send(RED.util.cloneMessage(msg));
} }
else if (node.splitc === 0) { else if (node.splitc === 0) {
msg.payload = data; msg.payload = data;
node.send(msg); node.send(RED.util.cloneMessage(msg));
} }
else { else {
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 (node.tout) { if (clients[connection_id] && clients[connection_id].timeout) {
i += 1; i += 1;
buf[i] = data[j]; buf[i] = data[j];
} }
else { else {
node.tout = setTimeout(function () { clients[connection_id].timeout = setTimeout(function () {
node.tout = 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 (client) { node.status({}); client.destroy(); } if (clients[connection_id]) {
node.status({}); clients[connection_id].destroy();
delete clients[connection_id];
}
}, node.splitc); }, node.splitc);
i = 0; i = 0;
buf[0] = data[j]; buf[0] = data[j];
@ -480,7 +487,10 @@ 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 (client) { node.status({}); client.destroy(); } if (clients[connection_id]) {
node.status({}); clients[connection_id].destroy();
delete clients[connection_id];
}
i = 0; i = 0;
} }
} }
@ -492,7 +502,10 @@ 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 (client) { node.status({}); client.destroy(); } if (clients[connection_id]) {
node.status({}); clients[connection_id].destroy();
delete clients[connection_id];
}
i = 0; i = 0;
} }
} }
@ -500,48 +513,53 @@ module.exports = function(RED) {
} }
}); });
client.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"});
client = null; clients[connection_id] = null;
}); });
client.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(); }
}); });
client.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 (client) { client.destroy(); } if (clients[connection_id]) {
clients[connection_id].destroy();
delete clients[connection_id];
}
}); });
client.on('timeout',function() { clients[connection_id].on('timeout',function() {
//console.log("TIMEOUT");
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 (client) { if (clients[connection_id]) {
client.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"});
}); });
} }
}); });
} }
else { client.write(msg.payload); } else {
clients[connection_id].write(msg.payload);
}
}); });
this.on("close", function(done) { this.on("close", function(done) {
node.done = done; node.done = done;
if (client) { for (var client in clients) {
client.destroy(); clients[client].destroy();
} }
clients = {};
node.status({}); node.status({});
if (!node.connected) { done(); } if (!node.connected) { done(); }
}); });