fixed rety bug added buffer support

This commit is contained in:
Brandon Nozaki Miller 2015-08-22 22:46:55 -07:00
parent c472f32cca
commit c2889c6a97
7 changed files with 149 additions and 29 deletions

View file

@ -65,6 +65,7 @@ Set these variables in the ``ipc.config`` scope to overwrite or set default valu
networkHost : 'localhost', //should resolve to 127.0.0.1 or ::1 see the table below related to this
networkPort : 8000,
encoding : 'utf8',
rawBuffer : false,
silent : false,
maxConnections : 100,
retry : 500,
@ -81,6 +82,7 @@ Set these variables in the ``ipc.config`` scope to overwrite or set default valu
| networkHost| the local or remote host on which TCP, TLS or UDP Sockets should connect |
| networkPort| the default port on which TCP, TLS, or UDP sockets should connect |
| encoding | the default encoding for data sent on sockets |
| rawBuffer| if true, data will be sent and recieved as a raw node ` Buffer ` __NOT__ an ` Object ` as JSON. |
| silent | turn on/off logging default is false which means logging is on |
| maxConnections| this is the max number of connections allowed to a socket. It is currently only being set on Unix Sockets. Other Socket types are using the system defaults. |
| retry | this is the time in milliseconds a client will wait before trying to reconnect to a server if the connection is lost. This does not effect UDP sockets since they do not have a client server relationship like Unix Sockets and TCP Sockets. |

View file

@ -36,6 +36,7 @@ ipc.connectTo(
'app.message',
function(data){
ipc.log('got a message from world : '.debug, data);
ipc.disconnect('world');
}
);
}

View file

@ -0,0 +1,35 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'hello';
ipc.config.retry= 1500;
ipc.config.rawBuffer=true;
ipc.config.encoding='ascii';
ipc.connectTo(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
ipc.of.world.emit(
'hello'
)
}
);
ipc.of.world.on(
'data',
function(data){
ipc.log('got a message from world : '.debug, data,data.toString());
}
);
}
);

View file

@ -0,0 +1,40 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
* You should start both hello and world
* then you will see them communicating.
*
* *************************************/
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.config.rawBuffer=true;
ipc.config.encoding='ascii';
ipc.serve(
function(){
ipc.server.on(
'connect',
function(socket){
ipc.server.emit(
socket,
'hello'
);
}
);
ipc.server.on(
'data',
function(data,socket){
ipc.log('got a message'.debug, data,data.toString());
ipc.server.emit(
socket,
'goodbye'
);
}
);
}
);
ipc.server.start();

View file

@ -20,14 +20,18 @@ function emit(type,data){
this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', type.data,',', data);
if(!data)
data=false;
this.socket.write(
eventParser.format(
if(this.config.rawBuffer){
data=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
{
type:type,
data:data
}
)
);
}
this.socket.write(data);
};
function connect(){
@ -81,9 +85,8 @@ function connect(){
client.log('connection closed'.notice ,client.id.variable , client.path.variable, client.retriesRemaining+' of '+client.config.maxRetries);
if(
client.config.maxRetries!==false && (
client.config.stopRetrying || client.retriesRemaining<1
)
(client.config.maxRetries!==false && !client.config.stopRetrying) || client.retriesRemaining<1
){
client.log(
client.config.id.variable,
@ -127,6 +130,14 @@ function connect(){
'data',
function(data) {
client.log('## recieved events ##'.rainbow);
if(client.config.rawBuffer){
client.trigger(
'data',
new Buffer(data,this.encoding)
);
return;
}
if(!this.ipcBuffer)
this.ipcBuffer='';

View file

@ -14,6 +14,14 @@ function emit(socket, type, data){
data:data
}
if(this.config.rawBuffer){
data=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
event
);
}
if(this.udp4 || this.udp6){
if(!socket.address || !socket.port){
@ -23,19 +31,13 @@ function emit(socket, type, data){
}
this.server.write(
eventParser.format(
event
),
data,
socket
)
);
return;
};
socket.write(
eventParser.format(
event
)
);
socket.write(data);
};
function broadcast(type,data){
@ -43,20 +45,27 @@ function broadcast(type,data){
if(!data)
data=false;
var e=eventParser.format(
{
var event={
type:type,
data:data
}
};
if(this.config.rawBuffer){
data=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
event
);
}
if(this.udp4 || this.udp6){
for(var i=1, count=this.sockets.length; i<count; i++){
this.server.write(e,this.sockets[i]);
this.server.write(data,this.sockets[i]);
}
}else{
for(var i=0, count=this.sockets.length; i<count; i++){
this.sockets[i].write(e);
this.sockets[i].write(data);
}
}
};
@ -167,6 +176,17 @@ function init(path,config,log,port){
socket.on(
'data',
function(data,UDPSocket){
var sock=((server.udp4 || server.udp6)? UDPSocket : socket);
if(server.config.rawBuffer){
data=new Buffer(data,this.encoding);
server.trigger(
'data',
data,
sock
);
return;
}
if(!this.ipcBuffer)
this.ipcBuffer='';
@ -180,7 +200,6 @@ function init(path,config,log,port){
this.ipcBuffer='';
data=eventParser.parse(data);
var sock=((server.udp4 || server.udp6)? UDPSocket : socket);
while(data.length>0){
var e=JSON.parse(data.shift());
@ -204,7 +223,14 @@ function init(path,config,log,port){
if (!rinfo)
return;
server.log('Received UDP message from '.debug, rinfo.address.variable, rinfo.port);
socket.emit('data',msg.toString(),rinfo);
var data;
if(server.config.rawSocket){
data=new Buffer(msg,this.encoding);
}else{
data=msg.toString();
}
socket.emit('data',data,rinfo);
}
);
@ -213,6 +239,10 @@ function init(path,config,log,port){
socket
);
if(server.config.rawBuffer){
return;
}
server.trigger(
'get.events.broadcasting',
socket

View file

@ -30,6 +30,7 @@ var defaults={
networkPort : 8000,
id : os.hostname(),
encoding : 'utf8',
rawBuffer : false,
silent : false,
maxConnections : 100,
retry : 500,