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

Add socketTimeout to settings.js for TCP server sockets

Fixes #125
adds an optional socketTimeout param to settings.js file to add a TCP server
socket timeout. Default is no timeout.
This commit is contained in:
Dave C-J 2013-12-24 13:12:17 +00:00
parent a9668a1999
commit 2ba5e0fe3e
2 changed files with 143 additions and 140 deletions

View File

@ -16,12 +16,11 @@
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var reconnectTime = RED.settings.socketReconnectTime||10000;
var socketTimeout = RED.settings.socketTimeout||null;
var net = require('net');
var connectionPool = {};
function TcpIn(n) {
RED.nodes.createNode(this,n);
this.host = n.host;
@ -83,7 +82,6 @@ function TcpIn(n) {
buffer = null;
}
});
client.on('close', function() {
delete connectionPool[id];
node.log("connection lost to "+node.host+":"+node.port);
@ -91,7 +89,6 @@ function TcpIn(n) {
reconnectTimeout = setTimeout(setupTcpClient, reconnectTime);
}
});
client.on('error', function(err) {
node.log(err);
});
@ -105,6 +102,7 @@ function TcpIn(n) {
});
} else {
var server = net.createServer(function (socket) {
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
var id = (1+Math.random()*4294967295).toString(16);
connectionPool[id] = socket;
@ -145,6 +143,10 @@ function TcpIn(n) {
buffer = null;
}
});
socket.on('timeout', function() {
node.log('timeout closed socket port '+node.port);
socket.end();
});
socket.on('close', function() {
delete connectionPool[id];
});
@ -163,7 +165,6 @@ function TcpIn(n) {
}
}
RED.nodes.registerType("tcp in",TcpIn);
function TcpOut(n) {
@ -187,14 +188,11 @@ function TcpOut(n) {
connected = true;
node.log("connected to "+node.host+":"+node.port);
});
client.on('error', function (err) {
node.log('error : '+err);
});
client.on('end', function (err) {
});
client.on('close', function() {
node.log("connection lost to "+node.host+":"+node.port);
connected = false;
@ -206,7 +204,6 @@ function TcpOut(n) {
}
setupTcpClient();
node.on("input", function(msg) {
if (connected && msg.payload != null) {
if (Buffer.isBuffer(msg.payload)) {
@ -243,9 +240,14 @@ function TcpOut(n) {
} else {
var connectedSockets = [];
var server = net.createServer(function (socket) {
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
var remoteDetails = socket.remoteAddress+":"+socket.remotePort;
node.log("connection from "+remoteDetails);
connectedSockets.push(socket);
socket.on('timeout', function() {
node.log('timeout closed socket port '+node.port);
socket.end();
});
socket.on('close',function() {
node.log("connection closed from "+remoteDetails);
connectedSockets.splice(connectedSockets.indexOf(socket),1);
@ -254,7 +256,6 @@ function TcpOut(n) {
node.log("socket error from "+remoteDetails);
connectedSockets.splice(connectedSockets.indexOf(socket),1);
});
});
node.on("input", function(msg) {
if (msg.payload != null) {
@ -283,5 +284,3 @@ function TcpOut(n) {
}
RED.nodes.registerType("tcp out",TcpOut);

View File

@ -31,6 +31,10 @@ module.exports = {
// Retry time in milliseconds for TCP socket connections
//socketReconnectTime: 10000,
// Timeout in milliseconds for TCP server socket connections
// defaults to no timeout
//socketTimeout: 120000,
// Maximum number of lines in debug window before pruning
debugMaxLength: 1000,