2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-14 12:44:19 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var reconnectTime = RED.settings.socketReconnectTime||10000;
|
|
|
|
var socketTimeout = RED.settings.socketTimeout||null;
|
|
|
|
var net = require('net');
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
var connectionPool = {};
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function TcpIn(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.host = n.host;
|
|
|
|
this.port = n.port * 1;
|
|
|
|
this.topic = n.topic;
|
|
|
|
this.stream = (!n.datamode||n.datamode=='stream'); /* stream,single*/
|
|
|
|
this.datatype = n.datatype||'buffer'; /* buffer,utf8,base64 */
|
|
|
|
this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r");
|
|
|
|
this.base64 = n.base64;
|
|
|
|
this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server");
|
|
|
|
this.closing = false;
|
2015-01-29 22:42:56 +01:00
|
|
|
this.connected = false;
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-09-03 21:06:29 +02:00
|
|
|
var count = 0;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!node.server) {
|
|
|
|
var buffer = null;
|
|
|
|
var client;
|
|
|
|
var reconnectTimeout;
|
2014-09-03 21:06:29 +02:00
|
|
|
var end = false;
|
2014-05-14 12:44:19 +02:00
|
|
|
var setupTcpClient = function() {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
|
2014-05-04 00:32:04 +02:00
|
|
|
var id = (1+Math.random()*4294967295).toString(16);
|
|
|
|
client = net.connect(node.port, node.host, function() {
|
2017-06-27 18:11:36 +02:00
|
|
|
buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
|
2015-01-29 22:42:56 +01:00
|
|
|
node.connected = true;
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
connectionPool[id] = client;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
client.on('data', function (data) {
|
|
|
|
if (node.datatype != 'buffer') {
|
|
|
|
data = data.toString(node.datatype);
|
|
|
|
}
|
|
|
|
if (node.stream) {
|
2015-04-19 13:13:45 +02:00
|
|
|
var msg;
|
|
|
|
if ((node.datatype) === "utf8" && node.newline !== "") {
|
2014-05-04 00:32:04 +02:00
|
|
|
buffer = buffer+data;
|
|
|
|
var parts = buffer.split(node.newline);
|
2017-04-20 13:22:36 +02:00
|
|
|
for (var i = 0; i<parts.length-1; i+=1) {
|
2015-04-19 13:13:45 +02:00
|
|
|
msg = {topic:node.topic, payload:parts[i]};
|
2014-05-04 00:32:04 +02:00
|
|
|
msg._session = {type:"tcp",id:id};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
buffer = parts[parts.length-1];
|
|
|
|
} else {
|
2015-04-19 13:13:45 +02:00
|
|
|
msg = {topic:node.topic, payload:data};
|
2013-12-19 22:16:25 +01:00
|
|
|
msg._session = {type:"tcp",id:id};
|
2013-11-14 15:39:26 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-04 00:32:04 +02:00
|
|
|
if ((typeof data) === "string") {
|
|
|
|
buffer = buffer+data;
|
|
|
|
} else {
|
|
|
|
buffer = Buffer.concat([buffer,data],buffer.length+data.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
client.on('end', function() {
|
2016-11-10 17:17:27 +01:00
|
|
|
if (!node.stream || (node.datatype == "utf8" && node.newline !== "" && buffer.length > 0)) {
|
2015-05-26 21:08:58 +02:00
|
|
|
var msg = {topic:node.topic, payload:buffer};
|
2013-12-19 22:16:25 +01:00
|
|
|
msg._session = {type:"tcp",id:id};
|
2014-09-03 21:06:29 +02:00
|
|
|
if (buffer.length !== 0) {
|
|
|
|
end = true; // only ask for fast re-connect if we actually got something
|
|
|
|
node.send(msg);
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
buffer = null;
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('close', function() {
|
|
|
|
delete connectionPool[id];
|
2015-01-29 22:42:56 +01:00
|
|
|
node.connected = false;
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"common.status.disconnected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!node.closing) {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (end) { // if we were asked to close then try to reconnect once very quick.
|
|
|
|
end = false;
|
|
|
|
reconnectTimeout = setTimeout(setupTcpClient, 20);
|
|
|
|
}
|
|
|
|
else {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.errors.connection-lost",{host:node.host,port:node.port}));
|
2014-09-03 21:06:29 +02:00
|
|
|
reconnectTimeout = setTimeout(setupTcpClient, reconnectTime);
|
|
|
|
}
|
2015-01-29 22:42:56 +01:00
|
|
|
} else {
|
|
|
|
if (node.done) { node.done(); }
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('error', function(err) {
|
2014-05-12 17:32:19 +02:00
|
|
|
node.log(err);
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
setupTcpClient();
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2015-01-29 22:42:56 +01:00
|
|
|
this.on('close', function(done) {
|
|
|
|
node.done = done;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.closing = true;
|
2015-12-07 20:44:25 +01:00
|
|
|
if (client) { client.destroy(); }
|
2014-05-04 00:32:04 +02:00
|
|
|
clearTimeout(reconnectTimeout);
|
2015-01-29 22:42:56 +01:00
|
|
|
if (!node.connected) { done(); }
|
2013-11-14 15:39:26 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
var server = net.createServer(function (socket) {
|
2015-10-20 11:13:48 +02:00
|
|
|
socket.setKeepAlive(true,120000);
|
2014-05-04 00:32:04 +02:00
|
|
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
|
|
|
var id = (1+Math.random()*4294967295).toString(16);
|
|
|
|
connectionPool[id] = socket;
|
2015-10-20 11:13:48 +02:00
|
|
|
count++;
|
|
|
|
node.status({text:RED._("tcpin.status.connections",{count:count})});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2017-06-27 18:11:36 +02:00
|
|
|
var buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
|
2014-05-04 00:32:04 +02:00
|
|
|
socket.on('data', function (data) {
|
|
|
|
if (node.datatype != 'buffer') {
|
|
|
|
data = data.toString(node.datatype);
|
|
|
|
}
|
|
|
|
if (node.stream) {
|
2015-04-19 13:13:45 +02:00
|
|
|
var msg;
|
|
|
|
if ((typeof data) === "string" && node.newline !== "") {
|
2014-05-04 00:32:04 +02:00
|
|
|
buffer = buffer+data;
|
|
|
|
var parts = buffer.split(node.newline);
|
2015-04-19 13:13:45 +02:00
|
|
|
for (var i = 0; i<parts.length-1; i+=1) {
|
2017-04-20 13:22:36 +02:00
|
|
|
msg = {topic:node.topic, payload:parts[i], ip:socket.remoteAddress, port:socket.remotePort};
|
2014-05-04 00:32:04 +02:00
|
|
|
msg._session = {type:"tcp",id:id};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
buffer = parts[parts.length-1];
|
|
|
|
} else {
|
2017-04-20 13:22:36 +02:00
|
|
|
msg = {topic:node.topic, payload:data, ip:socket.remoteAddress, port:socket.remotePort};
|
2013-12-19 22:16:25 +01:00
|
|
|
msg._session = {type:"tcp",id:id};
|
2013-11-14 15:39:26 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
2013-12-24 14:12:17 +01:00
|
|
|
} else {
|
2014-05-04 00:32:04 +02:00
|
|
|
if ((typeof data) === "string") {
|
|
|
|
buffer = buffer+data;
|
|
|
|
} else {
|
|
|
|
buffer = Buffer.concat([buffer,data],buffer.length+data.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
socket.on('end', function() {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (!node.stream || (node.datatype === "utf8" && node.newline !== "")) {
|
|
|
|
if (buffer.length > 0) {
|
2017-04-20 13:22:36 +02:00
|
|
|
var msg = {topic:node.topic, payload:buffer, ip:socket.remoteAddress, port:socket.remotePort};
|
2014-09-03 21:06:29 +02:00
|
|
|
msg._session = {type:"tcp",id:id};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
buffer = null;
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-03-17 17:09:07 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
socket.on('timeout', function() {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.errors.timeout",{port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
socket.on('close', function() {
|
|
|
|
delete connectionPool[id];
|
2015-10-20 11:13:48 +02:00
|
|
|
count--;
|
|
|
|
node.status({text:RED._("tcpin.status.connections",{count:count})});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
socket.on('error',function(err) {
|
|
|
|
node.log(err);
|
|
|
|
});
|
2013-11-14 15:39:26 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on('error', function(err) {
|
|
|
|
if (err) {
|
2015-06-16 17:09:53 +02:00
|
|
|
node.error(RED._("tcpin.errors.cannot-listen",{port:node.port,error:err.toString()}));
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2013-11-14 15:39:26 +01:00
|
|
|
});
|
2015-04-19 13:13:45 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.listen(node.port, function(err) {
|
|
|
|
if (err) {
|
2015-06-16 17:09:53 +02:00
|
|
|
node.error(RED._("tcpin.errors.cannot-listen",{port:node.port,error:err.toString()}));
|
2013-12-24 14:12:17 +01:00
|
|
|
} else {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.listening-port",{port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on('close', function() {
|
2014-10-25 18:52:24 +02:00
|
|
|
for (var c in connectionPool) {
|
2015-04-19 13:13:45 +02:00
|
|
|
if (connectionPool.hasOwnProperty(c)) {
|
|
|
|
connectionPool[c].end();
|
|
|
|
connectionPool[c].unref();
|
|
|
|
}
|
2014-10-25 18:52:24 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
node.closing = true;
|
|
|
|
server.close();
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.stopped-listening",{port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("tcp in",TcpIn);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function TcpOut(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.host = n.host;
|
|
|
|
this.port = n.port * 1;
|
|
|
|
this.base64 = n.base64;
|
2014-09-03 21:06:29 +02:00
|
|
|
this.doend = n.end || false;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.beserver = n.beserver;
|
|
|
|
this.name = n.name;
|
|
|
|
this.closing = false;
|
2015-01-29 22:42:56 +01:00
|
|
|
this.connected = false;
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!node.beserver||node.beserver=="client") {
|
|
|
|
var reconnectTimeout;
|
|
|
|
var client = null;
|
2014-09-03 21:06:29 +02:00
|
|
|
var end = false;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-14 12:44:19 +02:00
|
|
|
var setupTcpClient = function() {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
|
2014-05-04 00:32:04 +02:00
|
|
|
client = net.connect(node.port, node.host, function() {
|
2015-01-29 22:42:56 +01:00
|
|
|
node.connected = true;
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('error', function (err) {
|
2015-06-16 17:09:53 +02:00
|
|
|
node.log(RED._("tcpin.errors.error",{error:err.toString()}));
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('end', function (err) {
|
2015-12-07 18:41:51 +01:00
|
|
|
node.status({});
|
|
|
|
node.connected = false;
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('close', function() {
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"common.status.disconnected"});
|
2015-01-29 22:42:56 +01:00
|
|
|
node.connected = false;
|
2014-05-04 00:32:04 +02:00
|
|
|
client.destroy();
|
|
|
|
if (!node.closing) {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (end) {
|
|
|
|
end = false;
|
|
|
|
reconnectTimeout = setTimeout(setupTcpClient,20);
|
|
|
|
}
|
|
|
|
else {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.errors.connection-lost",{host:node.host,port:node.port}));
|
2014-09-03 21:06:29 +02:00
|
|
|
reconnectTimeout = setTimeout(setupTcpClient,reconnectTime);
|
|
|
|
}
|
2015-01-29 22:42:56 +01:00
|
|
|
} else {
|
|
|
|
if (node.done) { node.done(); }
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
});
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
setupTcpClient();
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on("input", function(msg) {
|
2015-01-29 22:42:56 +01:00
|
|
|
if (node.connected && msg.payload != null) {
|
2013-12-19 22:16:25 +01:00
|
|
|
if (Buffer.isBuffer(msg.payload)) {
|
|
|
|
client.write(msg.payload);
|
|
|
|
} else if (typeof msg.payload === "string" && node.base64) {
|
2017-06-27 18:11:36 +02:00
|
|
|
client.write(Buffer.from(msg.payload,'base64'));
|
2013-12-19 22:16:25 +01:00
|
|
|
} else {
|
2017-06-27 18:11:36 +02:00
|
|
|
client.write(Buffer.from(""+msg.payload));
|
2013-12-19 22:16:25 +01:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
if (node.doend === true) {
|
|
|
|
end = true;
|
2015-12-23 21:01:05 +01:00
|
|
|
if (client) { node.status({}); client.destroy(); }
|
2014-09-03 21:06:29 +02:00
|
|
|
}
|
2013-12-19 22:16:25 +01:00
|
|
|
}
|
2013-12-24 14:12:17 +01:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2015-01-29 22:42:56 +01:00
|
|
|
node.on("close", function(done) {
|
|
|
|
node.done = done;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.closing = true;
|
2015-12-07 18:41:51 +01:00
|
|
|
if (client) { client.destroy(); }
|
2014-05-04 00:32:04 +02:00
|
|
|
clearTimeout(reconnectTimeout);
|
2015-01-29 22:42:56 +01:00
|
|
|
if (!node.connected) { done(); }
|
2013-12-24 14:12:17 +01:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
} else if (node.beserver == "reply") {
|
|
|
|
node.on("input",function(msg) {
|
|
|
|
if (msg._session && msg._session.type == "tcp") {
|
|
|
|
var client = connectionPool[msg._session.id];
|
|
|
|
if (client) {
|
|
|
|
if (Buffer.isBuffer(msg.payload)) {
|
|
|
|
client.write(msg.payload);
|
|
|
|
} else if (typeof msg.payload === "string" && node.base64) {
|
2017-06-27 18:11:36 +02:00
|
|
|
client.write(Buffer.from(msg.payload,'base64'));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2017-06-27 18:11:36 +02:00
|
|
|
client.write(Buffer.from(""+msg.payload));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 14:17:50 +01:00
|
|
|
else {
|
|
|
|
for (var i in connectionPool) {
|
|
|
|
if (Buffer.isBuffer(msg.payload)) {
|
|
|
|
connectionPool[i].write(msg.payload);
|
|
|
|
} else if (typeof msg.payload === "string" && node.base64) {
|
2017-06-27 18:11:36 +02:00
|
|
|
connectionPool[i].write(Buffer.from(msg.payload,'base64'));
|
2016-02-12 14:17:50 +01:00
|
|
|
} else {
|
2017-06-27 18:11:36 +02:00
|
|
|
connectionPool[i].write(Buffer.from(""+msg.payload));
|
2016-02-12 14:17:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 14:12:17 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
var connectedSockets = [];
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({text:RED._("tcpin.status.connections",{count:0})});
|
2014-05-04 00:32:04 +02:00
|
|
|
var server = net.createServer(function (socket) {
|
2015-10-20 11:13:48 +02:00
|
|
|
socket.setKeepAlive(true,120000);
|
2014-05-04 00:32:04 +02:00
|
|
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
|
|
|
var remoteDetails = socket.remoteAddress+":"+socket.remotePort;
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.connection-from",{host:socket.remoteAddress, port:socket.remotePort}));
|
2014-05-04 00:32:04 +02:00
|
|
|
connectedSockets.push(socket);
|
2015-10-09 16:44:22 +02:00
|
|
|
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})});
|
2014-05-04 00:32:04 +02:00
|
|
|
socket.on('timeout', function() {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.errors.timeout",{port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
socket.on('close',function() {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.connection-closed",{host:socket.remoteAddress, port:socket.remotePort}));
|
2014-05-04 00:32:04 +02:00
|
|
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
socket.on('error',function() {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.errors.socket-error",{host:socket.remoteAddress, port:socket.remotePort}));
|
2014-05-04 00:32:04 +02:00
|
|
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
});
|
2014-09-03 21:06:29 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (msg.payload != null) {
|
|
|
|
var buffer;
|
|
|
|
if (Buffer.isBuffer(msg.payload)) {
|
|
|
|
buffer = msg.payload;
|
|
|
|
} else if (typeof msg.payload === "string" && node.base64) {
|
2017-06-27 18:11:36 +02:00
|
|
|
buffer = Buffer.from(msg.payload,'base64');
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2017-06-27 18:11:36 +02:00
|
|
|
buffer = Buffer.from(""+msg.payload);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2015-05-26 21:08:58 +02:00
|
|
|
for (var i = 0; i < connectedSockets.length; i += 1) {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (node.doend === true) { connectedSockets[i].end(buffer); }
|
|
|
|
else { connectedSockets[i].write(buffer); }
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on('error', function(err) {
|
|
|
|
if (err) {
|
2015-06-16 17:09:53 +02:00
|
|
|
node.error(RED._("tcpin.errors.cannot-listen",{port:node.port,error:err.toString()}));
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.listen(node.port, function(err) {
|
|
|
|
if (err) {
|
2015-06-16 17:09:53 +02:00
|
|
|
node.error(RED._("tcpin.errors.cannot-listen",{port:node.port,error:err.toString()}));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.listening-port",{port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on('close', function() {
|
2014-10-25 18:52:24 +02:00
|
|
|
for (var c in connectedSockets) {
|
2015-04-19 13:13:45 +02:00
|
|
|
if (connectedSockets.hasOwnProperty(c)) {
|
|
|
|
connectedSockets[c].end();
|
|
|
|
connectedSockets[c].unref();
|
|
|
|
}
|
2014-10-25 18:52:24 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
server.close();
|
2015-05-28 00:07:31 +02:00
|
|
|
node.log(RED._("tcpin.status.stopped-listening",{port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("tcp out",TcpOut);
|
2014-07-08 14:45:00 +02:00
|
|
|
|
|
|
|
function TcpGet(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.port = Number(n.port);
|
|
|
|
this.out = n.out;
|
|
|
|
this.splitc = n.splitc;
|
|
|
|
|
2017-05-11 18:24:20 +02:00
|
|
|
if (this.out === "immed") { this.splitc = -1; this.out = "time"; }
|
|
|
|
if (this.out !== "char") { this.splitc = Number(this.splitc); }
|
2016-08-02 17:09:41 +02:00
|
|
|
else {
|
2016-08-02 18:32:49 +02:00
|
|
|
if (this.splitc[0] == '\\') {
|
|
|
|
this.splitc = parseInt(this.splitc.replace("\\n",0x0A).replace("\\r",0x0D).replace("\\t",0x09).replace("\\e",0x1B).replace("\\f",0x0C).replace("\\0",0x00));
|
2016-08-04 16:49:38 +02:00
|
|
|
} // jshint ignore:line
|
2016-08-02 18:32:49 +02:00
|
|
|
if (typeof this.splitc == "string") {
|
|
|
|
if (this.splitc.substr(0,2) == "0x") {
|
|
|
|
this.splitc = parseInt(this.splitc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.splitc = this.splitc.charCodeAt(0);
|
|
|
|
}
|
2016-08-04 16:49:38 +02:00
|
|
|
} // jshint ignore:line
|
|
|
|
}
|
2016-08-02 18:32:49 +02:00
|
|
|
|
2014-07-08 14:45:00 +02:00
|
|
|
var node = this;
|
|
|
|
|
2016-11-10 04:33:55 +01:00
|
|
|
var clients = {};
|
|
|
|
|
2014-07-08 14:45:00 +02:00
|
|
|
this.on("input", function(msg) {
|
|
|
|
var i = 0;
|
2014-09-03 21:06:29 +02:00
|
|
|
if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) {
|
2014-07-08 14:45:00 +02:00
|
|
|
msg.payload = msg.payload.toString();
|
|
|
|
}
|
2016-11-09 16:15:23 +01:00
|
|
|
|
2016-11-09 19:25:19 +01:00
|
|
|
var host = node.server || msg.host;
|
|
|
|
var port = node.port || msg.port;
|
2016-11-14 19:30:43 +01:00
|
|
|
|
|
|
|
// Store client information independently
|
|
|
|
// the clients object will have:
|
|
|
|
// clients[id].client, clients[id].msg, clients[id].timeout
|
2016-11-10 23:35:44 +01:00
|
|
|
var connection_id = host + ":" + port;
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id] = clients[connection_id] || {};
|
|
|
|
clients[connection_id].msg = msg;
|
2016-11-19 17:25:54 +01:00
|
|
|
clients[connection_id].connected = clients[connection_id].connected || false;
|
2016-11-09 19:25:19 +01:00
|
|
|
|
2016-11-19 17:25:54 +01:00
|
|
|
if (!clients[connection_id].connected) {
|
2016-11-09 16:15:23 +01:00
|
|
|
var buf;
|
|
|
|
if (this.out == "count") {
|
2017-06-27 18:11:36 +02:00
|
|
|
if (this.splitc === 0) { buf = Buffer.alloc(1); }
|
|
|
|
else { buf = Buffer.alloc(this.splitc); }
|
2016-11-09 16:15:23 +01:00
|
|
|
}
|
2017-06-27 18:11:36 +02:00
|
|
|
else { buf = Buffer.alloc(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
|
2016-11-09 16:15:23 +01:00
|
|
|
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client = net.Socket();
|
|
|
|
if (socketTimeout !== null) { clients[connection_id].client.setTimeout(socketTimeout);}
|
2015-05-26 21:08:58 +02:00
|
|
|
|
2014-11-06 11:21:14 +01:00
|
|
|
if (host && port) {
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.connect(port, host, function() {
|
2015-05-10 22:47:22 +02:00
|
|
|
//node.log(RED._("tcpin.errors.client-connected"));
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
2016-11-17 22:04:38 +01:00
|
|
|
if (clients[connection_id] && clients[connection_id].client) {
|
2017-02-02 10:57:01 +01:00
|
|
|
clients[connection_id].connected = true;
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.write(clients[connection_id].msg.payload);
|
2017-05-11 18:24:20 +02:00
|
|
|
if (node.out === "time" && node.splitc < 0) {
|
2017-03-06 20:05:16 +01:00
|
|
|
clients[connection_id].connected = false;
|
2017-05-11 18:24:20 +02:00
|
|
|
clients[connection_id].client.end();
|
|
|
|
delete clients[connection_id];
|
2017-03-06 20:05:16 +01:00
|
|
|
node.status({});
|
|
|
|
}
|
2016-11-10 22:45:01 +01:00
|
|
|
}
|
2014-11-06 11:21:14 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.warn(RED._("tcpin.errors.no-host"));
|
2014-11-06 11:21:14 +01:00
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.on('data', function(data) {
|
2017-02-02 10:57:01 +01:00
|
|
|
if (node.out === "sit") { // if we are staying connected just send the buffer
|
2016-11-17 22:04:38 +01:00
|
|
|
if (clients[connection_id]) {
|
2017-04-20 13:22:36 +02:00
|
|
|
if (!clients[connection_id].hasOwnProperty("msg")) { clients[connection_id].msg = {}; }
|
2016-11-17 22:04:38 +01:00
|
|
|
clients[connection_id].msg.payload = data;
|
2017-02-02 10:57:01 +01:00
|
|
|
node.send(RED.util.cloneMessage(clients[connection_id].msg));
|
2016-11-17 22:04:38 +01:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
}
|
2017-05-11 20:11:51 +02:00
|
|
|
// else if (node.splitc === 0) {
|
|
|
|
// clients[connection_id].msg.payload = data;
|
|
|
|
// node.send(clients[connection_id].msg);
|
|
|
|
// }
|
2014-09-03 21:06:29 +02:00
|
|
|
else {
|
|
|
|
for (var j = 0; j < data.length; j++ ) {
|
2016-04-10 13:21:56 +02:00
|
|
|
if (node.out === "time") {
|
2016-11-26 22:16:19 +01:00
|
|
|
if (clients[connection_id]) {
|
|
|
|
// do the timer thing
|
|
|
|
if (clients[connection_id].timeout) {
|
|
|
|
i += 1;
|
|
|
|
buf[i] = data[j];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
clients[connection_id].timeout = setTimeout(function () {
|
|
|
|
if (clients[connection_id]) {
|
|
|
|
clients[connection_id].timeout = null;
|
2017-06-27 18:11:36 +02:00
|
|
|
clients[connection_id].msg.payload = Buffer.alloc(i+1);
|
2016-11-26 22:16:19 +01:00
|
|
|
buf.copy(clients[connection_id].msg.payload,0,0,i+1);
|
|
|
|
node.send(clients[connection_id].msg);
|
|
|
|
if (clients[connection_id].client) {
|
2017-04-20 13:22:36 +02:00
|
|
|
node.status({});
|
2017-03-06 20:05:16 +01:00
|
|
|
clients[connection_id].client.destroy();
|
2016-11-26 22:16:19 +01:00
|
|
|
delete clients[connection_id];
|
|
|
|
}
|
2016-11-19 17:25:54 +01:00
|
|
|
}
|
2016-11-26 22:16:19 +01:00
|
|
|
}, node.splitc);
|
|
|
|
i = 0;
|
|
|
|
buf[0] = data[j];
|
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// count bytes into a buffer...
|
|
|
|
else if (node.out == "count") {
|
2014-07-08 14:45:00 +02:00
|
|
|
buf[i] = data[j];
|
2014-09-03 21:06:29 +02:00
|
|
|
i += 1;
|
2015-05-26 21:08:58 +02:00
|
|
|
if ( i >= node.splitc) {
|
2016-11-18 12:25:06 +01:00
|
|
|
if (clients[connection_id]) {
|
2017-06-27 18:11:36 +02:00
|
|
|
clients[connection_id].msg.payload = Buffer.alloc(i);
|
2016-11-18 12:25:06 +01:00
|
|
|
buf.copy(clients[connection_id].msg.payload,0,0,i);
|
|
|
|
node.send(clients[connection_id].msg);
|
|
|
|
if (clients[connection_id].client) {
|
2017-04-20 13:22:36 +02:00
|
|
|
node.status({});
|
2017-03-06 20:05:16 +01:00
|
|
|
clients[connection_id].client.destroy();
|
2016-11-18 12:25:06 +01:00
|
|
|
delete clients[connection_id];
|
|
|
|
}
|
|
|
|
i = 0;
|
2016-11-09 19:25:19 +01:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
// look for a char
|
2014-07-08 14:45:00 +02:00
|
|
|
else {
|
2014-09-03 21:06:29 +02:00
|
|
|
buf[i] = data[j];
|
|
|
|
i += 1;
|
|
|
|
if (data[j] == node.splitc) {
|
2016-11-18 12:25:06 +01:00
|
|
|
if (clients[connection_id]) {
|
2017-06-27 18:11:36 +02:00
|
|
|
clients[connection_id].msg.payload = Buffer.alloc(i);
|
2016-11-18 12:25:06 +01:00
|
|
|
buf.copy(clients[connection_id].msg.payload,0,0,i);
|
|
|
|
node.send(clients[connection_id].msg);
|
|
|
|
if (clients[connection_id].client) {
|
2017-03-06 20:05:16 +01:00
|
|
|
node.status({});
|
|
|
|
clients[connection_id].client.destroy();
|
2016-11-18 12:25:06 +01:00
|
|
|
delete clients[connection_id];
|
|
|
|
}
|
|
|
|
i = 0;
|
2016-11-09 19:25:19 +01:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
});
|
2014-07-08 14:45:00 +02:00
|
|
|
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.on('end', function() {
|
2015-11-30 23:05:26 +01:00
|
|
|
//console.log("END");
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"});
|
2016-11-17 22:04:38 +01:00
|
|
|
if (clients[connection_id] && clients[connection_id].client) {
|
2017-02-02 10:57:01 +01:00
|
|
|
clients[connection_id].connected = false;
|
2016-11-17 22:04:38 +01:00
|
|
|
clients[connection_id].client = null;
|
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
});
|
2014-07-08 14:45:00 +02:00
|
|
|
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.on('close', function() {
|
2015-11-30 23:05:26 +01:00
|
|
|
//console.log("CLOSE");
|
2016-11-19 17:25:54 +01:00
|
|
|
if (clients[connection_id]) {
|
2017-02-02 10:57:01 +01:00
|
|
|
clients[connection_id].connected = false;
|
2016-11-19 17:25:54 +01:00
|
|
|
}
|
2016-11-25 19:14:51 +01:00
|
|
|
|
|
|
|
var anyConnected = false;
|
|
|
|
|
|
|
|
for (var client in clients) {
|
|
|
|
if (clients[client].connected) {
|
|
|
|
anyConnected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.done && !anyConnected) {
|
|
|
|
clients = {};
|
|
|
|
node.done();
|
|
|
|
}
|
2015-01-29 22:42:56 +01:00
|
|
|
});
|
|
|
|
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.on('error', function() {
|
2015-11-30 23:05:26 +01:00
|
|
|
//console.log("ERROR");
|
2015-07-02 00:02:50 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"common.status.error"});
|
2016-11-19 17:25:54 +01:00
|
|
|
node.error(RED._("tcpin.errors.connect-fail") + " " + connection_id, msg);
|
2016-11-17 22:04:38 +01:00
|
|
|
if (clients[connection_id] && clients[connection_id].client) {
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.destroy();
|
2016-11-10 23:35:44 +01:00
|
|
|
delete clients[connection_id];
|
2016-11-09 19:25:19 +01:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
});
|
|
|
|
|
2016-11-14 19:30:43 +01:00
|
|
|
clients[connection_id].client.on('timeout',function() {
|
2016-11-25 19:14:51 +01:00
|
|
|
//console.log("TIMEOUT");
|
2017-05-09 23:09:21 +02:00
|
|
|
if (clients[connection_id]) {
|
|
|
|
clients[connection_id].connected = false;
|
|
|
|
node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"});
|
|
|
|
//node.warn(RED._("tcpin.errors.connect-timeout"));
|
|
|
|
if (clients[connection_id].client) {
|
|
|
|
clients[connection_id].client.connect(port, host, function() {
|
|
|
|
clients[connection_id].connected = true;
|
|
|
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
|
|
|
});
|
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-11-09 19:25:19 +01:00
|
|
|
else {
|
2016-11-18 12:25:06 +01:00
|
|
|
if (clients[connection_id] && clients[connection_id].client) {
|
|
|
|
clients[connection_id].client.write(clients[connection_id].msg.payload);
|
|
|
|
}
|
2016-11-09 19:25:19 +01:00
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
});
|
|
|
|
|
2015-01-29 22:42:56 +01:00
|
|
|
this.on("close", function(done) {
|
|
|
|
node.done = done;
|
2017-05-11 18:24:20 +02:00
|
|
|
for (var cl in clients) {
|
|
|
|
if (clients[cl].hasOwnProperty("client")) {
|
|
|
|
clients[cl].client.destroy();
|
2017-04-20 13:22:36 +02:00
|
|
|
}
|
2015-01-29 22:42:56 +01:00
|
|
|
}
|
2015-11-30 23:05:26 +01:00
|
|
|
node.status({});
|
2016-11-25 19:14:51 +01:00
|
|
|
|
2017-05-11 18:24:20 +02:00
|
|
|
// this is probably not necessary and may be removed
|
2016-11-25 19:14:51 +01:00
|
|
|
var anyConnected = false;
|
|
|
|
for (var c in clients) {
|
|
|
|
if (clients[c].connected) {
|
|
|
|
anyConnected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 20:11:51 +02:00
|
|
|
if (!anyConnected) { clients = {}; }
|
2017-05-11 18:24:20 +02:00
|
|
|
done();
|
2014-07-08 14:45:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("tcp request",TcpGet);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|