@@ -321,10 +321,11 @@ Socket.prototype.read = function(n) {
321
321
} ;
322
322
323
323
324
+ // FIXME(joyeecheung): this method is neither documented nor tested
324
325
Socket . prototype . listen = function ( ) {
325
326
debug ( 'socket.listen' ) ;
326
327
this . on ( 'connection' , arguments [ 0 ] ) ;
327
- listen ( this , null , null , null ) ;
328
+ listenInCluster ( this , null , null , null ) ;
328
329
} ;
329
330
330
331
@@ -1156,13 +1157,7 @@ util.inherits(Server, EventEmitter);
1156
1157
1157
1158
function toNumber ( x ) { return ( x = Number ( x ) ) >= 0 ? x : false ; }
1158
1159
1159
- function _listen ( handle , backlog ) {
1160
- // Use a backlog of 512 entries. We pass 511 to the listen() call because
1161
- // the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1162
- // which will thus give us a backlog of 512 entries.
1163
- return handle . listen ( backlog || 511 ) ;
1164
- }
1165
-
1160
+ // Returns handle if it can be created, or error code if it can't
1166
1161
function createServerHandle ( address , port , addressType , fd ) {
1167
1162
var err = 0 ;
1168
1163
// assign handle in listen, and clean up if bind or listen fails
@@ -1219,19 +1214,19 @@ function createServerHandle(address, port, addressType, fd) {
1219
1214
return handle ;
1220
1215
}
1221
1216
1222
-
1223
- Server . prototype . _listen2 = function ( address , port , addressType , backlog , fd ) {
1224
- debug ( 'listen2' , address , port , addressType , backlog , fd ) ;
1217
+ function setupListenHandle ( address , port , addressType , backlog , fd ) {
1218
+ debug ( 'setupListenHandle' , address , port , addressType , backlog , fd ) ;
1225
1219
1226
1220
// If there is not yet a handle, we need to create one and bind.
1227
1221
// In the case of a server sent via IPC, we don't need to do this.
1228
1222
if ( this . _handle ) {
1229
- debug ( '_listen2 : have a handle already' ) ;
1223
+ debug ( 'setupListenHandle : have a handle already' ) ;
1230
1224
} else {
1231
- debug ( '_listen2 : create a handle' ) ;
1225
+ debug ( 'setupListenHandle : create a handle' ) ;
1232
1226
1233
1227
var rval = null ;
1234
1228
1229
+ // Try to bind to the unspecified IPv6 address, see if IPv6 is available
1235
1230
if ( ! address && typeof fd !== 'number' ) {
1236
1231
rval = createServerHandle ( '::' , port , 6 , fd ) ;
1237
1232
@@ -1259,7 +1254,10 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1259
1254
this . _handle . onconnection = onconnection ;
1260
1255
this . _handle . owner = this ;
1261
1256
1262
- var err = _listen ( this . _handle , backlog ) ;
1257
+ // Use a backlog of 512 entries. We pass 511 to the listen() call because
1258
+ // the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1259
+ // which will thus give us a backlog of 512 entries.
1260
+ var err = this . _handle . listen ( backlog || 511 ) ;
1263
1261
1264
1262
if ( err ) {
1265
1263
var ex = exceptionWithHostPort ( err , 'listen' , address , port ) ;
@@ -1277,8 +1275,9 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1277
1275
this . unref ( ) ;
1278
1276
1279
1277
process . nextTick ( emitListeningNT , this ) ;
1280
- } ;
1278
+ }
1281
1279
1280
+ Server . prototype . _listen2 = setupListenHandle ; // legacy alias
1282
1281
1283
1282
function emitErrorNT ( self , err ) {
1284
1283
self . emit ( 'error' , err ) ;
@@ -1292,25 +1291,32 @@ function emitListeningNT(self) {
1292
1291
}
1293
1292
1294
1293
1295
- function listen ( self , address , port , addressType , backlog , fd , exclusive ) {
1294
+ function listenInCluster ( server , address , port , addressType ,
1295
+ backlog , fd , exclusive ) {
1296
1296
exclusive = ! ! exclusive ;
1297
1297
1298
1298
if ( ! cluster ) cluster = require ( 'cluster' ) ;
1299
1299
1300
1300
if ( cluster . isMaster || exclusive ) {
1301
- self . _listen2 ( address , port , addressType , backlog , fd ) ;
1301
+ // Will create a new handle
1302
+ // _listen2 sets up the listened handle, it is still named like this
1303
+ // to avoid breaking code that wraps this method
1304
+ server . _listen2 ( address , port , addressType , backlog , fd ) ;
1302
1305
return ;
1303
1306
}
1304
1307
1305
- cluster . _getServer ( self , {
1308
+ const serverQuery = {
1306
1309
address : address ,
1307
1310
port : port ,
1308
1311
addressType : addressType ,
1309
1312
fd : fd ,
1310
1313
flags : 0
1311
- } , cb ) ;
1314
+ } ;
1315
+
1316
+ // Get the master's server handle, and listen on it
1317
+ cluster . _getServer ( server , serverQuery , listenOnMasterHandle ) ;
1312
1318
1313
- function cb ( err , handle ) {
1319
+ function listenOnMasterHandle ( err , handle ) {
1314
1320
// EADDRINUSE may not be reported until we call listen(). To complicate
1315
1321
// matters, a failed bind() followed by listen() will implicitly bind to
1316
1322
// a random port. Ergo, check that the socket is bound to the expected
@@ -1328,11 +1334,14 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
1328
1334
1329
1335
if ( err ) {
1330
1336
var ex = exceptionWithHostPort ( err , 'bind' , address , port ) ;
1331
- return self . emit ( 'error' , ex ) ;
1337
+ return server . emit ( 'error' , ex ) ;
1332
1338
}
1333
1339
1334
- self . _handle = handle ;
1335
- self . _listen2 ( address , port , addressType , backlog , fd ) ;
1340
+ // Reuse master's server handle
1341
+ server . _handle = handle ;
1342
+ // _listen2 sets up the listened handle, it is still named like this
1343
+ // to avoid breaking code that wraps this method
1344
+ server . _listen2 ( address , port , addressType , backlog , fd ) ;
1336
1345
}
1337
1346
}
1338
1347
@@ -1359,12 +1368,12 @@ Server.prototype.listen = function() {
1359
1368
// (handle[, backlog][, cb]) where handle is an object with a handle
1360
1369
if ( options instanceof TCP ) {
1361
1370
this . _handle = options ;
1362
- listen ( this , null , - 1 , - 1 , backlogFromArgs ) ;
1371
+ listenInCluster ( this , null , - 1 , - 1 , backlogFromArgs ) ;
1363
1372
return this ;
1364
1373
}
1365
1374
// (handle[, backlog][, cb]) where handle is an object with a fd
1366
1375
if ( typeof options . fd === 'number' && options . fd >= 0 ) {
1367
- listen ( this , null , null , null , backlogFromArgs , options . fd ) ;
1376
+ listenInCluster ( this , null , null , null , backlogFromArgs , options . fd ) ;
1368
1377
return this ;
1369
1378
}
1370
1379
@@ -1389,8 +1398,9 @@ Server.prototype.listen = function() {
1389
1398
lookupAndListen ( this , options . port | 0 , options . host , backlog ,
1390
1399
options . exclusive ) ;
1391
1400
} else { // Undefined host, listens on unspecified address
1392
- listen ( this , null , options . port | 0 , 4 , // addressType will be ignored
1393
- backlog , undefined , options . exclusive ) ;
1401
+ // Default addressType 4 will be used to search for master server
1402
+ listenInCluster ( this , null , options . port | 0 , 4 ,
1403
+ backlog , undefined , options . exclusive ) ;
1394
1404
}
1395
1405
return this ;
1396
1406
}
@@ -1400,20 +1410,23 @@ Server.prototype.listen = function() {
1400
1410
if ( options . path && isPipeName ( options . path ) ) {
1401
1411
const pipeName = this . _pipeName = options . path ;
1402
1412
const backlog = options . backlog || backlogFromArgs ;
1403
- listen ( this , pipeName , - 1 , - 1 , backlog , undefined , options . exclusive ) ;
1413
+ listenInCluster ( this , pipeName , - 1 , - 1 ,
1414
+ backlog , undefined , options . exclusive ) ;
1404
1415
return this ;
1405
1416
}
1406
1417
1407
1418
throw new Error ( 'Invalid listen argument: ' + options ) ;
1408
1419
} ;
1409
1420
1410
1421
function lookupAndListen ( self , port , address , backlog , exclusive ) {
1411
- require ( 'dns' ) . lookup ( address , function doListening ( err , ip , addressType ) {
1422
+ const dns = require ( 'dns' ) ;
1423
+ dns . lookup ( address , function doListen ( err , ip , addressType ) {
1412
1424
if ( err ) {
1413
1425
self . emit ( 'error' , err ) ;
1414
1426
} else {
1415
1427
addressType = ip ? addressType : 4 ;
1416
- listen ( self , ip , port , addressType , backlog , undefined , exclusive ) ;
1428
+ listenInCluster ( self , ip , port , addressType ,
1429
+ backlog , undefined , exclusive ) ;
1417
1430
}
1418
1431
} ) ;
1419
1432
}
0 commit comments