diff --git a/README.md b/README.md index 84a85cf..446124e 100644 --- a/README.md +++ b/README.md @@ -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. | diff --git a/example/unixSocket/basic/hello-client.js b/example/unixSocket/basic/hello-client.js index cfdb4ef..e3fb343 100644 --- a/example/unixSocket/basic/hello-client.js +++ b/example/unixSocket/basic/hello-client.js @@ -36,7 +36,8 @@ ipc.connectTo( 'app.message', function(data){ ipc.log('got a message from world : '.debug, data); + ipc.disconnect('world'); } ); } -); \ No newline at end of file +); diff --git a/example/unixSocket/rawBuffer/hello-client.js b/example/unixSocket/rawBuffer/hello-client.js new file mode 100644 index 0000000..3f1486b --- /dev/null +++ b/example/unixSocket/rawBuffer/hello-client.js @@ -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()); + } + ); + } +); diff --git a/example/unixSocket/rawBuffer/world.server.js b/example/unixSocket/rawBuffer/world.server.js new file mode 100644 index 0000000..f8cb56a --- /dev/null +++ b/example/unixSocket/rawBuffer/world.server.js @@ -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(); diff --git a/lib/client.js b/lib/client.js index 4532425..f398f23 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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=''; diff --git a/lib/socketServer.js b/lib/socketServer.js index 842bbf8..dba221d 100644 --- a/lib/socketServer.js +++ b/lib/socketServer.js @@ -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( - { - type:type, - data:data - } - ); + 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; i0){ var e=JSON.parse(data.shift()); @@ -193,7 +212,7 @@ function init(path,config,log,port){ e.type, e.data, sock - ); + ); } } ); @@ -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 diff --git a/node-ipc.js b/node-ipc.js index 40646b0..8f7fd5d 100644 --- a/node-ipc.js +++ b/node-ipc.js @@ -30,6 +30,7 @@ var defaults={ networkPort : 8000, id : os.hostname(), encoding : 'utf8', + rawBuffer : false, silent : false, maxConnections : 100, retry : 500,