|
|
@ -284,6 +284,26 @@ function afterWrite(status, handle, req, buffer) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function connectip(self, port, ip) { |
|
|
|
self.remoteAddress = ip; |
|
|
|
self.remotePort = port; |
|
|
|
|
|
|
|
// TODO return promise from Socket.prototype.connect which
|
|
|
|
// wraps _connectReq.
|
|
|
|
|
|
|
|
assert.ok(!self._connecting); |
|
|
|
|
|
|
|
var connectReq = self._handle.connect(ip, port); |
|
|
|
|
|
|
|
if (connectReq) { |
|
|
|
self._connecting = true; |
|
|
|
connectReq.oncomplete = afterConnect; |
|
|
|
} else { |
|
|
|
self.destroy(errnoException(errno, 'connect')); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.connect = function(port, host /* [cb] */) { |
|
|
|
var self = this; |
|
|
|
|
|
|
@ -293,36 +313,23 @@ Socket.prototype.connect = function(port, host /* [cb] */) { |
|
|
|
|
|
|
|
timers.active(this); |
|
|
|
|
|
|
|
require('dns').lookup(host, function(err, ip, addressType) { |
|
|
|
if (err) { |
|
|
|
self.emit('error', err); |
|
|
|
} else { |
|
|
|
timers.active(self); |
|
|
|
|
|
|
|
if (addressType != 4) { |
|
|
|
throw new Error("ipv6 addresses not yet supported by libuv"); |
|
|
|
} |
|
|
|
|
|
|
|
ip = ip || '127.0.0.1'; |
|
|
|
|
|
|
|
self.remoteAddress = ip; |
|
|
|
self.remotePort = port; |
|
|
|
|
|
|
|
// TODO retrun promise from Socket.prototype.connect which
|
|
|
|
// wraps _connectReq.
|
|
|
|
if (typeof host == 'undefined') { |
|
|
|
connectip(self, port, '127.0.0.1'); |
|
|
|
} else { |
|
|
|
require('dns').lookup(host, function(err, ip, addressType) { |
|
|
|
if (err) { |
|
|
|
self.emit('error', err); |
|
|
|
} else { |
|
|
|
timers.active(self); |
|
|
|
|
|
|
|
assert.ok(!self._connecting); |
|
|
|
|
|
|
|
var connectReq = self._handle.connect(ip, port); |
|
|
|
if (addressType != 4) { |
|
|
|
throw new Error("ipv6 addresses not yet supported by libuv"); |
|
|
|
} |
|
|
|
|
|
|
|
if (connectReq) { |
|
|
|
self._connecting = true; |
|
|
|
connectReq.oncomplete = afterConnect; |
|
|
|
} else { |
|
|
|
self.destroy(errnoException(errno, 'connect')); |
|
|
|
connectip(self, port, ip || '127.0.0.1'); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -391,6 +398,16 @@ exports.Server = Server; |
|
|
|
function toPort(x) { return (x = Number(x)) >= 0 ? x : false; } |
|
|
|
|
|
|
|
|
|
|
|
function listenip(self, ip, port) { |
|
|
|
var r = self._handle.bind(ip, port); |
|
|
|
if (r) { |
|
|
|
self.emit('error', errnoException(errno, 'listen')); |
|
|
|
} else { |
|
|
|
self._handle.listen(self._backlog || 128); |
|
|
|
self.emit('listening'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Server.prototype.listen = function() { |
|
|
|
var self = this; |
|
|
|
|
|
|
@ -406,8 +423,13 @@ Server.prototype.listen = function() { |
|
|
|
// The port can be found with server.address()
|
|
|
|
this._handle.listen(self._backlog || 128); |
|
|
|
this.emit('listening'); |
|
|
|
|
|
|
|
} else if (typeof arguments[1] == 'undefined') { |
|
|
|
// The first argument is the port, no IP given.
|
|
|
|
listenip(self, '0.0.0.0', port); |
|
|
|
|
|
|
|
} else { |
|
|
|
// the first argument is the port, the second an IP
|
|
|
|
// The first argument is the port, the second an IP
|
|
|
|
require('dns').lookup(arguments[1], function(err, ip, addressType) { |
|
|
|
if (err) { |
|
|
|
self.emit('error', err); |
|
|
@ -415,14 +437,7 @@ Server.prototype.listen = function() { |
|
|
|
if (addressType != 4) { |
|
|
|
throw new Error("ipv6 addresses not yet supported by libuv"); |
|
|
|
} |
|
|
|
|
|
|
|
var r = self._handle.bind(ip || '0.0.0.0', port); |
|
|
|
if (r) { |
|
|
|
self.emit('error', errnoException(errno, 'listen')); |
|
|
|
} else { |
|
|
|
self._handle.listen(self._backlog || 128); |
|
|
|
self.emit('listening'); |
|
|
|
} |
|
|
|
listenip(self, ip || '0.0.0.0', port); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|