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

Merge pull request #3658 from Steve-Mcl/backport-3630

Dont delete TCP socket twice (backport Backport: #3630 to v2.x)
This commit is contained in:
Nick O'Leary 2022-06-16 11:13:17 +01:00 committed by GitHub
commit 68a2786ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -432,7 +432,7 @@ module.exports = function(RED) {
}); });
} }
else { else {
var connectedSockets = []; const connectedSockets = new Set();
node.status({text:RED._("tcpin.status.connections",{count:0})}); node.status({text:RED._("tcpin.status.connections",{count:0})});
let srv = net; let srv = net;
let connOpts; let connOpts;
@ -453,16 +453,16 @@ module.exports = function(RED) {
}); });
socket.on('close',function() { socket.on('close',function() {
node.log(RED._("tcpin.status.connection-closed",{host:socket.remoteAddress, port:socket.remotePort})); node.log(RED._("tcpin.status.connection-closed",{host:socket.remoteAddress, port:socket.remotePort}));
connectedSockets.splice(connectedSockets.indexOf(socket),1); connectedSockets.delete(socket);
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})}); node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.size})});
}); });
socket.on('error',function() { socket.on('error',function() {
node.log(RED._("tcpin.errors.socket-error",{host:socket.remoteAddress, port:socket.remotePort})); node.log(RED._("tcpin.errors.socket-error",{host:socket.remoteAddress, port:socket.remotePort}));
connectedSockets.splice(connectedSockets.indexOf(socket),1); connectedSockets.delete(socket);
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})}); node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.size})});
}); });
connectedSockets.push(socket); connectedSockets.add(socket);
node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.length})}); node.status({text:RED._("tcpin.status.connections",{count:connectedSockets.size})});
}); });
node.on("input", function(msg, nodeSend, nodeDone) { node.on("input", function(msg, nodeSend, nodeDone) {
@ -475,10 +475,10 @@ module.exports = function(RED) {
} else { } else {
buffer = Buffer.from(""+msg.payload); buffer = Buffer.from(""+msg.payload);
} }
for (var i = 0; i < connectedSockets.length; i += 1) { connectedSockets.forEach(soc => {
if (node.doend === true) { connectedSockets[i].end(buffer); } if (node.doend === true) { soc.end(buffer); }
else { connectedSockets[i].write(buffer); } else { soc.write(buffer); }
} })
} }
nodeDone(); nodeDone();
}); });
@ -495,12 +495,10 @@ module.exports = function(RED) {
} else { } else {
node.log(RED._("tcpin.status.listening-port",{port:node.port})); node.log(RED._("tcpin.status.listening-port",{port:node.port}));
node.on('close', function() { node.on('close', function() {
for (var c in connectedSockets) { connectedSockets.forEach(soc => {
if (connectedSockets.hasOwnProperty(c)) { soc.end();
connectedSockets[c].end(); soc.unref();
connectedSockets[c].unref(); })
}
}
server.close(); server.close();
node.log(RED._("tcpin.status.stopped-listening",{port:node.port})); node.log(RED._("tcpin.status.stopped-listening",{port:node.port}));
}); });