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 networkHost : 'localhost', //should resolve to 127.0.0.1 or ::1 see the table below related to this
networkPort : 8000, networkPort : 8000,
encoding : 'utf8', encoding : 'utf8',
rawBuffer : false,
silent : false, silent : false,
maxConnections : 100, maxConnections : 100,
retry : 500, 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 | | 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 | | networkPort| the default port on which TCP, TLS, or UDP sockets should connect |
| encoding | the default encoding for data sent on sockets | | 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 | | 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. | | 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. | | 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,7 +36,8 @@ ipc.connectTo(
'app.message', 'app.message',
function(data){ function(data){
ipc.log('got a message from world : '.debug, 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); this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', type.data,',', data);
if(!data) if(!data)
data=false; data=false;
this.socket.write( if(this.config.rawBuffer){
eventParser.format( data=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
{ {
type:type, type:type,
data:data data:data
} }
) );
); }
this.socket.write(data);
}; };
function connect(){ 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); client.log('connection closed'.notice ,client.id.variable , client.path.variable, client.retriesRemaining+' of '+client.config.maxRetries);
if( if(
client.config.maxRetries!==false && ( (client.config.maxRetries!==false && !client.config.stopRetrying) || client.retriesRemaining<1
client.config.stopRetrying || client.retriesRemaining<1
)
){ ){
client.log( client.log(
client.config.id.variable, client.config.id.variable,
@ -127,6 +130,14 @@ function connect(){
'data', 'data',
function(data) { function(data) {
client.log('## recieved events ##'.rainbow); client.log('## recieved events ##'.rainbow);
if(client.config.rawBuffer){
client.trigger(
'data',
new Buffer(data,this.encoding)
);
return;
}
if(!this.ipcBuffer) if(!this.ipcBuffer)
this.ipcBuffer=''; this.ipcBuffer='';

View file

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

View file

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