diff --git a/red/tcp/tcp.html b/red/tcp/tcp.html index 1b3e8c1..926e875 100644 --- a/red/tcp/tcp.html +++ b/red/tcp/tcp.html @@ -44,6 +44,11 @@ +
+ + + ms +
+
+ + + ms +
+
@@ -157,6 +170,7 @@ beserver: { value: "client", required: true }, base64: { value: false, required: true }, end: { value: false, required: true }, + socketTimeout: { value: "" }, name: { value: "" } }, inputs: 1, @@ -188,6 +202,7 @@ }; updateOptions(); $("#node-input-beserver").change(updateOptions); + $("#node-input-socketTimeout").spinner({ min: 0 }); } }); @@ -212,6 +227,11 @@
+
+ + + ms +
@@ -227,6 +247,7 @@ port: { value: "", validate: RED.validators.regex(/^(\d*|)$/) }, out: { value: "time", required: true }, splitc: { value: "0", required: true }, + socketTimeout: { value: "" }, name: { value: "" } }, inputs: 1, @@ -263,6 +284,7 @@ $("#node-units").text(""); } }); + $("#node-input-socketTimeout").spinner({ min: 0 }); } }); @@ -289,4 +311,4 @@ return this.name ? "node_label_italic" : ""; }, }); - \ No newline at end of file + diff --git a/red/tcp/tcp.js b/red/tcp/tcp.js index 6bd170f..9092c74 100644 --- a/red/tcp/tcp.js +++ b/red/tcp/tcp.js @@ -17,13 +17,27 @@ module.exports = function(RED) { "use strict"; var reconnectTime = RED.settings.socketReconnectTime||10000; - var socketTimeout = RED.settings.socketTimeout||null; + var socketTimeout = parseSocketTimeout(RED.settings.socketTimeout, null); const msgQueueSize = RED.settings.tcpMsgQueueSize || 1000; const Denque = require('denque'); var net = require('net'); var connectionPool = {}; + function parseSocketTimeout(value, defaultValue) { + if (value === undefined || value === null || value === "") { + return defaultValue; + } + value = Number(value); + return Number.isFinite(value) && value >= 0 ? value : defaultValue; + } + + function applySocketTimeout(socket, timeout) { + if (timeout !== null && timeout > 0) { + socket.setTimeout(timeout); + } + } + /** * Enqueue `item` in `queue` * @param {Denque} queue - Queue @@ -56,6 +70,7 @@ module.exports = function(RED) { this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r"); this.base64 = n.base64; this.keepalive = parseInt(n.keepalive) || 120000; + this.socketTimeout = parseSocketTimeout(n.socketTimeout, socketTimeout); this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server"); this.closing = false; this.connected = false; @@ -77,7 +92,8 @@ module.exports = function(RED) { node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port})); node.status({fill:"green",shape:"dot",text:"common.status.connected"}); }); - client.setKeepAlive(true, this.keepalive); + client.setKeepAlive(true, node.keepalive); + applySocketTimeout(client, node.socketTimeout); connectionPool[id] = client; client.on('data', function (data) { @@ -152,8 +168,8 @@ module.exports = function(RED) { } else { var server = net.createServer(function (socket) { - socket.setKeepAlive(true, this.keepalive); - if (socketTimeout !== null) { socket.setTimeout(socketTimeout); } + socket.setKeepAlive(true, node.keepalive); + applySocketTimeout(socket, node.socketTimeout); var id = (1+Math.random()*4294967295).toString(16); var fromi; var fromp; @@ -267,6 +283,7 @@ module.exports = function(RED) { this.doend = n.end || false; this.beserver = n.beserver; this.name = n.name; + this.socketTimeout = parseSocketTimeout(n.socketTimeout, socketTimeout); this.closing = false; this.connected = false; var node = this; @@ -285,6 +302,7 @@ module.exports = function(RED) { node.status({fill:"green",shape:"dot",text:"common.status.connected"}); }); client.setKeepAlive(true,120000); + applySocketTimeout(client, node.socketTimeout); client.on('error', function (err) { node.log(RED._("tcpin.errors.error",{error:err.toString()})); }); @@ -371,7 +389,7 @@ module.exports = function(RED) { node.status({text:RED._("tcpin.status.connections",{count:0})}); var server = net.createServer(function (socket) { socket.setKeepAlive(true,120000); - if (socketTimeout !== null) { socket.setTimeout(socketTimeout); } + applySocketTimeout(socket, node.socketTimeout); node.log(RED._("tcpin.status.connection-from",{host:socket.remoteAddress, port:socket.remotePort})); connectedSockets.push(socket); node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})}); @@ -443,6 +461,7 @@ module.exports = function(RED) { this.port = Number(n.port); this.out = n.out; this.splitc = n.splitc; + this.socketTimeout = parseSocketTimeout(n.socketTimeout, socketTimeout); if (this.out === "immed") { this.splitc = -1; this.out = "time"; } if (this.out !== "char") { this.splitc = Number(this.splitc); } @@ -498,7 +517,7 @@ module.exports = function(RED) { else { buf = Buffer.alloc(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully clients[connection_id].client = net.Socket(); - if (socketTimeout !== null) { clients[connection_id].client.setTimeout(socketTimeout);} + applySocketTimeout(clients[connection_id].client, node.socketTimeout); if (host && port) { clients[connection_id].connecting = true; @@ -713,4 +732,4 @@ module.exports = function(RED) { }); } RED.nodes.registerType("briq tcp close", TcpClose); -} \ No newline at end of file +}