@@ -8,7 +8,6 @@ use std::{
88 task:: { Context , Poll } ,
99} ;
1010
11- use bytes:: BytesMut ;
1211use futures_concurrency:: stream:: { stream_group, StreamGroup } ;
1312use futures_util:: FutureExt as _;
1413use iroh:: {
@@ -32,19 +31,17 @@ use tokio::sync::{broadcast, mpsc, oneshot};
3231use tokio_util:: sync:: CancellationToken ;
3332use tracing:: { debug, error, error_span, trace, warn, Instrument } ;
3433
35- use self :: util:: { read_message , write_message , Timers } ;
34+ use self :: util:: { RecvLoop , SendLoop , Timers } ;
3635use crate :: {
37- api:: RpcMessage ,
36+ api:: { self , Command , Event , GossipApi , RpcMessage } ,
3837 metrics:: Metrics ,
3938 proto:: { self , HyparviewConfig , PeerData , PlumtreeConfig , Scope , TopicId } ,
4039} ;
4140
42- pub mod util;
43-
44- use crate :: api:: { self , Command , Event , GossipApi } ;
41+ mod util;
4542
4643/// ALPN protocol name
47- pub const GOSSIP_ALPN : & [ u8 ] = b"/iroh-gossip/0 " ;
44+ pub const GOSSIP_ALPN : & [ u8 ] = b"/iroh-gossip/1 " ;
4845
4946/// Channel capacity for the send queue (one per connection)
5047const SEND_QUEUE_CAP : usize = 64 ;
@@ -523,10 +520,10 @@ impl Actor {
523520 async move {
524521 let res = connection_loop (
525522 peer_id,
526- & conn,
523+ conn. clone ( ) ,
527524 origin,
528525 send_rx,
529- & in_event_tx,
526+ in_event_tx,
530527 max_message_size,
531528 queue,
532529 )
@@ -855,48 +852,22 @@ impl<T> From<mpsc::error::SendError<T>> for ConnectionLoopError {
855852
856853async fn connection_loop (
857854 from : PublicKey ,
858- conn : & Connection ,
855+ conn : Connection ,
859856 origin : ConnOrigin ,
860- mut send_rx : mpsc:: Receiver < ProtoMessage > ,
861- in_event_tx : & mpsc:: Sender < InEvent > ,
857+ send_rx : mpsc:: Receiver < ProtoMessage > ,
858+ in_event_tx : mpsc:: Sender < InEvent > ,
862859 max_message_size : usize ,
863860 queue : Vec < ProtoMessage > ,
864861) -> Result < ( ) , ConnectionLoopError > {
865- let ( mut send, mut recv) = match origin {
866- ConnOrigin :: Accept => conn. accept_bi ( ) . await ?,
867- ConnOrigin :: Dial => conn. open_bi ( ) . await ?,
868- } ;
869862 debug ! ( ?origin, "connection established" ) ;
870- let mut send_buf = BytesMut :: new ( ) ;
871- let mut recv_buf = BytesMut :: new ( ) ;
872863
873- let send_loop = async {
874- for msg in queue {
875- write_message ( & mut send, & mut send_buf, & msg, max_message_size) . await ?;
876- }
877- while let Some ( msg) = send_rx. recv ( ) . await {
878- write_message ( & mut send, & mut send_buf, & msg, max_message_size) . await ?;
879- }
880- // notify the other node no more data will be sent
881- let _ = send. finish ( ) ;
882- // wait for the other node to ack all the sent data
883- let _ = send. stopped ( ) . await ;
884- Result :: < _ , ConnectionLoopError > :: Ok ( ( ) )
885- } ;
886-
887- let recv_loop = async {
888- loop {
889- let msg = read_message ( & mut recv, & mut recv_buf, max_message_size) . await ?;
864+ let mut send_loop = SendLoop :: new ( conn. clone ( ) , send_rx, max_message_size) ;
865+ let mut recv_loop = RecvLoop :: new ( from, conn, in_event_tx, max_message_size) ;
890866
891- match msg {
892- None => break ,
893- Some ( msg) => in_event_tx. send ( InEvent :: RecvMessage ( from, msg) ) . await ?,
894- }
895- }
896- Result :: < _ , ConnectionLoopError > :: Ok ( ( ) )
897- } ;
867+ let send_fut = send_loop. run ( queue) . instrument ( error_span ! ( "send" ) ) ;
868+ let recv_fut = recv_loop. run ( ) . instrument ( error_span ! ( "recv" ) ) ;
898869
899- let ( send_res, recv_res) = tokio:: join!( send_loop , recv_loop ) ;
870+ let ( send_res, recv_res) = tokio:: join!( send_fut , recv_fut ) ;
900871 send_res?;
901872 recv_res?;
902873 Ok ( ( ) )
0 commit comments