@ -135,10 +135,9 @@ function replaceHandle(self, newHandle) {
}
Socket . prototype . bind = function ( port_ /*, address, callback*/ ) {
var self = this ;
let port = port_ ;
self . _ healthCheck ( ) ;
this . _ healthCheck ( ) ;
if ( this . _ bindState != BIND_STATE_UNBOUND )
throw new Error ( 'Socket is already bound' ) ;
@ -146,12 +145,12 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
this . _ bindState = BIND_STATE_BINDING ;
if ( typeof arguments [ arguments . length - 1 ] === 'function' )
self . once ( 'listening' , arguments [ arguments . length - 1 ] ) ;
this . once ( 'listening' , arguments [ arguments . length - 1 ] ) ;
if ( port instanceof UDP ) {
replaceHandle ( self , port ) ;
startListening ( self ) ;
return self ;
replaceHandle ( this , port ) ;
startListening ( this ) ;
return this ;
}
var address ;
@ -167,17 +166,17 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
}
// defaulting address for bind to all interfaces
if ( ! address && self . _ handle . lookup === lookup4 ) {
if ( ! address && this . _ handle . lookup === lookup4 ) {
address = '0.0.0.0' ;
} else if ( ! address && self . _ handle . lookup === lookup6 ) {
} else if ( ! address && this . _ handle . lookup === lookup6 ) {
address = '::' ;
}
// resolve address first
self . _ handle . lookup ( address , function ( err , ip ) {
this . _ handle . lookup ( address , ( err , ip ) => {
if ( err ) {
self . _ bindState = BIND_STATE_UNBOUND ;
self . emit ( 'error' , err ) ;
this . _ bindState = BIND_STATE_UNBOUND ;
this . emit ( 'error' , err ) ;
return ;
}
@ -185,51 +184,50 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
cluster = require ( 'cluster' ) ;
var flags = 0 ;
if ( self . _ reuseAddr )
if ( this . _ reuseAddr )
flags |= UV_UDP_REUSEADDR ;
if ( cluster . isWorker && ! exclusive ) {
function onHandle ( err , handle ) {
const onHandle = ( err , handle ) => {
if ( err ) {
var ex = exceptionWithHostPort ( err , 'bind' , ip , port ) ;
self . emit ( 'error' , ex ) ;
self . _ bindState = BIND_STATE_UNBOUND ;
this . emit ( 'error' , ex ) ;
this . _ bindState = BIND_STATE_UNBOUND ;
return ;
}
if ( ! self . _ handle )
if ( ! this . _ handle )
// handle has been closed in the mean time.
return handle . close ( ) ;
replaceHandle ( self , handle ) ;
startListening ( self ) ;
}
cluster . _ getServer ( self , {
replaceHandle ( this , handle ) ;
startListening ( this ) ;
} ;
cluster . _ getServer ( this , {
address : ip ,
port : port ,
addressType : self . type ,
addressType : this . type ,
fd : - 1 ,
flags : flags
} , onHandle ) ;
} else {
if ( ! self . _ handle )
if ( ! this . _ handle )
return ; // handle has been closed in the mean time
const err = self . _ handle . bind ( ip , port || 0 , flags ) ;
const err = this . _ handle . bind ( ip , port || 0 , flags ) ;
if ( err ) {
var ex = exceptionWithHostPort ( err , 'bind' , ip , port ) ;
self . emit ( 'error' , ex ) ;
self . _ bindState = BIND_STATE_UNBOUND ;
this . emit ( 'error' , ex ) ;
this . _ bindState = BIND_STATE_UNBOUND ;
// Todo: close?
return ;
}
startListening ( self ) ;
startListening ( this ) ;
}
} ) ;
return self ;
return this ;
} ;
@ -315,7 +313,6 @@ Socket.prototype.send = function(buffer,
port ,
address ,
callback ) {
const self = this ;
let list ;
if ( address || ( port && typeof port !== 'function' ) ) {
@ -347,24 +344,26 @@ Socket.prototype.send = function(buffer,
if ( typeof callback !== 'function' )
callback = undefined ;
self . _ healthCheck ( ) ;
this . _ healthCheck ( ) ;
if ( self . _ bindState == BIND_STATE_UNBOUND )
self . bind ( { port : 0 , exclusive : true } , null ) ;
if ( this . _ bindState = == BIND_STATE_UNBOUND )
this . bind ( { port : 0 , exclusive : true } , null ) ;
if ( list . length === 0 )
list . push ( Buffer . alloc ( 0 ) ) ;
// If the socket hasn't been bound yet, push the outbound packet onto the
// send queue and send after binding is complete.
if ( self . _ bindState != BIND_STATE_BOUND ) {
enqueue ( self , self . send . bind ( self , list , port , address , callback ) ) ;
if ( this . _ bindState != = BIND_STATE_BOUND ) {
enqueue ( this , this . send . bind ( this , list , port , address , callback ) ) ;
return ;
}
self . _ handle . lookup ( address , function afterDns ( ex , ip ) {
doSend ( ex , self , ip , list , address , port , callback ) ;
} ) ;
const afterDns = ( ex , ip ) => {
doSend ( ex , this , ip , list , address , port , callback ) ;
} ;
this . _ handle . lookup ( address , afterDns ) ;
} ;