Skip to content

Commit 76f94ff

Browse files
author
“ramfox”
committed
feat: make Connection::remote_id and Connection::alpn infallible
1 parent 45e1f48 commit 76f94ff

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,14 +1,20 @@
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
EndpointId, SecretKey,
7-
endpoint::{Connecting, Connection},
12+
endpoint::{Connecting, Connection, ZRTTConnection},
813
};
914
use n0_future::{StreamExt, future};
1015
use n0_snafu::ResultExt;
1116
use n0_watcher::Watcher;
17+
use quinn::VarInt;
1218
use tracing::{info, trace};
1319

1420
const PINGPONG_ALPN: &[u8] = b"0rtt-pingpong";
@@ -49,7 +55,7 @@ pub fn get_or_generate_secret_key() -> n0_snafu::Result<SecretKey> {
4955
/// read the response immediately. Otherwise, the stream pair is bad and we need
5056
/// to open a new stream pair.
5157
async fn pingpong(
52-
connection: &Connection,
58+
connection: &Conn,
5359
proceed: impl Future<Output = bool>,
5460
x: u64,
5561
) -> n0_snafu::Result<()> {
@@ -73,16 +79,46 @@ async fn pingpong(
7379
Ok(())
7480
}
7581

76-
async fn pingpong_0rtt(connecting: Connecting, i: u64) -> n0_snafu::Result<Connection> {
82+
enum Conn {
83+
ZRTT(ZRTTConnection),
84+
Full(Connection),
85+
}
86+
87+
impl Conn {
88+
fn open_bi(&self) -> quinn::OpenBi<'_> {
89+
match self {
90+
Conn::ZRTT(conn) => conn.open_bi(),
91+
Conn::Full(conn) => conn.open_bi(),
92+
}
93+
}
94+
95+
fn close(&self, error_code: VarInt, reason: &[u8]) {
96+
match self {
97+
Conn::ZRTT(conn) => conn.close(error_code, reason),
98+
Conn::Full(conn) => conn.close(error_code, reason),
99+
}
100+
}
101+
102+
fn rtt(&self) -> Duration {
103+
match self {
104+
Conn::ZRTT(conn) => conn.rtt(),
105+
Conn::Full(conn) => conn.rtt(),
106+
}
107+
}
108+
}
109+
110+
async fn pingpong_0rtt(connecting: Connecting, i: u64) -> n0_snafu::Result<Conn> {
77111
let connection = match connecting.into_0rtt() {
78112
Ok((connection, accepted)) => {
79113
trace!("0-RTT possible from our side");
114+
let connection = Conn::ZRTT(connection);
80115
pingpong(&connection, accepted, i).await?;
81116
connection
82117
}
83118
Err(connecting) => {
84119
trace!("0-RTT not possible from our side");
85120
let connection = connecting.await.e()?;
121+
let connection = Conn::Full(connection);
86122
pingpong(&connection, future::ready(true), i).await?;
87123
connection
88124
}
@@ -105,6 +141,7 @@ async fn connect(args: Args) -> n0_snafu::Result<()> {
105141
.await?;
106142
let connection = if args.disable_0rtt {
107143
let connection = connecting.await.e()?;
144+
let connection = Conn::Full(connection);
108145
trace!("connecting without 0-RTT");
109146
pingpong(&connection, future::ready(true), i).await?;
110147
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
@@ -89,7 +89,7 @@ async fn start_accept_side() -> Result<Endpoint> {
8989
let connection = incoming.await.e()?;
9090

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

9595
// 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
@@ -71,7 +71,7 @@ async fn main() -> n0_snafu::Result<()> {
7171
};
7272
let alpn = connecting.alpn().await?;
7373
let conn = connecting.await.e()?;
74-
let endpoint_id = conn.remote_id()?;
74+
let endpoint_id = conn.remote_id();
7575
info!(
7676
"new connection from {endpoint_id} with ALPN {}",
7777
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
@@ -340,7 +340,7 @@ async fn provide(endpoint: Endpoint, size: u64) -> Result<()> {
340340
let endpoint_clone = endpoint.clone();
341341
tokio::spawn(async move {
342342
let conn = connecting.await.e()?;
343-
let endpoint_id = conn.remote_id()?;
343+
let endpoint_id = conn.remote_id();
344344
info!(
345345
"new connection from {endpoint_id} with ALPN {}",
346346
String::from_utf8_lossy(TRANSFER_ALPN),

0 commit comments

Comments
 (0)