@@ -45,7 +45,6 @@ use libp2p_swarm::{
45
45
} ;
46
46
use std:: collections:: { hash_map, HashMap , VecDeque } ;
47
47
use std:: io:: { Error , ErrorKind , IoSlice } ;
48
- use std:: ops:: DerefMut ;
49
48
use std:: pin:: Pin ;
50
49
use std:: task:: { Context , Poll } ;
51
50
use transport:: Transport ;
@@ -387,32 +386,43 @@ impl NetworkBehaviour for Behaviour {
387
386
}
388
387
}
389
388
390
- /// A [`NegotiatedSubstream`] acting as a [`Connection`].
391
- pub enum Connection {
389
+ /// Represents a connection to another peer via a relay.
390
+ ///
391
+ /// Internally, this uses a stream to the relay.
392
+ pub struct Connection {
393
+ state : ConnectionState ,
394
+ }
395
+
396
+ enum ConnectionState {
392
397
InboundAccepting {
393
- accept : BoxFuture < ' static , Result < Connection , Error > > ,
398
+ accept : BoxFuture < ' static , Result < ConnectionState , Error > > ,
394
399
} ,
395
400
Operational {
396
401
read_buffer : Bytes ,
397
402
substream : NegotiatedSubstream ,
403
+ /// "Drop notifier" pattern to signal to the transport that the connection has been dropped.
404
+ ///
405
+ /// This is flagged as "dead-code" by the compiler because we never read from it here.
406
+ /// However, it is actual use is to trigger the `Canceled` error in the `Transport` when this `Sender` is dropped.
407
+ #[ allow( dead_code) ]
398
408
drop_notifier : oneshot:: Sender < void:: Void > ,
399
409
} ,
400
410
}
401
411
402
- impl Unpin for Connection { }
412
+ impl Unpin for ConnectionState { }
403
413
404
- impl Connection {
414
+ impl ConnectionState {
405
415
pub ( crate ) fn new_inbound (
406
416
circuit : inbound_stop:: Circuit ,
407
417
drop_notifier : oneshot:: Sender < void:: Void > ,
408
418
) -> Self {
409
- Connection :: InboundAccepting {
419
+ ConnectionState :: InboundAccepting {
410
420
accept : async {
411
421
let ( substream, read_buffer) = circuit
412
422
. accept ( )
413
423
. await
414
424
. map_err ( |e| Error :: new ( ErrorKind :: Other , e) ) ?;
415
- Ok ( Connection :: Operational {
425
+ Ok ( ConnectionState :: Operational {
416
426
read_buffer,
417
427
substream,
418
428
drop_notifier,
@@ -427,7 +437,7 @@ impl Connection {
427
437
read_buffer : Bytes ,
428
438
drop_notifier : oneshot:: Sender < void:: Void > ,
429
439
) -> Self {
430
- Connection :: Operational {
440
+ ConnectionState :: Operational {
431
441
substream,
432
442
read_buffer,
433
443
drop_notifier,
@@ -442,35 +452,41 @@ impl AsyncWrite for Connection {
442
452
buf : & [ u8 ] ,
443
453
) -> Poll < Result < usize , Error > > {
444
454
loop {
445
- match self . deref_mut ( ) {
446
- Connection :: InboundAccepting { accept } => {
447
- * self = ready ! ( accept. poll_unpin( cx) ) ?;
455
+ match & mut self . state {
456
+ ConnectionState :: InboundAccepting { accept } => {
457
+ * self = Connection {
458
+ state : ready ! ( accept. poll_unpin( cx) ) ?,
459
+ } ;
448
460
}
449
- Connection :: Operational { substream, .. } => {
461
+ ConnectionState :: Operational { substream, .. } => {
450
462
return Pin :: new ( substream) . poll_write ( cx, buf) ;
451
463
}
452
464
}
453
465
}
454
466
}
455
467
fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Result < ( ) , Error > > {
456
468
loop {
457
- match self . deref_mut ( ) {
458
- Connection :: InboundAccepting { accept } => {
459
- * self = ready ! ( accept. poll_unpin( cx) ) ?;
469
+ match & mut self . state {
470
+ ConnectionState :: InboundAccepting { accept } => {
471
+ * self = Connection {
472
+ state : ready ! ( accept. poll_unpin( cx) ) ?,
473
+ } ;
460
474
}
461
- Connection :: Operational { substream, .. } => {
475
+ ConnectionState :: Operational { substream, .. } => {
462
476
return Pin :: new ( substream) . poll_flush ( cx) ;
463
477
}
464
478
}
465
479
}
466
480
}
467
481
fn poll_close ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Result < ( ) , Error > > {
468
482
loop {
469
- match self . deref_mut ( ) {
470
- Connection :: InboundAccepting { accept } => {
471
- * self = ready ! ( accept. poll_unpin( cx) ) ?;
483
+ match & mut self . state {
484
+ ConnectionState :: InboundAccepting { accept } => {
485
+ * self = Connection {
486
+ state : ready ! ( accept. poll_unpin( cx) ) ?,
487
+ } ;
472
488
}
473
- Connection :: Operational { substream, .. } => {
489
+ ConnectionState :: Operational { substream, .. } => {
474
490
return Pin :: new ( substream) . poll_close ( cx) ;
475
491
}
476
492
}
@@ -483,11 +499,13 @@ impl AsyncWrite for Connection {
483
499
bufs : & [ IoSlice ] ,
484
500
) -> Poll < Result < usize , Error > > {
485
501
loop {
486
- match self . deref_mut ( ) {
487
- Connection :: InboundAccepting { accept } => {
488
- * self = ready ! ( accept. poll_unpin( cx) ) ?;
502
+ match & mut self . state {
503
+ ConnectionState :: InboundAccepting { accept } => {
504
+ * self = Connection {
505
+ state : ready ! ( accept. poll_unpin( cx) ) ?,
506
+ } ;
489
507
}
490
- Connection :: Operational { substream, .. } => {
508
+ ConnectionState :: Operational { substream, .. } => {
491
509
return Pin :: new ( substream) . poll_write_vectored ( cx, bufs) ;
492
510
}
493
511
}
@@ -502,11 +520,13 @@ impl AsyncRead for Connection {
502
520
buf : & mut [ u8 ] ,
503
521
) -> Poll < Result < usize , Error > > {
504
522
loop {
505
- match self . deref_mut ( ) {
506
- Connection :: InboundAccepting { accept } => {
507
- * self = ready ! ( accept. poll_unpin( cx) ) ?;
523
+ match & mut self . state {
524
+ ConnectionState :: InboundAccepting { accept } => {
525
+ * self = Connection {
526
+ state : ready ! ( accept. poll_unpin( cx) ) ?,
527
+ } ;
508
528
}
509
- Connection :: Operational {
529
+ ConnectionState :: Operational {
510
530
read_buffer,
511
531
substream,
512
532
..
0 commit comments