@@ -24,6 +24,8 @@ import {
24
24
import Logger from '@matrixai/logger' ;
25
25
import { Timer } from '@matrixai/timer' ;
26
26
import { context , timedCancellable } from '@matrixai/contexts/dist/decorators' ;
27
+ import { withF } from '@matrixai/resources' ;
28
+ import { utils as contextsUtils } from '@matrixai/contexts' ;
27
29
import { buildQuicheConfig } from './config' ;
28
30
import QUICStream from './QUICStream' ;
29
31
import { quiche } from './native' ;
@@ -278,27 +280,15 @@ class QUICConnection extends EventTarget {
278
280
} ;
279
281
ctx . signal . addEventListener ( 'abort' , abortHandler ) ;
280
282
const connection = new this ( args ) ;
281
- const startProm = connection . start ( ) . then ( async ( ) => {
282
- // If this is a server connection, we need to process the packet that created it
283
- if ( args . type === 'server' ) {
284
- await utils . withMonitor (
285
- undefined ,
286
- connection . lockbox ,
287
- RWLockWriter ,
288
- async ( mon ) => {
289
- await mon . withF ( connection . lockCode , async ( mon ) => {
290
- await connection . recv ( args . data , args . remoteInfo , mon ) ;
291
- await connection . send ( mon ) ;
292
- } ) ;
293
- } ,
294
- ) ;
295
- }
296
- } ) ;
283
+ const initialData =
284
+ args . type === 'server'
285
+ ? { data : args . data , remoteInfo : args . remoteInfo }
286
+ : undefined ;
297
287
// This ensures that TLS has been established and verified on both sides
298
288
try {
299
289
await Promise . race ( [
300
290
Promise . all ( [
301
- startProm ,
291
+ connection . start ( initialData ) ,
302
292
connection . establishedP ,
303
293
connection . secureEstablishedP ,
304
294
] ) ,
@@ -471,12 +461,24 @@ class QUICConnection extends EventTarget {
471
461
472
462
/**
473
463
* This will set up the connection initiate sending
464
+ * @param initialData - If the connection is server initiated then the data that initiated it needs to be provided here
474
465
*/
475
- public async start ( ) : Promise < void > {
466
+ public async start ( initialData ?: {
467
+ data : Uint8Array ;
468
+ remoteInfo : RemoteInfo ;
469
+ } ) : Promise < void > {
476
470
this . logger . info ( `Start ${ this . constructor . name } ` ) ;
477
471
// Set the connection up
478
472
this . socket . connectionMap . set ( this . connectionId , this ) ;
479
- await this . send ( ) ;
473
+ await withF (
474
+ [ contextsUtils . monitor ( this . lockbox , RWLockWriter ) ] ,
475
+ async ( [ mon ] ) => {
476
+ if ( initialData != null ) {
477
+ await this . recv ( initialData . data , initialData . remoteInfo , mon ) ;
478
+ }
479
+ await this . send ( mon ) ;
480
+ } ,
481
+ ) ;
480
482
this . logger . info ( `Started ${ this . constructor . name } ` ) ;
481
483
}
482
484
@@ -665,13 +667,15 @@ class QUICConnection extends EventTarget {
665
667
public async recv (
666
668
data : Uint8Array ,
667
669
remoteInfo : RemoteInfo ,
668
- mon : Monitor < RWLockWriter > ,
670
+ mon ? : Monitor < RWLockWriter > ,
669
671
) : Promise < void > {
670
- if ( ! mon . isLocked ( this . lockCode ) ) {
671
- return mon . withF ( this . lockCode , async ( mon ) => {
672
- return this . recv ( data , remoteInfo , mon ) ;
673
- } ) ;
674
- }
672
+ await utils . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
673
+ if ( ! mon . isLocked ( this . lockCode ) ) {
674
+ return mon . withF ( this . lockCode , async ( mon ) => {
675
+ return this . recv ( data , remoteInfo , mon ) ;
676
+ } ) ;
677
+ }
678
+ } ) ;
675
679
676
680
try {
677
681
// The remote information may be changed on each receive
0 commit comments