@@ -29,21 +29,6 @@ async function generateKeyHMAC() {
29
29
return key ;
30
30
}
31
31
32
- function socketCleanupFactory ( ) {
33
- const sockets = new Set ( ) ;
34
- return {
35
- extractSocket : ( thing ) => sockets . add ( thing . socket ) ,
36
- stopSockets : async ( ) => {
37
- const stopProms = [ ] ;
38
- for ( const socket of sockets ) {
39
- stopProms . push ( socket . stop ( { force : true } ) ) ;
40
- }
41
- await Promise . all ( stopProms ) ;
42
- } ,
43
- sockets,
44
- } ;
45
- }
46
-
47
32
async function signHMAC ( key , data ) {
48
33
const cryptoKey = await webcrypto . subtle . importKey (
49
34
'raw' ,
@@ -316,15 +301,13 @@ const main = async () => {
316
301
) ,
317
302
] ) ;
318
303
const key = await generateKeyHMAC ( ) ;
319
- let socketCleanMethods = socketCleanupFactory ( ) ;
320
304
const serverCrypto = {
321
305
sign : signHMAC ,
322
306
verify : verifyHMAC ,
323
307
} ;
324
308
const clientCrypto = {
325
309
randomBytes : ( data ) => webcrypto . getRandomValues ( new Uint8Array ( data ) ) ,
326
310
} ;
327
- let connectionEventProm = utils . promise ( ) ;
328
311
const tlsConfig = await generateTLSConfig ( ) ;
329
312
const server = new QUICServer ( {
330
313
crypto : {
@@ -338,9 +321,31 @@ const main = async () => {
338
321
verifyPeer : false ,
339
322
} ,
340
323
} ) ;
341
- socketCleanMethods . extractSocket ( server ) ;
342
- server . addEventListener ( events . EventQUICServerConnection . name , ( e ) =>
343
- connectionEventProm . resolveP ( e ) ,
324
+ let activeStream = undefined ;
325
+ let activeConn = undefined ;
326
+ // Lets keep all the handling in once place but track when it is done with promises
327
+ server . addEventListener ( events . EventQUICServerConnection . name , ( e ) => {
328
+ const conn = e . detail ;
329
+ conn . addEventListener (
330
+ events . EventQUICConnectionStream . name ,
331
+ ( streamEvent ) => {
332
+ const stream = streamEvent . detail ;
333
+ const streamProm = stream . readable . pipeTo ( stream . writable ) ;
334
+ if ( activeStream != null ) throw Error ( 'Active stream should be null' )
335
+ activeStream = streamProm ;
336
+ } ,
337
+ { 'once' : true } ,
338
+ ) ;
339
+ if ( activeConn != null ) throw Error ( 'Active stream should be null' )
340
+ activeConn = utils . promise ( ) ;
341
+ conn . addEventListener (
342
+ events . EventQUICClientDestroyed . name ,
343
+ ( ) => {
344
+ activeConn . resolveP ( )
345
+ } ,
346
+ { once : true } ,
347
+ ) ;
348
+ }
344
349
) ;
345
350
await server . start ( { host : '127.0.0.1' } ) ;
346
351
@@ -350,7 +355,6 @@ const main = async () => {
350
355
// if (i % 500 == 0) console.error('loop', i);
351
356
console . error ( 'loop' , i ) ;
352
357
353
- connectionEventProm = utils . promise ( ) ;
354
358
const client = await QUICClient . createQUICClient ( {
355
359
host : '127.0.0.1' ,
356
360
port : server . port ,
@@ -363,19 +367,6 @@ const main = async () => {
363
367
verifyPeer : false ,
364
368
} ,
365
369
} ) ;
366
- socketCleanMethods . extractSocket ( client ) ;
367
- const conn = ( await connectionEventProm . p ) . detail ;
368
- let activeStream = undefined ;
369
- conn . addEventListener (
370
- events . EventQUICConnectionStream . name ,
371
- ( streamEvent ) => {
372
- const stream = streamEvent . detail ;
373
- const streamProm = stream . readable . pipeTo ( stream . writable ) ;
374
- activeStream = streamProm ;
375
- } ,
376
- { 'once' : true } ,
377
- ) ;
378
-
379
370
const stream = client . connection . newStream ( ) ;
380
371
const writer = stream . writable . getWriter ( ) ;
381
372
await writer . write ( data ) ;
@@ -384,8 +375,10 @@ const main = async () => {
384
375
// do nothing
385
376
}
386
377
await activeStream ;
387
- await conn . stop ( { force : true } ) ;
388
378
await client . destroy ( { force : true } ) ;
379
+ await activeConn ;
380
+ activeConn = undefined ;
381
+ activeStream = undefined ;
389
382
}
390
383
391
384
await server . stop ( { force : true } ) ;
0 commit comments