Skip to content

Commit ab5d85a

Browse files
author
“ramfox”
committed
feat: make Connection::remote_id and Connection::alpn infallible
1 parent 4ed700e commit ab5d85a

File tree

12 files changed

+450
-80
lines changed

12 files changed

+450
-80
lines changed

iroh/examples/0rtt.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
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

38
use clap::Parser;
49
use data_encoding::HEXLOWER;
510
use iroh::{
611
SecretKey,
7-
endpoint::{Connecting, Connection},
12+
endpoint::{Connecting, Connection, ZRTTConnection},
813
};
914
use iroh_base::ticket::EndpointTicket;
1015
use n0_future::{StreamExt, future};
1116
use n0_snafu::ResultExt;
1217
use n0_watcher::Watcher;
18+
use quinn::VarInt;
1319
use tracing::{info, trace};
1420

1521
const 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.
5258
async 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

iroh/examples/dht_discovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async fn chat_server(args: Args) -> n0_snafu::Result<()> {
8989
};
9090
tokio::spawn(async move {
9191
let connection = connecting.await.e()?;
92-
let remote_endpoint_id = connection.remote_id()?;
92+
let remote_endpoint_id = connection.remote_id();
9393
println!("got connection from {remote_endpoint_id}");
9494
// just leave the tasks hanging. this is just an example.
9595
let (mut writer, mut reader) = connection.accept_bi().await.e()?;

iroh/examples/echo-no-router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async fn start_accept_side() -> Result<Endpoint> {
9090
let connection = incoming.await.e()?;
9191

9292
// We can get the remote's endpoint id from the connection.
93-
let endpoint_id = connection.remote_id()?;
93+
let endpoint_id = connection.remote_id();
9494
println!("accepted connection from {endpoint_id}");
9595

9696
// Our protocol is a simple request-response protocol, so we expect the

iroh/examples/echo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl ProtocolHandler for Echo {
8686
/// the connection lasts.
8787
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
8888
// We can get the remote's endpoint id from the connection.
89-
let endpoint_id = connection.remote_id()?;
89+
let endpoint_id = connection.remote_id();
9090
println!("accepted connection from {endpoint_id}");
9191

9292
// Our protocol is a simple request-response protocol, so we expect the

iroh/examples/listen-unreliable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async fn main() -> Result<()> {
7373
};
7474
let alpn = connecting.alpn().await?;
7575
let conn = connecting.await.e()?;
76-
let endpoint_id = conn.remote_id()?;
76+
let endpoint_id = conn.remote_id();
7777
info!(
7878
"new (unreliable) connection from {endpoint_id} with ALPN {}",
7979
String::from_utf8_lossy(&alpn),

iroh/examples/listen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async fn main() -> n0_snafu::Result<()> {
7272
};
7373
let alpn = connecting.alpn().await?;
7474
let conn = connecting.await.e()?;
75-
let endpoint_id = conn.remote_id()?;
75+
let endpoint_id = conn.remote_id();
7676
info!(
7777
"new connection from {endpoint_id} with ALPN {}",
7878
String::from_utf8_lossy(&alpn),

iroh/examples/screening-connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl ProtocolHandler for ScreenedEcho {
125125
/// the connection lasts.
126126
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
127127
// We can get the remote's endpoint id from the connection.
128-
let endpoint_id = connection.remote_id()?;
128+
let endpoint_id = connection.remote_id();
129129
println!("accepted connection from {endpoint_id}");
130130

131131
// Our protocol is a simple request-response protocol, so we expect the

iroh/examples/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl ProtocolHandler for BlobSearch {
128128
/// the connection lasts.
129129
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
130130
// We can get the remote's endpoint id from the connection.
131-
let endpoint_id = connection.remote_id()?;
131+
let endpoint_id = connection.remote_id();
132132
println!("accepted connection from {endpoint_id}");
133133

134134
// Our protocol is a simple request-response protocol, so we expect the

iroh/examples/transfer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ async fn provide(endpoint: Endpoint, size: u64) -> Result<()> {
329329
let endpoint_clone = endpoint.clone();
330330
tokio::spawn(async move {
331331
let conn = connecting.await.e()?;
332-
let endpoint_id = conn.remote_id()?;
332+
let endpoint_id = conn.remote_id();
333333
info!(
334334
"new connection from {endpoint_id} with ALPN {}",
335335
String::from_utf8_lossy(TRANSFER_ALPN),

0 commit comments

Comments
 (0)