Skip to content

Commit

Permalink
unpack net / js bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 18, 2011
1 parent 8e2fd7d commit 6a65a46
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/master.js
Expand Up @@ -11,11 +11,16 @@

var Worker = require('./worker')
, EventEmitter = require('events').EventEmitter
, binding = process.binding('net')
, dirname = require('path').dirname
, spawn = require('child_process').spawn
, utils = require('./utils')
, fsBinding = process.binding('fs')
, netBinding = process.binding('net')
, bind = netBinding.bind
, listen = netBinding.listen
, socket = netBinding.socket
, socketpair = netBinding.socketpair
, close = netBinding.close
, unlink = fsBinding.unlink
, ENOENT = process.version >= 'v0.4.0'
? require('constants').ENOENT
Expand All @@ -39,7 +44,7 @@ net.Socket = net.Stream;

// COMPAT:

if (!net.isIP) net.isIP = binding.isIP;
if (!net.isIP) net.isIP = netBinding.isIP;

/**
* Node binary.
Expand Down Expand Up @@ -459,21 +464,21 @@ Master.prototype.createSocket = function(fn){
if (this.host) {
// ip
if (ipv = net.isIP(this.host)) {
fn(null, binding.socket('tcp' + ipv));
fn(null, socket('tcp' + ipv));
// lookup
} else {
require('dns').lookup(this.host, function(err, ip, ipv){
if (err) return fn(err);
self.host = ip;
fn(null, binding.socket('tcp' + ipv));
fn(null, socket('tcp' + ipv));
});
}
// local socket
} else if ('string' == typeof this.port) {
fn(null, binding.socket('unix'));
fn(null, socket('unix'));
// only port
} else if (this.port) {
fn(null, binding.socket('tcp4'));
fn(null, socket('tcp4'));
}
};

Expand Down Expand Up @@ -555,8 +560,8 @@ Master.prototype.removeWorker = function(id){
var worker = this.children[id];
if (!worker) return;
if (worker.fds) {
binding.close(worker.fds[0]);
binding.close(worker.fds[1]);
close(worker.fds[0]);
close(worker.fds[1]);
}
delete this.children[id];
};
Expand Down Expand Up @@ -701,7 +706,7 @@ Master.prototype.restart = function(sig){
*/

Master.prototype.spawnMaster = function(){
var fds = binding.socketpair()
var fds = socketpair()
, customFds = [fds[0], 1, 2]
, env = {};

Expand Down Expand Up @@ -783,7 +788,7 @@ Master.prototype._destroy = function(){
var self = this;
unlink(this.clientSocketPath);
this._server.on('close', function(){
if (self.fd) binding.close(self.fd);
if (self.fd) close(self.fd);
self.emit('close');
process.nextTick(process.exit.bind(process));
}).close();
Expand Down Expand Up @@ -815,18 +820,18 @@ Master.prototype.startListening = function(bind){
if ('string' == typeof this.port && bind) {
fs.unlink(this.port, function(err){
if (ENOENT != err.errno) throw err;
listen();
startListening();
});
} else {
listen();
startListening();
}

// bind / listen
function listen() {
function startListening() {
if (bind) {
try {
binding.bind(self.fd, self.port, self.host);
binding.listen(self.fd, self.options.backlog);
bind(self.fd, self.port, self.host);
listen(self.fd, self.options.backlog);
} catch(e) {
self.kill('SIGKILL');
throw e;
Expand Down

0 comments on commit 6a65a46

Please sign in to comment.