@@ -24,6 +24,8 @@ import {
2424import Logger from '@matrixai/logger' ;
2525import { Timer } from '@matrixai/timer' ;
2626import { context , timedCancellable } from '@matrixai/contexts/dist/decorators' ;
27+ import { withF } from '@matrixai/resources' ;
28+ import { utils as contextsUtils } from '@matrixai/contexts' ;
2729import { buildQuicheConfig } from './config' ;
2830import QUICStream from './QUICStream' ;
2931import { quiche } from './native' ;
@@ -278,27 +280,15 @@ class QUICConnection extends EventTarget {
278280 } ;
279281 ctx . signal . addEventListener ( 'abort' , abortHandler ) ;
280282 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 ;
297287 // This ensures that TLS has been established and verified on both sides
298288 try {
299289 await Promise . race ( [
300290 Promise . all ( [
301- startProm ,
291+ connection . start ( initialData ) ,
302292 connection . establishedP ,
303293 connection . secureEstablishedP ,
304294 ] ) ,
@@ -471,12 +461,24 @@ class QUICConnection extends EventTarget {
471461
472462 /**
473463 * 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
474465 */
475- public async start ( ) : Promise < void > {
466+ public async start ( initialData ?: {
467+ data : Uint8Array ;
468+ remoteInfo : RemoteInfo ;
469+ } ) : Promise < void > {
476470 this . logger . info ( `Start ${ this . constructor . name } ` ) ;
477471 // Set the connection up
478472 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+ ) ;
480482 this . logger . info ( `Started ${ this . constructor . name } ` ) ;
481483 }
482484
@@ -665,13 +667,15 @@ class QUICConnection extends EventTarget {
665667 public async recv (
666668 data : Uint8Array ,
667669 remoteInfo : RemoteInfo ,
668- mon : Monitor < RWLockWriter > ,
670+ mon ? : Monitor < RWLockWriter > ,
669671 ) : 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+ } ) ;
675679
676680 try {
677681 // The remote information may be changed on each receive
0 commit comments