|
@ -85,6 +85,7 @@ function setupChannel(target, channel) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
channel.buffering = false; |
|
|
channel.onread = function(pool, offset, length, recvHandle) { |
|
|
channel.onread = function(pool, offset, length, recvHandle) { |
|
|
if (recvHandle && setSimultaneousAccepts) { |
|
|
if (recvHandle && setSimultaneousAccepts) { |
|
|
// Update simultaneous accepts on Windows
|
|
|
// Update simultaneous accepts on Windows
|
|
@ -117,10 +118,11 @@ function setupChannel(target, channel) { |
|
|
start = i + 1; |
|
|
start = i + 1; |
|
|
} |
|
|
} |
|
|
jsonBuffer = jsonBuffer.slice(start); |
|
|
jsonBuffer = jsonBuffer.slice(start); |
|
|
|
|
|
this.buffering = jsonBuffer.length !== 0; |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
channel.close(); |
|
|
this.buffering = false; |
|
|
target._channel = null; |
|
|
target.disconnect(); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -129,7 +131,7 @@ function setupChannel(target, channel) { |
|
|
throw new TypeError('message cannot be undefined'); |
|
|
throw new TypeError('message cannot be undefined'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!target._channel) throw new Error("channel closed"); |
|
|
if (!this.connected) throw new Error("channel closed"); |
|
|
|
|
|
|
|
|
// For overflow protection don't write if channel queue is too deep.
|
|
|
// For overflow protection don't write if channel queue is too deep.
|
|
|
if (channel.writeQueueSize > 1024 * 1024) { |
|
|
if (channel.writeQueueSize > 1024 * 1024) { |
|
@ -154,6 +156,34 @@ function setupChannel(target, channel) { |
|
|
return true; |
|
|
return true; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
target.connected = true; |
|
|
|
|
|
target.disconnect = function() { |
|
|
|
|
|
if (!this.connected) return; |
|
|
|
|
|
|
|
|
|
|
|
// do not allow messages to be written
|
|
|
|
|
|
this.connected = false; |
|
|
|
|
|
this._channel = null; |
|
|
|
|
|
|
|
|
|
|
|
var fired = false; |
|
|
|
|
|
function finish() { |
|
|
|
|
|
if (fired) return; |
|
|
|
|
|
fired = true; |
|
|
|
|
|
|
|
|
|
|
|
channel.close(); |
|
|
|
|
|
target.emit('disconnect'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If a message is being read, then wait for it to complete.
|
|
|
|
|
|
if (channel.buffering) { |
|
|
|
|
|
this.once('message', finish); |
|
|
|
|
|
this.once('internalMessage', finish); |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
finish(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
channel.readStart(); |
|
|
channel.readStart(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -201,11 +231,8 @@ exports.fork = function(modulePath /*, args, options*/) { |
|
|
|
|
|
|
|
|
if (!options.thread) setupChannel(child, options.stdinStream); |
|
|
if (!options.thread) setupChannel(child, options.stdinStream); |
|
|
|
|
|
|
|
|
child.on('exit', function() { |
|
|
// Disconnect when the child process exits.
|
|
|
if (child._channel) { |
|
|
child.once('exit', child.disconnect.bind(child)); |
|
|
child._channel.close(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return child; |
|
|
return child; |
|
|
}; |
|
|
}; |
|
|