1- use std:: { env, future:: Future , str:: FromStr , time:: Instant } ;
1+ use std:: {
2+ env,
3+ future:: Future ,
4+ str:: FromStr ,
5+ time:: { Duration , Instant } ,
6+ } ;
27
38use clap:: Parser ;
49use data_encoding:: HEXLOWER ;
510use iroh:: {
611 SecretKey ,
7- endpoint:: { Connecting , Connection } ,
12+ endpoint:: { Connecting , Connection , ZRTTConnection } ,
813} ;
914use iroh_base:: ticket:: EndpointTicket ;
1015use n0_future:: { StreamExt , future} ;
1116use n0_snafu:: ResultExt ;
1217use n0_watcher:: Watcher ;
18+ use quinn:: VarInt ;
1319use tracing:: { info, trace} ;
1420
1521const PINGPONG_ALPN : & [ u8 ] = b"0rtt-pingpong" ;
@@ -50,7 +56,7 @@ pub fn get_or_generate_secret_key() -> n0_snafu::Result<SecretKey> {
5056/// read the response immediately. Otherwise, the stream pair is bad and we need
5157/// to open a new stream pair.
5258async fn pingpong (
53- connection : & Connection ,
59+ connection : & Conn ,
5460 proceed : impl Future < Output = bool > ,
5561 x : u64 ,
5662) -> n0_snafu:: Result < ( ) > {
@@ -74,16 +80,46 @@ async fn pingpong(
7480 Ok ( ( ) )
7581}
7682
77- async fn pingpong_0rtt ( connecting : Connecting , i : u64 ) -> n0_snafu:: Result < Connection > {
83+ enum Conn {
84+ ZRTT ( ZRTTConnection ) ,
85+ Full ( Connection ) ,
86+ }
87+
88+ impl Conn {
89+ fn open_bi ( & self ) -> quinn:: OpenBi < ' _ > {
90+ match self {
91+ Conn :: ZRTT ( conn) => conn. open_bi ( ) ,
92+ Conn :: Full ( conn) => conn. open_bi ( ) ,
93+ }
94+ }
95+
96+ fn close ( & self , error_code : VarInt , reason : & [ u8 ] ) {
97+ match self {
98+ Conn :: ZRTT ( conn) => conn. close ( error_code, reason) ,
99+ Conn :: Full ( conn) => conn. close ( error_code, reason) ,
100+ }
101+ }
102+
103+ fn rtt ( & self ) -> Duration {
104+ match self {
105+ Conn :: ZRTT ( conn) => conn. rtt ( ) ,
106+ Conn :: Full ( conn) => conn. rtt ( ) ,
107+ }
108+ }
109+ }
110+
111+ async fn pingpong_0rtt ( connecting : Connecting , i : u64 ) -> n0_snafu:: Result < Conn > {
78112 let connection = match connecting. into_0rtt ( ) {
79113 Ok ( ( connection, accepted) ) => {
80114 trace ! ( "0-RTT possible from our side" ) ;
115+ let connection = Conn :: ZRTT ( connection) ;
81116 pingpong ( & connection, accepted, i) . await ?;
82117 connection
83118 }
84119 Err ( connecting) => {
85120 trace ! ( "0-RTT not possible from our side" ) ;
86121 let connection = connecting. await . e ( ) ?;
122+ let connection = Conn :: Full ( connection) ;
87123 pingpong ( & connection, future:: ready ( true ) , i) . await ?;
88124 connection
89125 }
@@ -106,6 +142,7 @@ async fn connect(args: Args) -> n0_snafu::Result<()> {
106142 . await ?;
107143 let connection = if args. disable_0rtt {
108144 let connection = connecting. await . e ( ) ?;
145+ let connection = Conn :: Full ( connection) ;
109146 trace ! ( "connecting without 0-RTT" ) ;
110147 pingpong ( & connection, future:: ready ( true ) , i) . await ?;
111148 connection
0 commit comments