@@ -13,10 +13,12 @@ use std::pin::Pin;
1313use std:: sync:: atomic:: { AtomicBool , Ordering } ;
1414use std:: sync:: Arc ;
1515use std:: task:: Poll ;
16+ use tracing:: debug;
1617
1718/// A protocol channel.
1819///
1920/// This is the handle that can be sent to other threads.
21+ #[ derive( Clone ) ]
2022pub struct Channel {
2123 inbound_rx : Option < Receiver < Message > > ,
2224 direct_inbound_tx : Sender < Message > ,
@@ -44,6 +46,25 @@ impl fmt::Debug for Channel {
4446}
4547
4648impl Channel {
49+ fn new (
50+ inbound_rx : Option < Receiver < Message > > ,
51+ direct_inbound_tx : Sender < Message > ,
52+ outbound_tx : Sender < Vec < ChannelMessage > > ,
53+ discovery_key : DiscoveryKey ,
54+ key : Key ,
55+ local_id : usize ,
56+ closed : Arc < AtomicBool > ,
57+ ) -> Self {
58+ Self {
59+ inbound_rx,
60+ direct_inbound_tx,
61+ outbound_tx,
62+ key,
63+ discovery_key,
64+ local_id,
65+ closed,
66+ }
67+ }
4768 /// Get the discovery key of this channel.
4869 pub fn discovery_key ( & self ) -> & [ u8 ; 32 ] {
4970 & self . discovery_key
@@ -72,6 +93,7 @@ impl Channel {
7293 "Channel is closed" ,
7394 ) ) ;
7495 }
96+ debug ! ( "TX:\n {message:?}\n " ) ;
7597 let message = ChannelMessage :: new ( self . local_id as u64 , message) ;
7698 self . outbound_tx
7799 . send ( vec ! [ message] )
@@ -97,9 +119,13 @@ impl Channel {
97119 "Channel is closed" ,
98120 ) ) ;
99121 }
122+
100123 let messages = messages
101124 . iter ( )
102- . map ( |message| ChannelMessage :: new ( self . local_id as u64 , message. clone ( ) ) )
125+ . map ( |message| {
126+ debug ! ( "TX:\n {message:?}\n " ) ;
127+ ChannelMessage :: new ( self . local_id as u64 , message. clone ( ) )
128+ } )
103129 . collect ( ) ;
104130 self . outbound_tx
105131 . send ( messages)
@@ -120,7 +146,7 @@ impl Channel {
120146 /// you will only want to send a LocalSignal message with this sender to make
121147 /// it clear what event came from the remote peer and what was local
122148 /// signaling.
123- pub fn local_sender ( & mut self ) -> Sender < Message > {
149+ pub fn local_sender ( & self ) -> Sender < Message > {
124150 self . direct_inbound_tx . clone ( )
125151 }
126152
@@ -257,15 +283,16 @@ impl ChannelHandle {
257283 . expect ( "May not open channel that is not locally attached" ) ;
258284
259285 let ( inbound_tx, inbound_rx) = async_channel:: unbounded ( ) ;
260- let channel = Channel {
261- inbound_rx : Some ( inbound_rx) ,
262- direct_inbound_tx : inbound_tx. clone ( ) ,
286+ let channel = Channel :: new (
287+ Some ( inbound_rx) ,
288+ inbound_tx. clone ( ) ,
263289 outbound_tx,
264- discovery_key : self . discovery_key ,
265- key : local_state. key ,
266- local_id : local_state. local_id ,
267- closed : self . closed . clone ( ) ,
268- } ;
290+ self . discovery_key ,
291+ local_state. key ,
292+ local_state. local_id ,
293+ self . closed . clone ( ) ,
294+ ) ;
295+
269296 self . inbound_tx = Some ( inbound_tx) ;
270297 channel
271298 }
@@ -387,7 +414,7 @@ impl ChannelMap {
387414 }
388415 }
389416
390- pub ( crate ) fn has_channel ( & mut self , discovery_key : & [ u8 ] ) -> bool {
417+ pub ( crate ) fn has_channel ( & self , discovery_key : & [ u8 ] ) -> bool {
391418 let hdkey = hex:: encode ( discovery_key) ;
392419 self . channels . contains_key ( & hdkey)
393420 }
0 commit comments