Browse Source

net: improve arbitrary tcp socket support

Consider this example:

  // fd 3 is a bound tcp socket
  var s = net.createServer(cb);
  s.listen({ fd: 3 });
  console.log(s.address());  // prints null

This commit makes net.Server#address() print the actual address.

Ditto for non-listen sockets; properties like net.Socket#localAddress
and net.Socket#remoteAddress now return the correct value.

Fixes #5009.
v0.10.1-release
Ben Noordhuis 12 years ago
parent
commit
ca5022b8f1
  1. 37
      lib/net.js
  2. 10
      src/tcp_wrap.cc

37
lib/net.js

@ -42,6 +42,15 @@ function createTCP() {
}
function createHandle(fd) {
var tty = process.binding('tty_wrap');
var type = tty.guessHandleType(fd);
if (type === 'PIPE') return createPipe();
if (type === 'TCP') return createTCP();
throw new TypeError('Unsupported fd type: ' + type);
}
var debug;
if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) {
var pid = process.pid;
@ -144,7 +153,7 @@ function Socket(options) {
if (options.handle) {
this._handle = options.handle; // private
} else if (typeof options.fd !== 'undefined') {
this._handle = createPipe();
this._handle = createHandle(options.fd);
this._handle.open(options.fd);
this.readable = options.readable !== false;
this.writable = options.writable !== false;
@ -930,26 +939,18 @@ var createServerHandle = exports._createServerHandle =
var handle;
if (typeof fd === 'number' && fd >= 0) {
var tty_wrap = process.binding('tty_wrap');
var type = tty_wrap.guessHandleType(fd);
switch (type) {
case 'PIPE':
case 'TCP':
debug('listen pipe fd=' + fd);
// create a PipeWrap
handle = createPipe();
handle.open(fd);
handle.readable = true;
handle.writable = true;
break;
default:
try {
handle = createHandle(fd);
}
catch (e) {
// Not a fd we can listen on. This will trigger an error.
debug('listen invalid fd=' + fd + ' type=' + type);
debug('listen invalid fd=' + fd + ': ' + e.message);
process._errno = 'EINVAL'; // hack, callers expect that errno is set
handle = null;
break;
return null;
}
handle.open(fd);
handle.readable = true;
handle.writable = true;
return handle;
} else if (port == -1 && addressType == -1) {

10
src/tcp_wrap.cc

@ -104,6 +104,7 @@ void TCPWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
NODE_SET_PROTOTYPE_METHOD(t, "open", Open);
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
@ -256,6 +257,15 @@ Handle<Value> TCPWrap::SetSimultaneousAccepts(const Arguments& args) {
#endif
Handle<Value> TCPWrap::Open(const Arguments& args) {
HandleScope scope;
UNWRAP(TCPWrap)
int fd = args[0]->IntegerValue();
uv_tcp_open(&wrap->handle_, fd);
return Null();
}
Handle<Value> TCPWrap::Bind(const Arguments& args) {
HandleScope scope;

Loading…
Cancel
Save