Rimuovi timeout socket dai nodi TCP

master
Guido Longoni 2026-05-06 12:45:55 +02:00
parent c99cde83ff
commit 6b19a8a606
2 changed files with 0 additions and 100 deletions

View File

@ -44,11 +44,6 @@
<label><i class="fa fa-clock-o"></i> <span>Keepalive</span></label> <label><i class="fa fa-clock-o"></i> <span>Keepalive</span></label>
<input type="text" id="node-input-keepalive" style="text-align:end; width:200px !important"> <input type="text" id="node-input-keepalive" style="text-align:end; width:200px !important">
</div> </div>
<div class="form-row">
<label><i class="fa fa-hourglass-half"></i> <span>Socket timeout</span></label>
<input type="text" id="node-input-socketTimeout" placeholder="default" style="text-align:end; width:120px !important">
<span>ms</span>
</div>
<div id="node-row-newline" class="form-row hidden" style="padding-left:110px;"> <div id="node-row-newline" class="form-row hidden" style="padding-left:110px;">
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;"> <span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;">
@ -77,7 +72,6 @@
datatype: { value: "buffer" }, datatype: { value: "buffer" },
newline: { value: "" }, newline: { value: "" },
keepalive: { value: "120000" }, keepalive: { value: "120000" },
socketTimeout: { value: "" },
topic: { value: "" }, topic: { value: "" },
base64: {/*deprecated*/ value: false, required: true } base64: {/*deprecated*/ value: false, required: true }
}, },
@ -115,7 +109,6 @@
$("#node-input-datatype").change(updateOptions); $("#node-input-datatype").change(updateOptions);
$("#node-input-datamode").change(updateOptions); $("#node-input-datamode").change(updateOptions);
$("#node-input-keepalive").spinner({ min: 1 }); $("#node-input-keepalive").spinner({ min: 1 });
$("#node-input-socketTimeout").spinner({ min: 0 });
} }
}); });
</script> </script>
@ -148,12 +141,6 @@
<label for="node-input-base64" style="width: 70%;"><span data-i18n="tcpin.label.decode-base64"></span></label> <label for="node-input-base64" style="width: 70%;"><span data-i18n="tcpin.label.decode-base64"></span></label>
</div> </div>
<div class="form-row">
<label><i class="fa fa-hourglass-half"></i> <span>Socket timeout</span></label>
<input type="text" id="node-input-socketTimeout" placeholder="default" style="text-align:end; width:120px !important">
<span>ms</span>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -170,7 +157,6 @@
beserver: { value: "client", required: true }, beserver: { value: "client", required: true },
base64: { value: false, required: true }, base64: { value: false, required: true },
end: { value: false, required: true }, end: { value: false, required: true },
socketTimeout: { value: "" },
name: { value: "" } name: { value: "" }
}, },
inputs: 1, inputs: 1,
@ -202,7 +188,6 @@
}; };
updateOptions(); updateOptions();
$("#node-input-beserver").change(updateOptions); $("#node-input-beserver").change(updateOptions);
$("#node-input-socketTimeout").spinner({ min: 0 });
} }
}); });
</script> </script>
@ -227,11 +212,6 @@
<input type="text" id="node-input-splitc" style="width:50px;"> <input type="text" id="node-input-splitc" style="width:50px;">
<span id="node-units"></span> <span id="node-units"></span>
</div> </div>
<div class="form-row">
<label><i class="fa fa-hourglass-half"></i> <span>Socket timeout</span></label>
<input type="text" id="node-input-socketTimeout" placeholder="default" style="text-align:end; width:120px !important">
<span>ms</span>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -247,7 +227,6 @@
port: { value: "", validate: RED.validators.regex(/^(\d*|)$/) }, port: { value: "", validate: RED.validators.regex(/^(\d*|)$/) },
out: { value: "time", required: true }, out: { value: "time", required: true },
splitc: { value: "0", required: true }, splitc: { value: "0", required: true },
socketTimeout: { value: "" },
name: { value: "" } name: { value: "" }
}, },
inputs: 1, inputs: 1,
@ -284,7 +263,6 @@
$("#node-units").text(""); $("#node-units").text("");
} }
}); });
$("#node-input-socketTimeout").spinner({ min: 0 });
} }
}); });
</script> </script>

View File

