@@ -41,7 +41,6 @@ use std::num::NonZeroU32;
41
41
use std:: ops:: Add ;
42
42
use std:: task:: { Context , Poll } ;
43
43
use std:: time:: Duration ;
44
- use void:: Void ;
45
44
46
45
/// Configuration for the relay [`Behaviour`].
47
46
///
@@ -230,7 +229,7 @@ pub struct Behaviour {
230
229
circuits : CircuitsTracker ,
231
230
232
231
/// Queue of actions to return when polled.
233
- queued_actions : VecDeque < Action > ,
232
+ queued_actions : VecDeque < ToSwarm < Event , THandlerInEvent < Self > > > ,
234
233
235
234
external_addresses : ExternalAddresses ,
236
235
}
@@ -269,14 +268,12 @@ impl Behaviour {
269
268
// Only emit [`CircuitClosed`] for accepted requests.
270
269
. filter ( |c| matches ! ( c. status, CircuitStatus :: Accepted ) )
271
270
{
272
- self . queued_actions . push_back (
273
- ToSwarm :: GenerateEvent ( Event :: CircuitClosed {
271
+ self . queued_actions
272
+ . push_back ( ToSwarm :: GenerateEvent ( Event :: CircuitClosed {
274
273
src_peer_id : circuit. src_peer_id ,
275
274
dst_peer_id : circuit. dst_peer_id ,
276
275
error : Some ( std:: io:: ErrorKind :: ConnectionAborted . into ( ) ) ,
277
- } )
278
- . into ( ) ,
279
- ) ;
276
+ } ) ) ;
280
277
}
281
278
}
282
279
}
@@ -414,18 +411,29 @@ impl NetworkBehaviour for Behaviour {
414
411
status : proto:: Status :: RESOURCE_LIMIT_EXCEEDED ,
415
412
} ) ,
416
413
}
417
- . into ( )
418
414
} else {
419
415
// Accept reservation.
420
416
self . reservations
421
417
. entry ( event_source)
422
418
. or_default ( )
423
419
. insert ( connection) ;
424
420
425
- Action :: AcceptReservationPrototype {
421
+ ToSwarm :: NotifyHandler {
426
422
handler : NotifyHandler :: One ( connection) ,
427
423
peer_id : event_source,
428
- inbound_reservation_req,
424
+ event : Either :: Left ( handler:: In :: AcceptReservationReq {
425
+ inbound_reservation_req,
426
+ addrs : self
427
+ . external_addresses
428
+ . iter ( )
429
+ . cloned ( )
430
+ // Add local peer ID in case it isn't present yet.
431
+ . filter_map ( |a| match a. iter ( ) . last ( ) ? {
432
+ Protocol :: P2p ( _) => Some ( a) ,
433
+ _ => Some ( a. with ( Protocol :: P2p ( self . local_peer_id ) ) ) ,
434
+ } )
435
+ . collect ( ) ,
436
+ } ) ,
429
437
}
430
438
} ;
431
439
@@ -439,39 +447,35 @@ impl NetworkBehaviour for Behaviour {
439
447
. or_default ( )
440
448
. insert ( connection) ;
441
449
442
- self . queued_actions . push_back (
443
- ToSwarm :: GenerateEvent ( Event :: ReservationReqAccepted {
450
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
451
+ Event :: ReservationReqAccepted {
444
452
src_peer_id : event_source,
445
453
renewed,
446
- } )
447
- . into ( ) ,
448
- ) ;
454
+ } ,
455
+ ) ) ;
449
456
}
450
457
handler:: Event :: ReservationReqAcceptFailed { error } => {
451
- self . queued_actions . push_back (
452
- ToSwarm :: GenerateEvent ( Event :: ReservationReqAcceptFailed {
458
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
459
+ Event :: ReservationReqAcceptFailed {
453
460
src_peer_id : event_source,
454
461
error,
455
- } )
456
- . into ( ) ,
457
- ) ;
462
+ } ,
463
+ ) ) ;
458
464
}
459
465
handler:: Event :: ReservationReqDenied { } => {
460
- self . queued_actions . push_back (
461
- ToSwarm :: GenerateEvent ( Event :: ReservationReqDenied {
466
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
467
+ Event :: ReservationReqDenied {
462
468
src_peer_id : event_source,
463
- } )
464
- . into ( ) ,
465
- ) ;
469
+ } ,
470
+ ) ) ;
466
471
}
467
472
handler:: Event :: ReservationReqDenyFailed { error } => {
468
- self . queued_actions . push_back (
469
- ToSwarm :: GenerateEvent ( Event :: ReservationReqDenyFailed {
473
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
474
+ Event :: ReservationReqDenyFailed {
470
475
src_peer_id : event_source,
471
476
error,
472
- } )
473
- . into ( ) ,
474
- ) ;
477
+ } ,
478
+ ) ) ;
475
479
}
476
480
handler:: Event :: ReservationTimedOut { } => {
477
481
match self . reservations . entry ( event_source) {
@@ -490,12 +494,10 @@ impl NetworkBehaviour for Behaviour {
490
494
}
491
495
}
492
496
493
- self . queued_actions . push_back (
494
- ToSwarm :: GenerateEvent ( Event :: ReservationTimedOut {
497
+ self . queued_actions
498
+ . push_back ( ToSwarm :: GenerateEvent ( Event :: ReservationTimedOut {
495
499
src_peer_id : event_source,
496
- } )
497
- . into ( ) ,
498
- ) ;
500
+ } ) ) ;
499
501
}
500
502
handler:: Event :: CircuitReqReceived {
501
503
inbound_circuit_req,
@@ -565,7 +567,7 @@ impl NetworkBehaviour for Behaviour {
565
567
} ) ,
566
568
}
567
569
} ;
568
- self . queued_actions . push_back ( action. into ( ) ) ;
570
+ self . queued_actions . push_back ( action) ;
569
571
}
570
572
handler:: Event :: CircuitReqDenied {
571
573
circuit_id,
@@ -575,13 +577,11 @@ impl NetworkBehaviour for Behaviour {
575
577
self . circuits . remove ( circuit_id) ;
576
578
}
577
579
578
- self . queued_actions . push_back (
579
- ToSwarm :: GenerateEvent ( Event :: CircuitReqDenied {
580
+ self . queued_actions
581
+ . push_back ( ToSwarm :: GenerateEvent ( Event :: CircuitReqDenied {
580
582
src_peer_id : event_source,
581
583
dst_peer_id,
582
- } )
583
- . into ( ) ,
584
- ) ;
584
+ } ) ) ;
585
585
}
586
586
handler:: Event :: CircuitReqDenyFailed {
587
587
circuit_id,
@@ -592,14 +592,13 @@ impl NetworkBehaviour for Behaviour {
592
592
self . circuits . remove ( circuit_id) ;
593
593
}
594
594
595
- self . queued_actions . push_back (
596
- ToSwarm :: GenerateEvent ( Event :: CircuitReqDenyFailed {
595
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
596
+ Event :: CircuitReqDenyFailed {
597
597
src_peer_id : event_source,
598
598
dst_peer_id,
599
599
error,
600
- } )
601
- . into ( ) ,
602
- ) ;
600
+ } ,
601
+ ) ) ;
603
602
}
604
603
handler:: Event :: OutboundConnectNegotiated {
605
604
circuit_id,
@@ -610,21 +609,18 @@ impl NetworkBehaviour for Behaviour {
610
609
dst_stream,
611
610
dst_pending_data,
612
611
} => {
613
- self . queued_actions . push_back (
614
- ToSwarm :: NotifyHandler {
615
- handler : NotifyHandler :: One ( src_connection_id) ,
616
- peer_id : src_peer_id,
617
- event : Either :: Left ( handler:: In :: AcceptAndDriveCircuit {
618
- circuit_id,
619
- dst_peer_id : event_source,
620
- inbound_circuit_req,
621
- dst_handler_notifier,
622
- dst_stream,
623
- dst_pending_data,
624
- } ) ,
625
- }
626
- . into ( ) ,
627
- ) ;
612
+ self . queued_actions . push_back ( ToSwarm :: NotifyHandler {
613
+ handler : NotifyHandler :: One ( src_connection_id) ,
614
+ peer_id : src_peer_id,
615
+ event : Either :: Left ( handler:: In :: AcceptAndDriveCircuit {
616
+ circuit_id,
617
+ dst_peer_id : event_source,
618
+ inbound_circuit_req,
619
+ dst_handler_notifier,
620
+ dst_stream,
621
+ dst_pending_data,
622
+ } ) ,
623
+ } ) ;
628
624
}
629
625
handler:: Event :: OutboundConnectNegotiationFailed {
630
626
circuit_id,
@@ -634,54 +630,47 @@ impl NetworkBehaviour for Behaviour {
634
630
status,
635
631
error,
636
632
} => {
637
- self . queued_actions . push_back (
638
- ToSwarm :: NotifyHandler {
639
- handler : NotifyHandler :: One ( src_connection_id) ,
640
- peer_id : src_peer_id,
641
- event : Either :: Left ( handler:: In :: DenyCircuitReq {
642
- circuit_id : Some ( circuit_id) ,
643
- inbound_circuit_req,
644
- status,
645
- } ) ,
646
- }
647
- . into ( ) ,
648
- ) ;
649
- self . queued_actions . push_back (
650
- ToSwarm :: GenerateEvent ( Event :: CircuitReqOutboundConnectFailed {
633
+ self . queued_actions . push_back ( ToSwarm :: NotifyHandler {
634
+ handler : NotifyHandler :: One ( src_connection_id) ,
635
+ peer_id : src_peer_id,
636
+ event : Either :: Left ( handler:: In :: DenyCircuitReq {
637
+ circuit_id : Some ( circuit_id) ,
638
+ inbound_circuit_req,
639
+ status,
640
+ } ) ,
641
+ } ) ;
642
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
643
+ Event :: CircuitReqOutboundConnectFailed {
651
644
src_peer_id,
652
645
dst_peer_id : event_source,
653
646
error,
654
- } )
655
- . into ( ) ,
656
- ) ;
647
+ } ,
648
+ ) ) ;
657
649
}
658
650
handler:: Event :: CircuitReqAccepted {
659
651
dst_peer_id,
660
652
circuit_id,
661
653
} => {
662
654
self . circuits . accepted ( circuit_id) ;
663
- self . queued_actions . push_back (
664
- ToSwarm :: GenerateEvent ( Event :: CircuitReqAccepted {
655
+ self . queued_actions
656
+ . push_back ( ToSwarm :: GenerateEvent ( Event :: CircuitReqAccepted {
665
657
src_peer_id : event_source,
666
658
dst_peer_id,
667
- } )
668
- . into ( ) ,
669
- ) ;
659
+ } ) ) ;
670
660
}
671
661
handler:: Event :: CircuitReqAcceptFailed {
672
662
dst_peer_id,
673
663
circuit_id,
674
664
error,
675
665
} => {
676
666
self . circuits . remove ( circuit_id) ;
677
- self . queued_actions . push_back (
678
- ToSwarm :: GenerateEvent ( Event :: CircuitReqAcceptFailed {
667
+ self . queued_actions . push_back ( ToSwarm :: GenerateEvent (
668
+ Event :: CircuitReqAcceptFailed {
679
669
src_peer_id : event_source,
680
670
dst_peer_id,
681
671
error,
682
- } )
683
- . into ( ) ,
684
- ) ;
672
+ } ,
673
+ ) ) ;
685
674
}
686
675
handler:: Event :: CircuitClosed {
687
676
dst_peer_id,
@@ -690,14 +679,12 @@ impl NetworkBehaviour for Behaviour {
690
679
} => {
691
680
self . circuits . remove ( circuit_id) ;
692
681
693
- self . queued_actions . push_back (
694
- ToSwarm :: GenerateEvent ( Event :: CircuitClosed {
682
+ self . queued_actions
683
+ . push_back ( ToSwarm :: GenerateEvent ( Event :: CircuitClosed {
695
684
src_peer_id : event_source,
696
685
dst_peer_id,
697
686
error,
698
- } )
699
- . into ( ) ,
700
- ) ;
687
+ } ) ) ;
701
688
}
702
689
}
703
690
}
@@ -707,8 +694,8 @@ impl NetworkBehaviour for Behaviour {
707
694
_cx : & mut Context < ' _ > ,
708
695
_: & mut impl PollParameters ,
709
696
) -> Poll < ToSwarm < Self :: ToSwarm , THandlerInEvent < Self > > > {
710
- if let Some ( action ) = self . queued_actions . pop_front ( ) {
711
- return Poll :: Ready ( action . build ( self . local_peer_id , & self . external_addresses ) ) ;
697
+ if let Some ( to_swarm ) = self . queued_actions . pop_front ( ) {
698
+ return Poll :: Ready ( to_swarm ) ;
712
699
}
713
700
714
701
Poll :: Pending
@@ -804,53 +791,3 @@ impl Add<u64> for CircuitId {
804
791
CircuitId ( self . 0 + rhs)
805
792
}
806
793
}
807
-
808
- /// A [`ToSwarm`], either complete, or still requiring data from [`PollParameters`]
809
- /// before being returned in [`Behaviour::poll`].
810
- #[ allow( clippy:: large_enum_variant) ]
811
- enum Action {
812
- Done ( ToSwarm < Event , Either < handler:: In , Void > > ) ,
813
- AcceptReservationPrototype {
814
- inbound_reservation_req : inbound_hop:: ReservationReq ,
815
- handler : NotifyHandler ,
816
- peer_id : PeerId ,
817
- } ,
818
- }
819
-
820
- impl From < ToSwarm < Event , Either < handler:: In , Void > > > for Action {
821
- fn from ( action : ToSwarm < Event , Either < handler:: In , Void > > ) -> Self {
822
- Self :: Done ( action)
823
- }
824
- }
825
-
826
- impl Action {
827
- fn build (
828
- self ,
829
- local_peer_id : PeerId ,
830
- external_addresses : & ExternalAddresses ,
831
- ) -> ToSwarm < Event , Either < handler:: In , Void > > {
832
- match self {
833
- Action :: Done ( action) => action,
834
- Action :: AcceptReservationPrototype {
835
- inbound_reservation_req,
836
- handler,
837
- peer_id,
838
- } => ToSwarm :: NotifyHandler {
839
- handler,
840
- peer_id,
841
- event : Either :: Left ( handler:: In :: AcceptReservationReq {
842
- inbound_reservation_req,
843
- addrs : external_addresses
844
- . iter ( )
845
- . cloned ( )
846
- // Add local peer ID in case it isn't present yet.
847
- . filter_map ( |a| match a. iter ( ) . last ( ) ? {
848
- Protocol :: P2p ( _) => Some ( a) ,
849
- _ => Some ( a. with ( Protocol :: P2p ( local_peer_id) ) ) ,
850
- } )
851
- . collect ( ) ,
852
- } ) ,
853
- } ,
854
- }
855
- }
856
- }
0 commit comments