1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/nodes/core/io/31-tcpin.js

525 lines
23 KiB
JavaScript
Raw Normal View History

2013-09-05 16:02:48 +02:00
/**
* Copyright 2013,2015 IBM Corp.
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) {
"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-04 00:32:04 +02:00
var connectionPool = {};
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;
var count = 0;
2014-05-04 00:32:04 +02:00
if (!node.server) {
var buffer = null;
var client;
var reconnectTimeout;
var end = false;
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() {
buffer = (node.datatype == 'buffer')? new Buffer(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-04 00:32:04 +02:00
client.on('data', function (data) {
if (node.datatype != 'buffer') {
data = data.toString(node.datatype);
}
if (node.stream) {
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);
for (var i = 0;i<parts.length-1;i+=1) {
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 {
msg = {topic:node.topic, payload:data};
msg._session = {type:"tcp",id:id};
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() {
if (!node.stream || (node.datatype == "utf8" && node.newline != "" && buffer.length > 0)) {
var msg = {topic:node.topic, payload:buffer};
msg._session = {type:"tcp",id:id};
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;
}
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) {
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}));
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
});
client.on('error', function(err) {
node.log(err);
2014-05-04 00:32:04 +02:00
});
}
setupTcpClient();
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;
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(); }
});
2014-05-04 00:32:04 +02:00
} else {
var server = net.createServer(function (socket) {
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;
count++;
node.status({text:RED._("tcpin.status.connections",{count:count})});
2014-05-04 00:32:04 +02:00
var buffer = (node.datatype == 'buffer')? new Buffer(0):"";
socket.on('data', function (data) {
if (node.datatype != 'buffer') {
data = data.toString(node.datatype);
}
if (node.stream) {
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);
for (var i = 0; i<parts.length-1; i+=1) {
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 {
msg = {topic:node.topic, payload:data};
msg._session = {type:"tcp",id:id};
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);
}
}
});
socket.on('end', function() {
if (!node.stream || (node.datatype === "utf8" && node.newline !== "")) {
if (buffer.length > 0) {
var msg = {topic:node.topic, payload:buffer};
msg._session = {type:"tcp",id:id};
node.send(msg);
}
2014-05-04 00:32:04 +02:00
buffer = null;
}
});
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];
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);
});
});
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()}));
}
});
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()}));
} 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() {
for (var c in connectionPool) {
if (connectionPool.hasOwnProperty(c)) {
connectionPool[c].end();
connectionPool[c].unref();
}
}
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
});
}
2014-05-04 00:32:04 +02:00
});
}
2014-05-04 00:32:04 +02:00
}
RED.nodes.registerType("tcp in",TcpIn);
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;
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-04 00:32:04 +02:00
if (!node.beserver||node.beserver=="client") {
var reconnectTimeout;
var client = null;
var end = false;
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) {
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) {
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}));
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
}
});
}
2014-05-04 00:32:04 +02:00
setupTcpClient();
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) {
if (Buffer.isBuffer(msg.payload)) {
client.write(msg.payload);
} else if (typeof msg.payload === "string" && node.base64) {
client.write(new Buffer(msg.payload,'base64'));
} else {
client.write(new Buffer(""+msg.payload));
}
if (node.doend === true) {
end = true;
if (client) { client.end(); }
}
}
});
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;
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(); }
});
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) {
client.write(new Buffer(msg.payload,'base64'));
} else {
client.write(new Buffer(""+msg.payload));
}
}
}
});
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) {
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-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) {
buffer = new Buffer(msg.payload,'base64');
} else {
buffer = new Buffer(""+msg.payload);
}
for (var i = 0; i < connectedSockets.length; i += 1) {
if (node.doend === true) { connectedSockets[i].end(buffer); }
else { connectedSockets[i].write(buffer); }
2014-05-04 00:32:04 +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()}));
}
2014-05-04 00:32:04 +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() {
for (var c in connectedSockets) {
if (connectedSockets.hasOwnProperty(c)) {
connectedSockets[c].end();
connectedSockets[c].unref();
}
}
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
});
}
2014-05-04 00:32:04 +02:00
});
}
}
2014-05-04 00:32:04 +02:00
RED.nodes.registerType("tcp out",TcpOut);
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;
if (this.out != "char") { this.splitc = Number(this.splitc); }
else { this.splitc = this.splitc.replace("\\n",0x0A).replace("\\r",0x0D).replace("\\t",0x09).replace("\\e",0x1B).replace("\\f",0x0C).replace("\\0",0x00); } // jshint ignore:line
var buf;
if (this.out == "count") { 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;
var node = this;
var client;
this.on("input", function(msg) {
var i = 0;
if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) {
msg.payload = msg.payload.toString();
}
if (!node.connected) {
client = net.Socket();
if (socketTimeout !== null) { client.setTimeout(socketTimeout); }
var host = node.server || msg.host;
var port = node.port || msg.port;
if (host && port) {
client.connect(port, host, function() {
//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"});
node.connected = true;
client.write(msg.payload);
});
}
else {
node.warn(RED._("tcpin.errors.no-host"));
}
client.on('data', function(data) {
if (node.out == "sit") { // if we are staying connected just send the buffer
msg.payload = data;
node.send(msg);
}
else if (node.splitc === 0) {
msg.payload = data;
node.send(msg);
}
else {
for (var j = 0; j < data.length; j++ ) {
if (node.out === "time") {
// do the timer thing
if (node.tout) {
i += 1;
buf[i] = data[j];
}
else {
node.tout = setTimeout(function () {
node.tout = null;
msg.payload = new Buffer(i+1);
buf.copy(msg.payload,0,0,i+1);
node.send(msg);
//if (client) { client.end(); }
}, node.splitc);
i = 0;
buf[0] = data[j];
}
}
// count bytes into a buffer...
else if (node.out == "count") {
buf[i] = data[j];
i += 1;
if ( i >= node.splitc) {
msg.payload = new Buffer(i);
buf.copy(msg.payload,0,0,i);
node.send(msg);
//if (client) { client.end(); }
i = 0;
}
}
// look for a char
else {
buf[i] = data[j];
i += 1;
if (data[j] == node.splitc) {
msg.payload = new Buffer(i);
buf.copy(msg.payload,0,0,i);
node.send(msg);
//if (client) { client.end(); }
i = 0;
}
}
}
}
});
client.on('end', function() {
//console.log("END");
node.connected = false;
node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"});
client = null;
});
2015-01-29 22:42:56 +01:00
client.on('close', function() {
//console.log("CLOSE");
node.connected = false;
2015-01-29 22:42:56 +01:00
if (node.done) { node.done(); }
});
client.on('error', function() {
//console.log("ERROR");
node.connected = false;
2015-07-02 00:02:50 +02:00
node.status({fill:"red",shape:"ring",text:"common.status.error"});
node.error(RED._("tcpin.errors.connect-fail"),msg);
2015-12-07 23:39:32 +01:00
if (client) { client.destroy(); }
});
client.on('timeout',function() {
//console.log("TIMEOUT");
node.connected = false;
node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"});
//node.warn(RED._("tcpin.errors.connect-timeout"));
if (client) {
2015-12-07 23:39:32 +01:00
client.connect(port, host, function() {
node.connected = true;
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
});
}
});
}
else { client.write(msg.payload); }
});
2015-01-29 22:42:56 +01:00
this.on("close", function(done) {
node.done = done;
if (client) {
buf = null;
client.destroy();
2015-01-29 22:42:56 +01:00
}
node.status({});
2015-01-29 22:42:56 +01:00
if (!node.connected) { done(); }
});
}
RED.nodes.registerType("tcp request",TcpGet);
2013-09-05 16:02:48 +02:00
}