|
|
@ -20,6 +20,7 @@ |
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter; |
|
|
|
var net = require('net'); |
|
|
|
var Process = process.binding('process_wrap').Process; |
|
|
|
var inherits = require('util').inherits; |
|
|
|
var constants; // if (!constants) constants = process.binding('constants');
|
|
|
@ -39,8 +40,7 @@ function createPipe(ipc) { |
|
|
|
} |
|
|
|
|
|
|
|
function createSocket(pipe, readable) { |
|
|
|
var Socket = require('net').Socket; |
|
|
|
var s = new Socket({ handle: pipe }); |
|
|
|
var s = new net.Socket({ handle: pipe }); |
|
|
|
|
|
|
|
if (readable) { |
|
|
|
s.writable = false; |
|
|
@ -75,16 +75,24 @@ function setupChannel(target, channel) { |
|
|
|
|
|
|
|
channel.onread = function(pool, offset, length, recvStream) { |
|
|
|
if (pool) { |
|
|
|
for (var i = 0; i < length; i++) { |
|
|
|
if (pool[offset + i] === LF) { |
|
|
|
jsonBuffer += pool.toString('ascii', offset, offset + i); |
|
|
|
var message = JSON.parse(jsonBuffer); |
|
|
|
jsonBuffer = pool.toString('ascii', i, length); |
|
|
|
offset = i + 1; |
|
|
|
jsonBuffer += pool.toString('ascii', offset, offset + length); |
|
|
|
|
|
|
|
target.emit('message', message, recvStream); |
|
|
|
var i; |
|
|
|
while ((i = jsonBuffer.indexOf('\n')) >= 0) { |
|
|
|
var json = jsonBuffer.slice(0, i); |
|
|
|
var message = JSON.parse(json); |
|
|
|
jsonBuffer = jsonBuffer.slice(i + 1); |
|
|
|
|
|
|
|
if (recvStream) { |
|
|
|
// TODO support other types of stream.
|
|
|
|
// TODO probably need a queue of recvStreams
|
|
|
|
var server = new net.Server(); |
|
|
|
server._handle = recvStream; |
|
|
|
} |
|
|
|
|
|
|
|
target.emit('message', message, server); |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
channel.close(); |
|
|
|
target._channel = null; |
|
|
@ -94,13 +102,12 @@ function setupChannel(target, channel) { |
|
|
|
target.send = function(message, sendStream) { |
|
|
|
if (!target._channel) throw new Error("channel closed"); |
|
|
|
|
|
|
|
// Open up net.Socket instances
|
|
|
|
if (sendStream instanceof require('net').Socket || |
|
|
|
sendStream instanceof require('net').Server) { |
|
|
|
sendStream = sendStream._handle; |
|
|
|
if (!sendStream) { |
|
|
|
throw new Error("sendStream handle not yet opened"); |
|
|
|
// Open up net.Server instances
|
|
|
|
if (sendStream) { |
|
|
|
if (false == sendStream instanceof net.Server) { |
|
|
|
throw new Error("sendStream must be instance of net.Server"); |
|
|
|
} |
|
|
|
sendStream = sendStream._handle; |
|
|
|
} |
|
|
|
|
|
|
|
// For overflow protection don't write if channel queue is too deep.
|
|
|
@ -114,10 +121,10 @@ function setupChannel(target, channel) { |
|
|
|
|
|
|
|
if (!writeReq) { |
|
|
|
throw new Error(errno + " cannot write to IPC channel."); |
|
|
|
} else { |
|
|
|
writeReq.oncomplete = nop; |
|
|
|
} |
|
|
|
|
|
|
|
writeReq.oncomplete = nop; |
|
|
|
|
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|