@ -17,39 +17,12 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var reconnectTime = RED.settings.socketReconnectTime||10000; var reconnectTime = RED.settings.socketReconnectTime||10000;
var socketTimeout = parseSocketTimeout(RED.settings.socketTimeout, null);
const msgQueueSize = RED.settings.tcpMsgQueueSize || 1000; const msgQueueSize = RED.settings.tcpMsgQueueSize || 1000;
const Denque = require('denque'); const Denque = require('denque');
var net = require('net'); var net = require('net');
var connectionPool = {}; 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);
}
}
function startConnectTimeout(socket, timeout, onTimeout) {
if (timeout === null || timeout <= 0) {
return null;
}
return setTimeout(function() {
if (!socket.destroyed) {
onTimeout();
socket.destroy();
}
}, timeout);
}
/** /**
* Enqueue `item` in `queue` * Enqueue `item` in `queue`
* @param {Denque} queue - Queue * @param {Denque} queue - Queue
@ -82,7 +55,6 @@ module.exports = function(RED) {
this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r"); this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r");
this.base64 = n.base64; this.base64 = n.base64;
this.keepalive = parseInt(n.keepalive) || 120000; this.keepalive = parseInt(n.keepalive) || 120000;
this.socketTimeout = parseSocketTimeout(n.socketTimeout, socketTimeout);
this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server"); this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server");
this.closing = false; this.closing = false;
this.connected = false; this.connected = false;
@ -98,19 +70,13 @@ module.exports = function(RED) {
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port})); node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"}); node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
var id = (1+Math.random()*4294967295).toString(16); var id = (1+Math.random()*4294967295).toString(16);
var connectTimeout;
client = net.connect(node.port, node.host, function() { client = net.connect(node.port, node.host, function() {
clearTimeout(connectTimeout);
buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : ""; buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
node.connected = true; node.connected = true;
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port})); node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
}); });
client.setKeepAlive(true, node.keepalive); client.setKeepAlive(true, node.keepalive);
applySocketTimeout(client, node.socketTimeout);
connectTimeout = startConnectTimeout(client, node.socketTimeout, function() {
node.log(RED._("tcpin.errors.timeout",{port:node.port}));
});
connectionPool[id] = client; connectionPool[id] = client;
client.on('data', function (data) { client.on('data', function (data) {
@ -152,12 +118,7 @@ module.exports = function(RED) {
buffer = null; buffer = null;
} }
}); });
client.on('timeout', function() {
node.log(RED._("tcpin.errors.timeout",{port:node.port}));
client.destroy();
});
client.on('close', function() { client.on('close', function() {
clearTimeout(connectTimeout);
delete connectionPool[id]; delete connectionPool[id];
node.connected = false; node.connected = false;
node.status({fill:"red",shape:"ring",text:"common.status.disconnected"}); node.status({fill:"red",shape:"ring",text:"common.status.disconnected"});
@ -191,7 +152,6 @@ module.exports = function(RED) {
else { else {
var server = net.createServer(function (socket) { var server = net.createServer(function (socket) {
socket.setKeepAlive(true, node.keepalive); socket.setKeepAlive(true, node.keepalive);
applySocketTimeout(socket, node.socketTimeout);
var id = (1+Math.random()*4294967295).toString(16); var id = (1+Math.random()*4294967295).toString(16);
var fromi; var fromi;
var fromp; var fromp;
@ -304,7 +264,6 @@ module.exports = function(RED) {
this.doend = n.end || false; this.doend = n.end || false;
this.beserver = n.beserver; this.beserver = n.beserver;
this.name = n.name; this.name = n.name;
this.socketTimeout = parseSocketTimeout(n.socketTimeout, socketTimeout);
this.closing = false; this.closing = false;
this.connected = false; this.connected = false;
var node = this; var node = this;
@ -317,18 +276,12 @@ module.exports = function(RED) {
var setupTcpClient = function() { var setupTcpClient = function() {
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port})); node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"}); node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
var connectTimeout;
client = net.connect(node.port, node.host, function() { client = net.connect(node.port, node.host, function() {
clearTimeout(connectTimeout);
node.connected = true; node.connected = true;
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port})); node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
}); });
client.setKeepAlive(true,120000); client.setKeepAlive(true,120000);
applySocketTimeout(client, node.socketTimeout);
connectTimeout = startConnectTimeout(client, node.socketTimeout, function() {
node.log(RED._("tcpin.errors.timeout",{port:node.port}));
});
client.on('error', function (err) { client.on('error', function (err) {
node.log(RED._("tcpin.errors.error",{error:err.toString()})); node.log(RED._("tcpin.errors.error",{error:err.toString()}));
}); });
@ -336,12 +289,7 @@ module.exports = function(RED) {
node.status({}); node.status({});
node.connected = false; node.connected = false;
}); });
client.on('timeout', function() {
node.log(RED._("tcpin.errors.timeout",{port:node.port}));
client.destroy();
});
client.on('close', function() { client.on('close', function() {
clearTimeout(connectTimeout);
node.status({fill:"red",shape:"ring",text:"common.status.disconnected"}); node.status({fill:"red",shape:"ring",text:"common.status.disconnected"});
node.connected = false; node.connected = false;
client.destroy(); client.destroy();
@ -420,7 +368,6 @@ module.exports = function(RED) {
node.status({text:RED._("tcpin.status.connections",{count:0})}); node.status({text:RED._("tcpin.status.connections",{count:0})});
var server = net.createServer(function (socket) { var server = net.createServer(function (socket) {
socket.setKeepAlive(true,120000); socket.setKeepAlive(true,120000);
applySocketTimeout(socket, node.socketTimeout);
node.log(RED._("tcpin.status.connection-from",{host:socket.remoteAddress, port:socket.remotePort})); node.log(RED._("tcpin.status.connection-from",{host:socket.remoteAddress, port:socket.remotePort}));
connectedSockets.push(socket); connectedSockets.push(socket);
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})}); node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})});
@ -491,7 +438,6 @@ module.exports = function(RED) {
this.port = Number(n.port); this.port = Number(n.port);
this.out = n.out; this.out = n.out;
this.splitc = n.splitc; this.splitc = n.splitc;
this.socketTimeout = parseSocketTimeout(n.socketTimeout, socketTimeout);
if (this.out === "immed") { this.splitc = -1; this.out = "time"; } if (this.out === "immed") { this.splitc = -1; this.out = "time"; }
if (this.out !== "char") { this.splitc = Number(this.splitc); } if (this.out !== "char") { this.splitc = Number(this.splitc); }
@ -547,22 +493,13 @@ module.exports = function(RED) {
else { buf = Buffer.alloc(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully 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(); clients[connection_id].client = net.Socket();
applySocketTimeout(clients[connection_id].client, node.socketTimeout);
if (host && port) { if (host && port) {
clients[connection_id].connecting = true; clients[connection_id].connecting = true;
clients[connection_id].connectTimeout = startConnectTimeout(clients[connection_id].client, node.socketTimeout, function() {
if (clients[connection_id]) {
clients[connection_id].connected = clients[connection_id].connecting = false;
node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"});
}
});
clients[connection_id].client.connect(port, host, function() { clients[connection_id].client.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"});
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clearTimeout(clients[connection_id].connectTimeout);
clients[connection_id].connectTimeout = null;
clients[connection_id].connected = true; clients[connection_id].connected = true;
clients[connection_id].connecting = false; clients[connection_id].connecting = false;
let event; let event;
@ -678,8 +615,6 @@ module.exports = function(RED) {
clients[connection_id].client.on('close', function() { clients[connection_id].client.on('close', function() {
//console.log("CLOSE"); //console.log("CLOSE");
if (clients[connection_id]) { if (clients[connection_id]) {
clearTimeout(clients[connection_id].connectTimeout);
clients[connection_id].connectTimeout = null;
clients[connection_id].connected = clients[connection_id].connecting = false; clients[connection_id].connected = clients[connection_id].connecting = false;
} }
@ -702,23 +637,10 @@ module.exports = function(RED) {
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") + " " + connection_id, msg); node.error(RED._("tcpin.errors.connect-fail") + " " + connection_id, msg);
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clearTimeout(clients[connection_id].connectTimeout);
clients[connection_id].client.destroy(); clients[connection_id].client.destroy();
delete clients[connection_id]; delete clients[connection_id];
} }
}); });
clients[connection_id].client.on('timeout',function() {
//console.log("TIMEOUT");
if (clients[connection_id]) {
clients[connection_id].connected = clients[connection_id].connecting = 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.destroy();
}
}
});
} }
else if (!clients[connection_id].connecting && clients[connection_id].connected) { else if (!clients[connection_id].connecting && clients[connection_id].connected) {
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {