2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2014-05-13 12:39:59 +02:00
|
|
|
* Copyright 2013,2014 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) {
|
2014-05-14 12:44:19 +02:00
|
|
|
"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-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
var connectionPool = {};
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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;
|
2014-09-03 21:06:29 +02:00
|
|
|
var count = 0;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!node.server) {
|
|
|
|
var buffer = null;
|
|
|
|
var client;
|
|
|
|
var reconnectTimeout;
|
2014-09-03 21:06:29 +02:00
|
|
|
var end = false;
|
2014-05-14 12:44:19 +02:00
|
|
|
var setupTcpClient = function() {
|
2014-05-04 00:32:04 +02:00
|
|
|
node.log("connecting to "+node.host+":"+node.port);
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"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;
|
2014-05-04 00:32:04 +02:00
|
|
|
node.log("connected to "+node.host+":"+node.port);
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
connectionPool[id] = client;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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) {
|
|
|
|
if ((node.datatype) === "utf8" && node.newline != "") {
|
|
|
|
buffer = buffer+data;
|
|
|
|
var parts = buffer.split(node.newline);
|
|
|
|
for (var i = 0;i<parts.length-1;i+=1) {
|
|
|
|
var msg = {topic:node.topic, payload:parts[i]};
|
|
|
|
msg._session = {type:"tcp",id:id};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
buffer = parts[parts.length-1];
|
|
|
|
} else {
|
|
|
|
var msg = {topic:node.topic, payload:data};
|
2013-12-19 22:16:25 +01:00
|
|
|
msg._session = {type:"tcp",id:id};
|
2013-11-14 15:39:26 +01:00
|
|
|
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};
|
2013-12-19 22:16:25 +01:00
|
|
|
msg._session = {type:"tcp",id:id};
|
2014-09-03 21:06:29 +02:00
|
|
|
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;
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
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;
|
2014-05-12 17:32:19 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"disconnected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!node.closing) {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (end) { // if we were asked to close then try to reconnect once very quick.
|
|
|
|
end = false;
|
|
|
|
reconnectTimeout = setTimeout(setupTcpClient, 20);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.log("connection lost to "+node.host+":"+node.port);
|
|
|
|
reconnectTimeout = setTimeout(setupTcpClient, reconnectTime);
|
|
|
|
}
|
2015-01-29 22:42:56 +01:00
|
|
|
} else {
|
|
|
|
if (node.done) { node.done(); }
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('error', function(err) {
|
2014-05-12 17:32:19 +02:00
|
|
|
node.log(err);
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
setupTcpClient();
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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;
|
|
|
|
client.end();
|
|
|
|
clearTimeout(reconnectTimeout);
|
2015-01-29 22:42:56 +01:00
|
|
|
if (!node.connected) { done(); }
|
2013-11-14 15:39:26 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
var server = net.createServer(function (socket) {
|
|
|
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
|
|
|
var id = (1+Math.random()*4294967295).toString(16);
|
|
|
|
connectionPool[id] = socket;
|
2014-09-03 21:06:29 +02:00
|
|
|
node.status({text:++count+" connections"});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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) {
|
|
|
|
if ((typeof data) === "string" && node.newline != "") {
|
|
|
|
buffer = buffer+data;
|
|
|
|
var parts = buffer.split(node.newline);
|
|
|
|
for (var i = 0;i<parts.length-1;i+=1) {
|
|
|
|
var msg = {topic:node.topic, payload:parts[i],ip:socket.remoteAddress,port:socket.remotePort};
|
|
|
|
msg._session = {type:"tcp",id:id};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
buffer = parts[parts.length-1];
|
|
|
|
} else {
|
|
|
|
var msg = {topic:node.topic, payload:data};
|
2013-12-19 22:16:25 +01:00
|
|
|
msg._session = {type:"tcp",id:id};
|
2013-11-14 15:39:26 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
2013-12-24 14:12:17 +01:00
|
|
|
} 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() {
|
2014-09-03 21:06:29 +02:00
|
|
|
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;
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-03-17 17:09:07 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
socket.on('timeout', function() {
|
|
|
|
node.log('timeout closed socket port '+node.port);
|
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
socket.on('close', function() {
|
|
|
|
delete connectionPool[id];
|
2014-09-03 21:06:29 +02:00
|
|
|
node.status({text:--count+" connections"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
socket.on('error',function(err) {
|
|
|
|
node.log(err);
|
|
|
|
});
|
2013-11-14 15:39:26 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on('error', function(err) {
|
|
|
|
if (err) {
|
|
|
|
node.error('unable to listen on port '+node.port+' : '+err);
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2013-11-14 15:39:26 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
server.listen(node.port, function(err) {
|
|
|
|
if (err) {
|
|
|
|
node.error('unable to listen on port '+node.port+' : '+err);
|
2013-12-24 14:12:17 +01:00
|
|
|
} else {
|
2014-05-04 00:32:04 +02:00
|
|
|
node.log('listening on port '+node.port);
|
|
|
|
node.on('close', function() {
|
2014-10-25 18:52:24 +02:00
|
|
|
for (var c in connectionPool) {
|
|
|
|
connectionPool[c].end();
|
|
|
|
connectionPool[c].unref();
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
node.closing = true;
|
|
|
|
server.close();
|
|
|
|
node.log('stopped listening on port '+node.port);
|
|
|
|
});
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("tcp in",TcpIn);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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;
|
2014-09-03 21:06:29 +02:00
|
|
|
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-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!node.beserver||node.beserver=="client") {
|
|
|
|
var reconnectTimeout;
|
|
|
|
var client = null;
|
2014-09-03 21:06:29 +02:00
|
|
|
var end = false;
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-14 12:44:19 +02:00
|
|
|
var setupTcpClient = function() {
|
2014-05-04 00:32:04 +02:00
|
|
|
node.log("connecting to "+node.host+":"+node.port);
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"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;
|
2014-05-04 00:32:04 +02:00
|
|
|
node.log("connected to "+node.host+":"+node.port);
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
client.on('error', function (err) {
|
|
|
|
node.log('error : '+err);
|
|
|
|
});
|
|
|
|
client.on('end', function (err) {
|
|
|
|
});
|
|
|
|
client.on('close', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"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) {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (end) {
|
|
|
|
end = false;
|
|
|
|
reconnectTimeout = setTimeout(setupTcpClient,20);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.log("connection lost to "+node.host+":"+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
|
|
|
}
|
|
|
|
});
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
setupTcpClient();
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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) {
|
2013-12-19 22:16:25 +01:00
|
|
|
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-09-03 21:06:29 +02:00
|
|
|
if (node.doend === true) {
|
|
|
|
end = true;
|
|
|
|
client.end();
|
|
|
|
}
|
2013-12-19 22:16:25 +01:00
|
|
|
}
|
2013-12-24 14:12:17 +01:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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;
|
|
|
|
client.end();
|
|
|
|
clearTimeout(reconnectTimeout);
|
2015-01-29 22:42:56 +01:00
|
|
|
if (!node.connected) { done(); }
|
2013-12-24 14:12:17 +01:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 14:12:17 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
var connectedSockets = [];
|
2014-09-03 21:06:29 +02:00
|
|
|
node.status({text:"0 connections"});
|
2014-05-04 00:32:04 +02:00
|
|
|
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);
|
2014-09-03 21:06:29 +02:00
|
|
|
node.status({text:connectedSockets.length+" connections"});
|
2014-05-04 00:32:04 +02:00
|
|
|
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);
|
2014-09-03 21:06:29 +02:00
|
|
|
node.status({text:connectedSockets.length+" connections"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
socket.on('error',function() {
|
|
|
|
node.log("socket error from "+remoteDetails);
|
|
|
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
2014-09-03 21:06:29 +02:00
|
|
|
node.status({text:connectedSockets.length+" connections"});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
});
|
2014-09-03 21:06:29 +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) {
|
2014-09-03 21:06:29 +02:00
|
|
|
if (node.doend === true) { connectedSockets[i].end(buffer); }
|
|
|
|
else { connectedSockets[i].write(buffer); }
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on('error', function(err) {
|
|
|
|
if (err) {
|
|
|
|
node.error('unable to listen on port '+node.port+' : '+err);
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.listen(node.port, function(err) {
|
|
|
|
if (err) {
|
|
|
|
node.error('unable to listen on port '+node.port+' : '+err);
|
|
|
|
} else {
|
|
|
|
node.log('listening on port '+node.port);
|
|
|
|
node.on('close', function() {
|
2014-10-25 18:52:24 +02:00
|
|
|
for (var c in connectedSockets) {
|
|
|
|
connectedSockets[c].end();
|
|
|
|
connectedSockets[c].unref();
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
server.close();
|
|
|
|
node.log('stopped listening on port '+node.port);
|
|
|
|
});
|
2013-12-24 14:12:17 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
2013-11-14 15:39:26 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("tcp out",TcpOut);
|
2014-07-08 14:45:00 +02:00
|
|
|
|
|
|
|
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.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0"); }
|
|
|
|
|
|
|
|
var buf;
|
|
|
|
if (this.out == "count") { buf = new Buffer(this.splitc); }
|
|
|
|
else { buf = new Buffer(32768); } // set it to 32k... hopefully big enough for most.... but only hopefully
|
|
|
|
|
2014-09-03 21:06:29 +02:00
|
|
|
this.connected = false;
|
2014-07-08 14:45:00 +02:00
|
|
|
var node = this;
|
|
|
|
var client;
|
|
|
|
|
|
|
|
this.on("input", function(msg) {
|
|
|
|
var i = 0;
|
2014-09-03 21:06:29 +02:00
|
|
|
if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) {
|
2014-07-08 14:45:00 +02:00
|
|
|
msg.payload = msg.payload.toString();
|
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
if (!node.connected) {
|
|
|
|
client = net.Socket();
|
|
|
|
client.setTimeout(socketTimeout);
|
|
|
|
node.status({});
|
2014-11-06 11:21:14 +01:00
|
|
|
var host = node.server || msg.host;
|
|
|
|
var port = node.port || msg.port;
|
|
|
|
if (host && port) {
|
|
|
|
client.connect(port, host, function() {
|
|
|
|
//node.log('client connected');
|
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
|
|
|
node.connected = true;
|
|
|
|
client.write(msg.payload);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("Host and/or port not set");
|
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
|
2014-09-03 21:06:29 +02:00
|
|
|
client.on('data', function(data) {
|
|
|
|
//node.log("data:"+ data.length+":"+ data);
|
|
|
|
if (node.splitc === 0) {
|
|
|
|
node.send({"payload": data});
|
|
|
|
}
|
|
|
|
else if (node.out === "sit") { // if we are staying connected just send the buffer
|
|
|
|
node.send({"payload": data});
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
var m = new Buffer(i+1);
|
|
|
|
buf.copy(m,0,0,i+1);
|
|
|
|
node.send({"payload": m});
|
|
|
|
client.end();
|
|
|
|
m = null;
|
|
|
|
}, node.splitc);
|
|
|
|
i = 0;
|
|
|
|
buf[0] = data[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// count bytes into a buffer...
|
|
|
|
else if (node.out == "count") {
|
2014-07-08 14:45:00 +02:00
|
|
|
buf[i] = data[j];
|
2014-09-03 21:06:29 +02:00
|
|
|
i += 1;
|
|
|
|
if ( i >= node.serialConfig.count) {
|
|
|
|
node.send({"payload": buf});
|
|
|
|
client.end();
|
|
|
|
i = 0;
|
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
// look for a char
|
2014-07-08 14:45:00 +02:00
|
|
|
else {
|
2014-09-03 21:06:29 +02:00
|
|
|
buf[i] = data[j];
|
|
|
|
i += 1;
|
|
|
|
if (data[j] == node.splitc) {
|
|
|
|
var m = new Buffer(i);
|
|
|
|
buf.copy(m,0,0,i);
|
2014-07-08 14:45:00 +02:00
|
|
|
node.send({"payload": m});
|
|
|
|
client.end();
|
|
|
|
m = null;
|
2014-09-03 21:06:29 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
2014-07-08 14:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 21:06:29 +02:00
|
|
|
});
|
2014-07-08 14:45:00 +02:00
|
|
|
|
2014-09-03 21:06:29 +02:00
|
|
|
client.on('end', function() {
|
|
|
|
//node.log('client disconnected');
|
|
|
|
node.connected = false;
|
|
|
|
node.status({});
|
|
|
|
client = null;
|
|
|
|
});
|
2014-07-08 14:45:00 +02:00
|
|
|
|
2015-01-29 22:42:56 +01:00
|
|
|
client.on('close', function() {
|
|
|
|
if (node.done) { node.done(); }
|
|
|
|
});
|
|
|
|
|
2014-09-03 21:06:29 +02:00
|
|
|
client.on('error', function() {
|
|
|
|
node.log('connect failed');
|
|
|
|
node.status({fill:"red",shape:"ring",text:"error"});
|
|
|
|
if (client) { client.end(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('timeout',function() {
|
|
|
|
node.log('connect timeout');
|
|
|
|
if (client) {
|
|
|
|
client.end();
|
|
|
|
setTimeout(function() {
|
2014-11-06 11:21:14 +01:00
|
|
|
client.connect(port, host, function() {
|
2014-09-03 21:06:29 +02:00
|
|
|
//node.log('client connected');
|
|
|
|
node.connected = true;
|
|
|
|
client.write(msg.payload);
|
|
|
|
});
|
|
|
|
},reconnectTime);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else { client.write(msg.payload); }
|
2014-07-08 14:45:00 +02:00
|
|
|
});
|
|
|
|
|
2015-01-29 22:42:56 +01:00
|
|
|
this.on("close", function(done) {
|
|
|
|
node.done = done;
|
|
|
|
if (client) {
|
|
|
|
buf = null;
|
|
|
|
client.end();
|
|
|
|
}
|
|
|
|
if (!node.connected) { done(); }
|
2014-07-08 14:45:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("tcp request",TcpGet);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|