@@ -23,7 +23,7 @@ import { IdInternal } from '@matrixai/id';
23
23
import { Lock , LockBox } from '@matrixai/async-locks' ;
24
24
import { Timer } from '@matrixai/timer' ;
25
25
import { timedCancellable , context } from '@matrixai/contexts/dist/decorators' ;
26
- import { Evented } from '@matrixai/events' ;
26
+ import { EventDefault , Evented } from '@matrixai/events' ;
27
27
import { QUICSocket , QUICServer , events as quicEvents } from '@matrixai/quic' ;
28
28
import NodeConnection from './NodeConnection' ;
29
29
import * as nodesUtils from './utils' ;
@@ -140,6 +140,12 @@ class NodeConnectionManager {
140
140
141
141
protected connectionLocks : LockBox < Lock > = new LockBox ( ) ;
142
142
143
+ protected eventQUICServerConnectionHandler = (
144
+ e : quicEvents . QUICServerConnectionEvent ,
145
+ ) => {
146
+ void this . handleConnectionReverse ( e . detail ) ;
147
+ } ;
148
+
143
149
protected handleQUICSocketEvents = ( e : quicEvents . QUICSocketEvent ) => {
144
150
// QUICSocket events are...
145
151
// - QUICSocketEvent,
@@ -158,9 +164,6 @@ class NodeConnectionManager {
158
164
// - QUICServerStartEvent,
159
165
// - QUICServerStopEvent,
160
166
// - QUICServerErrorEvent,
161
- if ( e instanceof quicEvents . QUICServerConnectionEvent ) {
162
- void this . handleConnectionReverse ( e . detail ) ;
163
- }
164
167
this . dispatchEvent ( e . clone ( ) ) ;
165
168
} ;
166
169
@@ -311,17 +314,37 @@ class NodeConnectionManager {
311
314
ipv6Only,
312
315
} ) ;
313
316
314
- this . quicSocket . addEventListener ( this . handleQUICSocketEvents ) ;
315
- this . quicServer . addEventListener ( this . handleQUICServerEvents ) ;
317
+ this . quicSocket . addEventListener (
318
+ EventDefault . name ,
319
+ this . handleQUICSocketEvents ,
320
+ ) ;
321
+ this . quicServer . addEventListener (
322
+ quicEvents . QUICServerConnectionEvent . name ,
323
+ this . eventQUICServerConnectionHandler ,
324
+ ) ;
325
+ this . quicServer . addEventListener (
326
+ EventDefault . name ,
327
+ this . handleQUICServerEvents ,
328
+ ) ;
316
329
317
330
this . logger . info ( `Started ${ this . constructor . name } ` ) ;
318
331
}
319
332
320
333
public async stop ( ) {
321
334
this . logger . info ( `Stop ${ this . constructor . name } ` ) ;
322
335
323
- this . quicSocket . removeEventListener ( this . handleQUICSocketEvents ) ;
324
- this . quicServer . removeEventListener ( this . handleQUICServerEvents ) ;
336
+ this . quicServer . removeEventListener (
337
+ EventDefault . name ,
338
+ this . handleQUICServerEvents ,
339
+ ) ;
340
+ this . quicServer . removeEventListener (
341
+ quicEvents . QUICServerConnectionEvent . name ,
342
+ this . eventQUICServerConnectionHandler ,
343
+ ) ;
344
+ this . quicSocket . removeEventListener (
345
+ EventDefault . name ,
346
+ this . handleQUICSocketEvents ,
347
+ ) ;
325
348
326
349
const destroyProms : Array < Promise < void > > = [ ] ;
327
350
for ( const [ nodeId , connAndTimer ] of this . connections ) {
@@ -806,26 +829,39 @@ class NodeConnectionManager {
806
829
const nodeIdString = nodeId . toString ( ) as NodeIdString ;
807
830
// Check if exists in map, this should never happen but better safe than sorry.
808
831
if ( this . connections . has ( nodeIdString ) ) utils . never ( ) ;
809
- // TODO: set up event handling for all events here, need to make sure that the stream event and connection event is propagated.
810
- // The connection event should only contain connection metadata and not the connection itself otherwise we circumvent the locking.
811
832
// Setting up events
812
833
const nodeConnectionEventsHandler = ( e ) => {
813
834
// Propagate all events upwards
814
835
this . dispatchEvent ( e . clone ( ) ) ;
815
836
} ;
816
- nodeConnection . addEventListener ( nodeConnectionEventsHandler ) ;
817
837
nodeConnection . addEventListener (
818
- nodesEvents . EventNodeConnectionDestroy . name ,
819
- ( ) => {
838
+ EventDefault . name ,
839
+ nodeConnectionEventsHandler ,
840
+ ) ;
841
+ nodeConnection . addEventListener (
842
+ nodesEvents . EventNodeConnectionError . name ,
843
+ async ( e : nodesEvents . EventNodeConnectionError ) => {
820
844
this . logger . debug ( 'stream destroyed event' ) ;
821
845
nodeConnection . removeEventListener ( nodeConnectionEventsHandler ) ;
822
- // To avoid deadlock only in the case where this is called
823
- // we want to check for destroying connection and read lock
824
- // If the connection is calling destroyCallback then it SHOULD exist in the connection map.
825
- if ( ! this . connections . has ( nodeIdString ) ) return ;
826
- // Already locked so already destroying
827
- if ( this . connectionLocks . isLocked ( nodeIdString ) ) return ;
828
- void this . destroyConnection ( nodeId ) ;
846
+ try {
847
+ // To avoid deadlock only in the case where this is called
848
+ // we want to check for destroying connection and read lock
849
+ // If the connection is calling destroyCallback then it SHOULD exist in the connection map.
850
+ // Already locked so already destroying
851
+ if ( this . connectionLocks . isLocked ( nodeIdString ) ) return ;
852
+ await this . destroyConnection ( nodeId ) ;
853
+ this . dispatchEvent (
854
+ new nodesEvents . EventNodeConnectionManagerConnectionFailure ( {
855
+ detail : e ,
856
+ } ) ,
857
+ ) ;
858
+ } catch ( err ) {
859
+ this . dispatchEvent (
860
+ new nodesEvents . EventNodeConnectionManagerConnectionFailure ( {
861
+ detail : new AggregateError ( [ err , e . detail ] ) ,
862
+ } ) ,
863
+ ) ;
864
+ }
829
865
} ,
830
866
{ once : true } ,
831
867
) ;
0 commit comments