Created
August 31, 2012 03:52
-
-
Save aleafs/3548894 to your computer and use it in GitHub Desktop.
node crash when send handle to child
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | |
"use strict"; | |
var listen = function (port, onconnection) { | |
var res = new (process.binding('tcp_wrap').TCP); | |
res.onconnection = onconnection; | |
res.bind('0.0.0.0', port); | |
res.listen(128); | |
return res; | |
}; | |
var _master = (process.argv.length > 2 && 'worker' == process.argv[2]) ? false : true; | |
if (_master) { | |
var child = require('child_process').fork(__filename, ['worker']); | |
listen(3126, function (handle) { | |
console.log('Got connection'); | |
handle.close(); | |
//process.nextTick(function () { | |
child.send('request', handle); | |
//}); | |
//handle.close(); | |
//handle = null; | |
}); | |
} else { | |
var _http = require('http').createServer(function (req, res) { | |
res.end('Hello world'); | |
}); | |
process.on('message', function (msg, handle) { | |
if (!handle) { | |
return; | |
} | |
var socket = new (require('net').Socket)({ | |
'handle' : handle, | |
}); | |
socket.readable = true; | |
socket.writable = true; | |
socket.resume(); | |
socket.emit('connect'); | |
_http.emit('connection', socket); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment