@@ -342,10 +342,11 @@ Socket.prototype.read = function(n) {
342
342
} ;
343
343
344
344
345
+ // FIXME(joyeecheung): this method is neither documented nor tested
345
346
Socket . prototype . listen = function ( ) {
346
347
debug ( 'socket.listen' ) ;
347
348
this . on ( 'connection' , arguments [ 0 ] ) ;
348
- listen ( this , null , null , null ) ;
349
+ listenInCluster ( this , null , null , null ) ;
349
350
} ;
350
351
351
352
@@ -1178,13 +1179,7 @@ util.inherits(Server, EventEmitter);
1178
1179
1179
1180
function toNumber ( x ) { return ( x = Number ( x ) ) >= 0 ? x : false ; }
1180
1181
1181
- function _listen ( handle , backlog ) {
1182
- // Use a backlog of 512 entries. We pass 511 to the listen() call because
1183
- // the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1184
- // which will thus give us a backlog of 512 entries.
1185
- return handle . listen ( backlog || 511 ) ;
1186
- }
1187
-
1182
+ // Returns handle if it can be created, or error code if it can't
1188
1183
function createServerHandle ( address , port , addressType , fd ) {
1189
1184
var err = 0 ;
1190
1185
// assign handle in listen, and clean up if bind or listen fails
@@ -1241,19 +1236,19 @@ function createServerHandle(address, port, addressType, fd) {
1241
1236
return handle ;
1242
1237
}
1243
1238
1244
-
1245
- Server . prototype . _listen2 = function ( address , port , addressType , backlog , fd ) {
1246
- debug ( 'listen2' , address , port , addressType , backlog , fd ) ;
1239
+ function setupListenHandle ( address , port , addressType , backlog , fd ) {
1240
+ debug ( 'setupListenHandle' , address , port , addressType , backlog , fd ) ;
1247
1241
1248
1242
// If there is not yet a handle, we need to create one and bind.
1249
1243
// In the case of a server sent via IPC, we don't need to do this.
1250
1244
if ( this . _handle ) {
1251
- debug ( '_listen2 : have a handle already' ) ;
1245
+ debug ( 'setupListenHandle : have a handle already' ) ;
1252
1246
} else {
1253
- debug ( '_listen2 : create a handle' ) ;
1247
+ debug ( 'setupListenHandle : create a handle' ) ;
1254
1248
1255
1249
var rval = null ;
1256
1250
1251
+ // Try to bind to the unspecified IPv6 address, see if IPv6 is available
1257
1252
if ( ! address && typeof fd !== 'number' ) {
1258
1253
rval = createServerHandle ( '::' , port , 6 , fd ) ;
1259
1254
@@ -1281,7 +1276,10 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1281
1276
this . _handle . onconnection = onconnection ;
1282
1277
this . _handle . owner = this ;
1283
1278
1284
- var err = _listen ( this . _handle , backlog ) ;
1279
+ // Use a backlog of 512 entries. We pass 511 to the listen() call because
1280
+ // the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1281
+ // which will thus give us a backlog of 512 entries.
1282
+ var err = this . _handle . listen ( backlog || 511 ) ;
1285
1283
1286
1284
if ( err ) {
1287
1285
var ex = exceptionWithHostPort ( err , 'listen' , address , port ) ;
@@ -1299,8 +1297,9 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1299
1297
this . unref ( ) ;
1300
1298
1301
1299
process . nextTick ( emitListeningNT , this ) ;
1302
- } ;
1300
+ }
1303
1301
1302
+ Server . prototype . _listen2 = setupListenHandle ; // legacy alias
1304
1303
1305
1304
function emitErrorNT ( self , err ) {
1306
1305
self . emit ( 'error' , err ) ;
@@ -1314,25 +1313,32 @@ function emitListeningNT(self) {
1314
1313
}
1315
1314
1316
1315
1317
- function listen ( self , address , port , addressType , backlog , fd , exclusive ) {
1316
+ function listenInCluster ( server , address , port , addressType ,
1317
+ backlog , fd , exclusive ) {
1318
1318
exclusive = ! ! exclusive ;
1319
1319
1320
1320
if ( ! cluster ) cluster = require ( 'cluster' ) ;
1321
1321
1322
1322
if ( cluster . isMaster || exclusive ) {
1323
- self . _listen2 ( address , port , addressType , backlog , fd ) ;
1323
+ // Will create a new handle
1324
+ // _listen2 sets up the listened handle, it is still named like this
1325
+ // to avoid breaking code that wraps this method
1326
+ server . _listen2 ( address , port , addressType , backlog , fd ) ;
1324
1327
return ;
1325
1328
}
1326
1329
1327
- cluster . _getServer ( self , {
1330
+ const serverQuery = {
1328
1331
address : address ,
1329
1332
port : port ,
1330
1333
addressType : addressType ,
1331
1334
fd : fd ,
1332
1335
flags : 0
1333
- } , cb ) ;
1336
+ } ;
1337
+
1338
+ // Get the master's server handle, and listen on it
1339
+ cluster . _getServer ( server , serverQuery , listenOnMasterHandle ) ;
1334
1340
1335
- function cb ( err , handle ) {
1341
+ function listenOnMasterHandle ( err , handle ) {
1336
1342
// EADDRINUSE may not be reported until we call listen(). To complicate
1337
1343
// matters, a failed bind() followed by listen() will implicitly bind to
1338
1344
// a random port. Ergo, check that the socket is bound to the expected
@@ -1350,11 +1356,14 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
1350
1356
1351
1357
if ( err ) {
1352
1358
var ex = exceptionWithHostPort ( err , 'bind' , address , port ) ;
1353
- return self . emit ( 'error' , ex ) ;
1359
+ return server . emit ( 'error' , ex ) ;
1354
1360
}
1355
1361
1356
- self . _handle = handle ;
1357
- self . _listen2 ( address , port , addressType , backlog , fd ) ;
1362
+ // Reuse master's server handle
1363
+ server . _handle = handle ;
1364
+ // _listen2 sets up the listened handle, it is still named like this
1365
+ // to avoid breaking code that wraps this method
1366
+ server . _listen2 ( address , port , addressType , backlog , fd ) ;
1358
1367
}
1359
1368
}
1360
1369
@@ -1381,12 +1390,12 @@ Server.prototype.listen = function() {
1381
1390
// (handle[, backlog][, cb]) where handle is an object with a handle
1382
1391
if ( options instanceof TCP ) {
1383
1392
this . _handle = options ;
1384
- listen ( this , null , - 1 , - 1 , backlogFromArgs ) ;
1393
+ listenInCluster ( this , null , - 1 , - 1 , backlogFromArgs ) ;
1385
1394
return this ;
1386
1395
}
1387
1396
// (handle[, backlog][, cb]) where handle is an object with a fd
1388
1397
if ( typeof options . fd === 'number' && options . fd >= 0 ) {
1389
- listen ( this , null , null , null , backlogFromArgs , options . fd ) ;
1398
+ listenInCluster ( this , null , null , null , backlogFromArgs , options . fd ) ;
1390
1399
return this ;
1391
1400
}
1392
1401
@@ -1411,8 +1420,9 @@ Server.prototype.listen = function() {
1411
1420
lookupAndListen ( this , options . port | 0 , options . host , backlog ,
1412
1421
options . exclusive ) ;
1413
1422
} else { // Undefined host, listens on unspecified address
1414
- listen ( this , null , options . port | 0 , 4 , // addressType will be ignored
1415
- backlog , undefined , options . exclusive ) ;
1423
+ // Default addressType 4 will be used to search for master server
1424
+ listenInCluster ( this , null , options . port | 0 , 4 ,
1425
+ backlog , undefined , options . exclusive ) ;
1416
1426
}
1417
1427
return this ;
1418
1428
}
@@ -1422,20 +1432,23 @@ Server.prototype.listen = function() {
1422
1432
if ( options . path && isPipeName ( options . path ) ) {
1423
1433
const pipeName = this . _pipeName = options . path ;
1424
1434
const backlog = options . backlog || backlogFromArgs ;
1425
- listen ( this , pipeName , - 1 , - 1 , backlog , undefined , options . exclusive ) ;
1435
+ listenInCluster ( this , pipeName , - 1 , - 1 ,
1436
+ backlog , undefined , options . exclusive ) ;
1426
1437
return this ;
1427
1438
}
1428
1439
1429
1440
throw new Error ( 'Invalid listen argument: ' + util . inspect ( options ) ) ;
1430
1441
} ;
1431
1442
1432
1443
function lookupAndListen ( self , port , address , backlog , exclusive ) {
1433
- require ( 'dns' ) . lookup ( address , function doListening ( err , ip , addressType ) {
1444
+ const dns = require ( 'dns' ) ;
1445
+ dns . lookup ( address , function doListen ( err , ip , addressType ) {
1434
1446
if ( err ) {
1435
1447
self . emit ( 'error' , err ) ;
1436
1448
} else {
1437
1449
addressType = ip ? addressType : 4 ;
1438
- listen ( self , ip , port , addressType , backlog , undefined , exclusive ) ;
1450
+ listenInCluster ( self , ip , port , addressType ,
1451
+ backlog , undefined , exclusive ) ;
1439
1452
}
1440
1453
} ) ;
1441
1454
}
0 commit comments