@@ -12,6 +12,7 @@ assertCrypto();
12
12
const {
13
13
Array,
14
14
BigInt64Array,
15
+ Boolean,
15
16
Error,
16
17
Map,
17
18
Number,
@@ -38,6 +39,7 @@ const {
38
39
validateQuicEndpointOptions,
39
40
validateCreateSecureContextOptions,
40
41
validateQuicSocketConnectOptions,
42
+ QuicSocketSharedState,
41
43
QuicSessionSharedState,
42
44
QLogStream,
43
45
} = require ( 'internal/quic/util' ) ;
@@ -269,8 +271,8 @@ function onSocketClose(err) {
269
271
270
272
// Called by the C++ internals when the server busy state of
271
273
// the QuicSocket has been changed.
272
- function onSocketServerBusy ( on ) {
273
- this [ owner_symbol ] [ kServerBusy ] ( on ) ;
274
+ function onSocketServerBusy ( ) {
275
+ this [ owner_symbol ] [ kServerBusy ] ( ) ;
274
276
}
275
277
276
278
// Called by the C++ internals when a new server QuicSession has been created.
@@ -845,31 +847,24 @@ class QuicEndpoint {
845
847
class QuicSocket extends EventEmitter {
846
848
[ kInternalState ] = {
847
849
alpn : undefined ,
848
- autoClose : undefined ,
849
850
client : undefined ,
850
851
defaultEncoding : undefined ,
851
852
endpoints : new Set ( ) ,
852
853
highWaterMark : undefined ,
854
+ listenPending : false ,
853
855
lookup : undefined ,
854
856
server : undefined ,
855
- serverBusy : false ,
856
- serverListening : false ,
857
857
serverSecureContext : undefined ,
858
858
sessions : new Set ( ) ,
859
859
state : kSocketUnbound ,
860
- statelessResetEnabled : true ,
860
+ sharedState : undefined ,
861
861
stats : undefined ,
862
862
} ;
863
863
864
864
constructor ( options ) {
865
865
const {
866
866
endpoint,
867
867
868
- // True if the QuicSocket should automatically enter a graceful shutdown
869
- // if it is not listening as a server and the last QuicClientSession
870
- // closes
871
- autoClose,
872
-
873
868
// Default configuration for QuicClientSessions
874
869
client,
875
870
@@ -913,7 +908,6 @@ class QuicSocket extends EventEmitter {
913
908
914
909
const state = this [ kInternalState ] ;
915
910
916
- state . autoClose = autoClose ;
917
911
state . client = client ;
918
912
state . lookup = lookup || ( type === AF_INET6 ? lookup6 : lookup4 ) ;
919
913
state . server = server ;
@@ -976,6 +970,10 @@ class QuicSocket extends EventEmitter {
976
970
if ( handle !== undefined ) {
977
971
handle [ owner_symbol ] = this ;
978
972
this [ async_id_symbol ] = handle . getAsyncId ( ) ;
973
+ this [ kInternalState ] . sharedState =
974
+ new QuicSocketSharedState ( handle . state ) ;
975
+ } else {
976
+ this [ kInternalState ] . sharedState = undefined ;
979
977
}
980
978
}
981
979
@@ -1081,16 +1079,13 @@ class QuicSocket extends EventEmitter {
1081
1079
}
1082
1080
1083
1081
// Called by the C++ internals to notify when server busy status is toggled.
1084
- [ kServerBusy ] ( on ) {
1085
- this [ kInternalState ] . serverBusy = on ;
1086
- // In a nextTick because the event ends up being
1087
- // emitted synchronously when quicSocket.serverBusy
1088
- // is called.
1082
+ [ kServerBusy ] ( ) {
1083
+ const busy = this . serverBusy ;
1089
1084
process . nextTick ( ( ) => {
1090
1085
try {
1091
- this . emit ( 'busy' , on ) ;
1086
+ this . emit ( 'busy' , busy ) ;
1092
1087
} catch ( error ) {
1093
- this [ kRejections ] ( error , 'busy' , on ) ;
1088
+ this [ kRejections ] ( error , 'busy' , busy ) ;
1094
1089
}
1095
1090
} ) ;
1096
1091
}
@@ -1161,6 +1156,7 @@ class QuicSocket extends EventEmitter {
1161
1156
// server and will emit session events whenever a new QuicServerSession
1162
1157
// is created.
1163
1158
const state = this [ kInternalState ] ;
1159
+ state . listenPending = false ;
1164
1160
this [ kHandle ] . listen (
1165
1161
state . serverSecureContext . context ,
1166
1162
address ,
@@ -1225,14 +1221,12 @@ class QuicSocket extends EventEmitter {
1225
1221
// function.
1226
1222
listen ( options , callback ) {
1227
1223
const state = this [ kInternalState ] ;
1228
- if ( state . serverListening )
1229
- throw new ERR_QUICSOCKET_LISTENING ( ) ;
1230
-
1231
1224
if ( state . state === kSocketDestroyed ||
1232
1225
state . state === kSocketClosing ) {
1233
1226
throw new ERR_QUICSOCKET_DESTROYED ( 'listen' ) ;
1234
1227
}
1235
-
1228
+ if ( this . listening || state . listenPending )
1229
+ throw new ERR_QUICSOCKET_LISTENING ( ) ;
1236
1230
if ( typeof options === 'function' ) {
1237
1231
callback = options ;
1238
1232
options = { } ;
@@ -1265,8 +1259,8 @@ class QuicSocket extends EventEmitter {
1265
1259
1266
1260
state . highWaterMark = highWaterMark ;
1267
1261
state . defaultEncoding = defaultEncoding ;
1268
- state . serverListening = true ;
1269
1262
state . alpn = alpn ;
1263
+ state . listenPending = true ;
1270
1264
1271
1265
// If the callback function is provided, it is registered as a
1272
1266
// handler for the on('session') event and will be called whenever
@@ -1403,10 +1397,8 @@ class QuicSocket extends EventEmitter {
1403
1397
// listening for new QuicServerSession connections.
1404
1398
// New initial connection packets for currently unknown
1405
1399
// DCID's will be ignored.
1406
- if ( this [ kHandle ] ) {
1407
- this [ kHandle ] . stopListening ( ) ;
1408
- }
1409
- state . serverListening = false ;
1400
+ if ( this [ kHandle ] )
1401
+ this [ kInternalState ] . sharedState . serverListening = false ;
1410
1402
1411
1403
// If there are no sessions, calling maybeDestroy
1412
1404
// will immediately and synchronously destroy the
@@ -1502,7 +1494,7 @@ class QuicSocket extends EventEmitter {
1502
1494
1503
1495
// True if listen() has been called successfully
1504
1496
get listening ( ) {
1505
- return this [ kInternalState ] . serverListening ;
1497
+ return Boolean ( this [ kInternalState ] . sharedState ?. serverListening ) ;
1506
1498
}
1507
1499
1508
1500
// True if the QuicSocket is currently waiting on at least one
@@ -1518,12 +1510,27 @@ class QuicSocket extends EventEmitter {
1518
1510
if ( state . state === kSocketDestroyed )
1519
1511
throw new ERR_QUICSOCKET_DESTROYED ( 'serverBusy' ) ;
1520
1512
validateBoolean ( on , 'on' ) ;
1521
- if ( state . serverBusy !== on )
1522
- this [ kHandle ] . setServerBusy ( on ) ;
1513
+ if ( state . sharedState . serverBusy !== on ) {
1514
+ state . sharedState . serverBusy = on ;
1515
+ this [ kServerBusy ] ( ) ;
1516
+ }
1523
1517
}
1524
1518
1525
1519
get serverBusy ( ) {
1526
- return this [ kInternalState ] . serverBusy ;
1520
+ return Boolean ( this [ kInternalState ] . sharedState ?. serverBusy ) ;
1521
+ }
1522
+
1523
+ set statelessResetDisabled ( on ) {
1524
+ const state = this [ kInternalState ] ;
1525
+ if ( state . state === kSocketDestroyed )
1526
+ throw new ERR_QUICSOCKET_DESTROYED ( 'statelessResetDisabled' ) ;
1527
+ validateBoolean ( on , 'on' ) ;
1528
+ if ( state . sharedState . statelessResetDisabled !== on )
1529
+ state . sharedState . statelessResetDisabled = on ;
1530
+ }
1531
+
1532
+ get statelessResetDisabled ( ) {
1533
+ return Boolean ( this [ kInternalState ] . sharedState ?. statelessResetDisabled ) ;
1527
1534
}
1528
1535
1529
1536
get duration ( ) {
@@ -1613,21 +1620,6 @@ class QuicSocket extends EventEmitter {
1613
1620
}
1614
1621
this [ kHandle ] . setDiagnosticPacketLoss ( rx , tx ) ;
1615
1622
}
1616
-
1617
- get statelessResetEnabled ( ) {
1618
- return this [ kInternalState ] . statelessResetEnabled ;
1619
- }
1620
-
1621
- set statelessResetEnabled ( on ) {
1622
- const state = this [ kInternalState ] ;
1623
- if ( state . state === kSocketDestroyed )
1624
- throw new ERR_QUICSOCKET_DESTROYED ( 'serverBusy' ) ;
1625
- validateBoolean ( on , 'on' ) ;
1626
- if ( state . statelessResetEnabled !== on ) {
1627
- this [ kHandle ] . enableStatelessReset ( on ) ;
1628
- state . statelessResetEnabled = on ;
1629
- }
1630
- }
1631
1623
}
1632
1624
1633
1625
class QuicSession extends EventEmitter {
0 commit comments