@@ -506,6 +506,7 @@ impl IoHandler<Message> for Handler {
506
506
is_inbound : true ,
507
507
} => {
508
508
let mut inbound_connections = self . inbound_connections . write ( ) ;
509
+ let outbound_connections = self . outbound_connections . write ( ) ;
509
510
let target = connection. peer_addr ( ) ;
510
511
self . peer_db . insert ( * target) ;
511
512
if let Some ( token) = self . inbound_tokens . lock ( ) . gen ( ) {
@@ -526,10 +527,23 @@ impl IoHandler<Message> for Handler {
526
527
token
527
528
) ;
528
529
}
529
-
530
- let t = inbound_connections. insert ( token, connection) ;
531
- assert ! ( t. is_none( ) ) ;
532
- io. register_stream ( token) ;
530
+ let mut can_insert: bool = true ;
531
+ let mut is_not_out: bool = true ;
532
+ for ( _, in_connection) in inbound_connections. iter ( ) {
533
+ if in_connection. peer_addr ( ) == connection. peer_addr ( ) {
534
+ can_insert = false ;
535
+ }
536
+ }
537
+ for ( _, out_connection) in outbound_connections. iter ( ) {
538
+ if out_connection. peer_addr ( ) == connection. peer_addr ( ) {
539
+ is_not_out = false ;
540
+ }
541
+ }
542
+ if can_insert && is_not_out {
543
+ let t = inbound_connections. insert ( token, connection) ;
544
+ assert ! ( t. is_none( ) ) ;
545
+ io. register_stream ( token) ;
546
+ }
533
547
} else {
534
548
cwarn ! ( NETWORK , "Cannot establish an inbound connection" ) ;
535
549
}
@@ -539,6 +553,7 @@ impl IoHandler<Message> for Handler {
539
553
is_inbound : false ,
540
554
} => {
541
555
let mut outbound_connections = self . outbound_connections . write ( ) ;
556
+ let inbound_connections = self . inbound_connections . write ( ) ;
542
557
if let Some ( token) = self . outbound_tokens . lock ( ) . gen ( ) {
543
558
let peer_addr = * connection. peer_addr ( ) ;
544
559
let remote_node_id = peer_addr. into ( ) ;
@@ -570,9 +585,24 @@ impl IoHandler<Message> for Handler {
570
585
network_message_size,
571
586
) ;
572
587
}
573
- let t = outbound_connections. insert ( token, connection) ;
574
- assert ! ( t. is_none( ) ) ;
575
- io. register_stream ( token) ;
588
+ let mut can_insert: bool = true ;
589
+ let mut is_not_in: bool = true ;
590
+ for ( _, out_connection) in outbound_connections. iter ( ) {
591
+ if out_connection. peer_addr ( ) == connection. peer_addr ( ) {
592
+ can_insert = false ;
593
+ }
594
+ }
595
+ for ( _, in_connection) in inbound_connections. iter ( ) {
596
+ if in_connection. peer_addr ( ) == connection. peer_addr ( ) {
597
+ is_not_in = false ;
598
+ }
599
+ }
600
+
601
+ if can_insert && is_not_in {
602
+ let t = outbound_connections. insert ( token, connection) ;
603
+ assert ! ( t. is_none( ) ) ;
604
+ io. register_stream ( token) ;
605
+ }
576
606
} else {
577
607
cwarn ! ( NETWORK , "Cannot establish an outbound connection" ) ;
578
608
}
0 commit comments