99/// grade code you might nevertheless put the tasks into a [tokio::task::JoinSet] or
1010/// [n0_future::FuturesUnordered].
1111mod common;
12- use std::{path::PathBuf, time::Instant};
12+ use std:: { io , path:: PathBuf , time:: Instant } ;
1313
1414use anyhow:: Result ;
1515use async_compression:: tokio:: { bufread:: Lz4Decoder , write:: Lz4Encoder } ;
@@ -22,7 +22,8 @@ use iroh_blobs::{
2222 get:: fsm:: { AtConnected , ConnectedNext , EndBlobNext } ,
2323 protocol:: { ChunkRangesSeq , GetRequest , Request } ,
2424 provider:: {
25- events::{ClientConnected, EventSender, HasErrorCode}, handle_get, AsyncReadRecvStream, AsyncWriteSendStream, ErrorHandler, StreamPair
25+ events:: { ClientConnected , EventSender , HasErrorCode } ,
26+ handle_get, AsyncReadRecvStream , AsyncWriteSendStream , StreamPair ,
2627 } ,
2728 store:: mem:: MemStore ,
2829 ticket:: BlobTicket ,
@@ -50,11 +51,91 @@ pub enum Args {
5051 } ,
5152}
5253
53- type CompressedWriter =
54- AsyncWriteSendStream<async_compression::tokio::write::Lz4Encoder<iroh::endpoint::SendStream>>;
55- type CompressedReader = AsyncReadRecvStream<
54+ struct CompressedWriter ( async_compression:: tokio:: write:: Lz4Encoder < iroh:: endpoint:: SendStream > ) ;
55+ struct CompressedReader (
5656 async_compression:: tokio:: bufread:: Lz4Decoder < BufReader < iroh:: endpoint:: RecvStream > > ,
57- >;
57+ ) ;
58+
59+ impl iroh_blobs:: provider:: SendStream for CompressedWriter {
60+ async fn send_bytes ( & mut self , bytes : bytes:: Bytes ) -> io:: Result < ( ) > {
61+ AsyncWriteSendStream :: new ( self ) . send_bytes ( bytes) . await
62+ }
63+
64+ async fn send < const L : usize > ( & mut self , buf : & [ u8 ; L ] ) -> io:: Result < ( ) > {
65+ AsyncWriteSendStream :: new ( self ) . send ( buf) . await
66+ }
67+
68+ async fn sync ( & mut self ) -> io:: Result < ( ) > {
69+ AsyncWriteSendStream :: new ( self ) . sync ( ) . await
70+ }
71+ }
72+
73+ impl iroh_blobs:: provider:: RecvStream for CompressedReader {
74+ async fn recv_bytes ( & mut self , len : usize ) -> io:: Result < bytes:: Bytes > {
75+ AsyncReadRecvStream :: new ( self ) . recv_bytes ( len) . await
76+ }
77+
78+ async fn recv_bytes_exact ( & mut self , len : usize ) -> io:: Result < bytes:: Bytes > {
79+ AsyncReadRecvStream :: new ( self ) . recv_bytes_exact ( len) . await
80+ }
81+
82+ async fn recv < const L : usize > ( & mut self ) -> io:: Result < [ u8 ; L ] > {
83+ AsyncReadRecvStream :: new ( self ) . recv :: < L > ( ) . await
84+ }
85+ }
86+
87+ impl tokio:: io:: AsyncRead for CompressedReader {
88+ fn poll_read (
89+ mut self : std:: pin:: Pin < & mut Self > ,
90+ cx : & mut std:: task:: Context < ' _ > ,
91+ buf : & mut tokio:: io:: ReadBuf < ' _ > ,
92+ ) -> std:: task:: Poll < io:: Result < ( ) > > {
93+ std:: pin:: Pin :: new ( & mut self . 0 ) . poll_read ( cx, buf)
94+ }
95+ }
96+
97+ impl tokio:: io:: AsyncWrite for CompressedWriter {
98+ fn poll_write (
99+ mut self : std:: pin:: Pin < & mut Self > ,
100+ cx : & mut std:: task:: Context < ' _ > ,
101+ buf : & [ u8 ] ,
102+ ) -> std:: task:: Poll < io:: Result < usize > > {
103+ std:: pin:: Pin :: new ( & mut self . 0 ) . poll_write ( cx, buf)
104+ }
105+
106+ fn poll_flush (
107+ mut self : std:: pin:: Pin < & mut Self > ,
108+ cx : & mut std:: task:: Context < ' _ > ,
109+ ) -> std:: task:: Poll < io:: Result < ( ) > > {
110+ std:: pin:: Pin :: new ( & mut self . 0 ) . poll_flush ( cx)
111+ }
112+
113+ fn poll_shutdown (
114+ mut self : std:: pin:: Pin < & mut Self > ,
115+ cx : & mut std:: task:: Context < ' _ > ,
116+ ) -> std:: task:: Poll < io:: Result < ( ) > > {
117+ std:: pin:: Pin :: new ( & mut self . 0 ) . poll_shutdown ( cx)
118+ }
119+ }
120+
121+ impl iroh_blobs:: provider:: SendStreamSpecific for CompressedWriter {
122+ fn reset ( & mut self , code : quinn:: VarInt ) -> io:: Result < ( ) > {
123+ self . 0 . get_mut ( ) . reset ( code) ?;
124+ Ok ( ( ) )
125+ }
126+
127+ async fn stopped ( & mut self ) -> io:: Result < Option < quinn:: VarInt > > {
128+ let res = self . 0 . get_mut ( ) . stopped ( ) . await ?;
129+ Ok ( res)
130+ }
131+ }
132+
133+ impl iroh_blobs:: provider:: RecvStreamSpecific for CompressedReader {
134+ fn stop ( & mut self , code : quinn:: VarInt ) -> io:: Result < ( ) > {
135+ self . 0 . get_mut ( ) . get_mut ( ) . stop ( code) ?;
136+ Ok ( ( ) )
137+ }
138+ }
58139
59140#[ derive( Debug , Clone ) ]
60141struct CompressedBlobsProtocol {
@@ -71,22 +152,6 @@ impl CompressedBlobsProtocol {
71152 }
72153}
73154
74- struct CompressedErrorHandler;
75-
76- impl ErrorHandler for CompressedErrorHandler {
77- type W = CompressedWriter;
78-
79- type R = CompressedReader;
80-
81- async fn stop(reader: &mut Self::R, code: quinn::VarInt) {
82- reader.0.get_mut().get_mut().stop(code).ok();
83- }
84-
85- async fn reset(writer: &mut Self::W, code: quinn::VarInt) {
86- writer.0.get_mut().reset(code).ok();
87- }
88- }
89-
90155impl ProtocolHandler for CompressedBlobsProtocol {
91156 async fn accept (
92157 & self ,
@@ -108,15 +173,15 @@ impl ProtocolHandler for CompressedBlobsProtocol {
108173 }
109174 while let Ok ( ( send, recv) ) = connection. accept_bi ( ) . await {
110175 let stream_id = send. id ( ) . index ( ) ;
111- let send = TokioStreamWriter (Lz4Encoder::new(send));
112- let recv = TokioStreamReader (Lz4Decoder::new(BufReader::new(recv)));
176+ let send = CompressedWriter ( Lz4Encoder :: new ( send) ) ;
177+ let recv = CompressedReader ( Lz4Decoder :: new ( BufReader :: new ( recv) ) ) ;
113178 let store = self . store . clone ( ) ;
114179 let mut pair =
115180 StreamPair :: new ( connection_id, stream_id, recv, send, self . events . clone ( ) ) ;
116181 tokio:: spawn ( async move {
117182 let request = pair. read_request ( ) . await ?;
118183 if let Request :: Get ( request) = request {
119- handle_get::<CompressedErrorHandler> (pair, store, request).await?;
184+ handle_get ( pair, store, request) . await ?;
120185 }
121186 anyhow:: Ok ( ( ) )
122187 } ) ;
@@ -156,8 +221,8 @@ async fn main() -> Result<()> {
156221 Args :: Get { ticket, target } => {
157222 let conn = endpoint. connect ( ticket. node_addr ( ) . clone ( ) , ALPN ) . await ?;
158223 let ( send, recv) = conn. open_bi ( ) . await ?;
159- let send = AsyncWriteSendStream (Lz4Encoder::new(send));
160- let recv = AsyncReadRecvStream::new (Lz4Decoder::new(BufReader::new(recv)));
224+ let send = CompressedWriter ( Lz4Encoder :: new ( send) ) ;
225+ let recv = CompressedReader ( Lz4Decoder :: new ( BufReader :: new ( recv) ) ) ;
161226 let request = GetRequest {
162227 hash : ticket. hash ( ) ,
163228 ranges : ChunkRangesSeq :: root ( ) ,
0 commit comments