Change API for sending handles

Does not support sending net.Server objects only raw TCPWrap objects.
This commit is contained in:
Ryan Dahl 2011-10-07 16:43:53 -07:00
parent a23d8ad313
commit 12486a6437
4 changed files with 68 additions and 72 deletions

View file

@ -73,7 +73,7 @@ function setupChannel(target, channel) {
var jsonBuffer = ''; var jsonBuffer = '';
channel.onread = function(pool, offset, length, recvStream) { channel.onread = function(pool, offset, length, recvHandle) {
if (pool) { if (pool) {
jsonBuffer += pool.toString('ascii', offset, offset + length); jsonBuffer += pool.toString('ascii', offset, offset + length);
@ -83,14 +83,7 @@ function setupChannel(target, channel) {
var message = JSON.parse(json); var message = JSON.parse(json);
jsonBuffer = jsonBuffer.slice(i + 1); jsonBuffer = jsonBuffer.slice(i + 1);
if (recvStream) { target.emit('message', message, recvHandle);
// 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 { } else {
@ -99,17 +92,9 @@ function setupChannel(target, channel) {
} }
}; };
target.send = function(message, sendStream) { target.send = function(message, sendHandle) {
if (!target._channel) throw new Error("channel closed"); if (!target._channel) throw new Error("channel closed");
// 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. // For overflow protection don't write if channel queue is too deep.
if (channel.writeQueueSize > 1024 * 1024) { if (channel.writeQueueSize > 1024 * 1024) {
return false; return false;
@ -117,7 +102,7 @@ function setupChannel(target, channel) {
var buffer = Buffer(JSON.stringify(message) + '\n'); var buffer = Buffer(JSON.stringify(message) + '\n');
var writeReq = channel.write(buffer, 0, buffer.length, sendStream); var writeReq = channel.write(buffer, 0, buffer.length, sendHandle);
if (!writeReq) { if (!writeReq) {
throw new Error(errno + " cannot write to IPC channel."); throw new Error(errno + " cannot write to IPC channel.");

View file

@ -59,7 +59,7 @@ exports.connect = exports.createConnection = function(port /* [host], [cb] */) {
/* called when creating new Socket, or when re-using a closed Socket */ /* called when creating new Socket, or when re-using a closed Socket */
function initSocketHandle(self) { function initSocketHandle(self) {
self._pendingWriteReqs = 0; self._writeRequests = [];
self._flags = 0; self._flags = 0;
self._connectQueueSize = 0; self._connectQueueSize = 0;
@ -237,7 +237,7 @@ Socket.prototype.destroySoon = function() {
this.writable = false; this.writable = false;
this._flags |= FLAG_DESTROY_SOON; this._flags |= FLAG_DESTROY_SOON;
if (this._pendingWriteReqs == 0) { if (this._writeRequests.length == 0) {
this.destroy(); this.destroy();
} }
}; };
@ -342,42 +342,29 @@ Socket.prototype.setEncoding = function(encoding) {
}; };
Socket.prototype._getpeername = function() { Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
if (!this._handle || !this._handle.getpeername) { var encoding, fd, cb;
return {};
}
if (!this._peername) {
this._peername = this._handle.getpeername();
}
return this._peername;
};
Socket.prototype.__defineGetter__('remoteAddress', function() {
return this._getpeername().address;
});
Socket.prototype.__defineGetter__('remotePort', function() {
return this._getpeername().port;
});
/*
* Arguments data, [encoding], [cb]
*/
Socket.prototype.write = function(data, arg1, arg2) {
var encoding, cb;
// parse arguments // parse arguments
if (arg1) { if (typeof arguments[3] == 'function') {
if (typeof arg1 === 'string') { cb = arguments[3];
encoding = arg1; fd = arguments[2];
cb = arg2; encoding = arguments[1];
} else if (typeof arg1 === 'function') { } else if (typeof arguments[2] == 'function') {
cb = arg1; cb = arguments[2];
if (typeof arguments[1] == 'number') {
fd = arguments[1];
} else { } else {
throw new Error("bad arg"); encoding = arguments[1];
}
} else if (typeof arguments[1] == 'function') {
cb = arguments[1];
} else {
if (typeof arguments[1] == 'number') {
fd = arguments[1];
} else {
encoding = arguments[1];
fd = arguments[2];
} }
} }
@ -392,9 +379,9 @@ Socket.prototype.write = function(data, arg1, arg2) {
if (this._connecting) { if (this._connecting) {
this._connectQueueSize += data.length; this._connectQueueSize += data.length;
if (this._connectQueue) { if (this._connectQueue) {
this._connectQueue.push([data, encoding, cb]); this._connectQueue.push([data, encoding, fd, cb]);
} else { } else {
this._connectQueue = [[data, encoding, cb]]; this._connectQueue = [[data, encoding, fd, cb]];
} }
return false; return false;
} }
@ -408,7 +395,7 @@ Socket.prototype.write = function(data, arg1, arg2) {
writeReq.oncomplete = afterWrite; writeReq.oncomplete = afterWrite;
writeReq.cb = cb; writeReq.cb = cb;
this._pendingWriteReqs++; this._writeRequests.push(writeReq);
return this._handle.writeQueueSize == 0; return this._handle.writeQueueSize == 0;
}; };
@ -423,9 +410,10 @@ function afterWrite(status, handle, req, buffer) {
} }
// TODO check status. // TODO check status.
self._pendingWriteReqs--; var req_ = self._writeRequests.shift();
assert.equal(req, req_);
if (self._pendingWriteReqs == 0) { if (self._writeRequests.length == 0) {
// TODO remove all uses of ondrain - this is not a good hack. // TODO remove all uses of ondrain - this is not a good hack.
if (self.ondrain) self.ondrain(); if (self.ondrain) self.ondrain();
self.emit('drain'); self.emit('drain');
@ -433,7 +421,7 @@ function afterWrite(status, handle, req, buffer) {
if (req.cb) req.cb(); if (req.cb) req.cb();
if (self._pendingWriteReqs == 0 && self._flags & FLAG_DESTROY_SOON) { if (self._writeRequests.length == 0 && self._flags & FLAG_DESTROY_SOON) {
self.destroy(); self.destroy();
} }
} }
@ -679,11 +667,17 @@ Server.prototype.listen = function() {
var port = toPort(arguments[0]); var port = toPort(arguments[0]);
var TCP = process.binding('tcp_wrap').TCP
if (arguments.length == 0 || typeof arguments[0] == 'function') { if (arguments.length == 0 || typeof arguments[0] == 'function') {
// Don't bind(). OS will assign a port with INADDR_ANY. // Don't bind(). OS will assign a port with INADDR_ANY.
// The port can be found with server.address() // The port can be found with server.address()
listen(self, null, null); listen(self, null, null);
} else if (arguments[0] instanceof TCP) {
self._handle = arguments[0];
listen(self, null, -1, -1);
} else if (isPipeName(arguments[0])) { } else if (isPipeName(arguments[0])) {
// UNIX socket or Windows pipe. // UNIX socket or Windows pipe.
listen(self, arguments[0], -1, -1); listen(self, arguments[0], -1, -1);
@ -712,6 +706,7 @@ Server.prototype.address = function() {
function onconnection(clientHandle) { function onconnection(clientHandle) {
var handle = this; var handle = this;
var self = handle.socket; var self = handle.socket;
var peername;
debug('onconnection'); debug('onconnection');
@ -725,12 +720,29 @@ function onconnection(clientHandle) {
return; return;
} }
// Todo: implement this for unix sockets
if (clientHandle.getpeername) {
peername = clientHandle.getpeername();
if (!peername.address || !peername.port) {
var err = errnoException(errno, 'accept');
clientHandle.close();
self.emit('error', err);
return;
}
}
var socket = new Socket({ var socket = new Socket({
handle: clientHandle, handle: clientHandle,
allowHalfOpen: self.allowHalfOpen allowHalfOpen: self.allowHalfOpen
}); });
socket.readable = socket.writable = true; socket.readable = socket.writable = true;
if (peername) {
socket.remoteAddress = peername.address;
socket.remotePort = peername.port;
// TODO: set family as well
}
socket.resume(); socket.resume();
self.connections++; self.connections++;

View file

@ -3,25 +3,24 @@ var net = require('net');
var connections = 0; var connections = 0;
process.on('message', function(m, server) { process.on('message', function(m, serverHandle) {
console.log('CHILD got message:', m); console.log('CHILD got message:', m);
assert.ok(m.hello); assert.ok(m.hello);
assert.ok(server); assert.ok(serverHandle);
assert.ok(server instanceof net.Server);
// TODO need better API for this. var server = new net.Server(function(c) {
server._backlog = 9;
server.listen(function() {
process.send({ gotHandle: true });
});
server.on('connection', function(c) {
connections++; connections++;
console.log('CHILD got connection'); console.log('CHILD got connection');
c.destroy(); c.destroy();
process.send({ childConnections: connections }); process.send({ childConnections: connections });
}); });
// TODO need better API for this.
server._backlog = 9;
server.listen(serverHandle, function() {
process.send({ gotHandle: true });
});
}); });

View file

@ -20,7 +20,7 @@ server._backlog = 9;
server.listen(common.PORT, function() { server.listen(common.PORT, function() {
console.log('PARENT send child server handle'); console.log('PARENT send child server handle');
n.send({ hello: 'world' }, server); n.send({ hello: 'world' }, server._handle);
}); });
function makeConnections() { function makeConnections() {