diff --git a/Cargo.lock b/Cargo.lock index cd1c89c..f86b3aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "addr2line" -version = "0.25.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler2" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" - [[package]] name = "aead" version = "0.6.0-rc.2" @@ -102,15 +87,6 @@ dependencies = [ "windows-sys 0.60.2", ] -[[package]] -name = "anyhow" -version = "1.0.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" -dependencies = [ - "backtrace", -] - [[package]] name = "arrayref" version = "0.3.9" @@ -144,7 +120,7 @@ checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -202,21 +178,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "backtrace" -version = "0.3.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-link 0.2.1", -] - [[package]] name = "base16ct" version = "0.3.0" @@ -270,12 +231,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "btparse" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "387e80962b798815a2b5c4bcfdb6bf626fa922ffe9f74e373103b858738e9f31" - [[package]] name = "bumpalo" version = "3.19.0" @@ -389,7 +344,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -407,17 +362,6 @@ dependencies = [ "thiserror 2.0.17", ] -[[package]] -name = "color-backtrace" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e49b1973af2a47b5b44f7dd0a344598da95c872e1556b045607888784e973b91" -dependencies = [ - "backtrace", - "btparse", - "termcolor", -] - [[package]] name = "colorchoice" version = "1.0.4" @@ -589,7 +533,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -644,7 +588,7 @@ checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", "unicode-xid", ] @@ -656,7 +600,7 @@ checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", "unicode-xid", ] @@ -685,7 +629,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -762,7 +706,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -892,7 +836,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -966,12 +910,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "gimli" -version = "0.32.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" - [[package]] name = "glob" version = "0.3.3" @@ -1441,8 +1379,8 @@ dependencies = [ [[package]] name = "iroh" -version = "0.94.0" -source = "git+https://github.com/n0-computer/iroh.git?branch=main#30c23e8dbaa02d17ab57ba41f0aa5271b0a411dc" +version = "0.95.0" +source = "git+https://github.com/n0-computer/iroh.git?branch=Frando/0rtt-api#3ceb8a2d18c9f8d1c76b46ade0293136c20e3bba" dependencies = [ "aead", "backon", @@ -1458,7 +1396,7 @@ dependencies = [ "http", "igd-next", "instant", - "iroh-base 0.94.1", + "iroh-base", "iroh-metrics", "iroh-quinn", "iroh-quinn-proto", @@ -1495,22 +1433,8 @@ dependencies = [ [[package]] name = "iroh-base" -version = "0.94.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db942f6f3d6fa9b475690c6e8e6684d60591dd886bf1bdfef4c60d89d502215c" -dependencies = [ - "derive_more 2.0.1", - "n0-snafu", - "nested_enum_utils", - "serde", - "snafu", - "url", -] - -[[package]] -name = "iroh-base" -version = "0.94.1" -source = "git+https://github.com/n0-computer/iroh.git?branch=main#30c23e8dbaa02d17ab57ba41f0aa5271b0a411dc" +version = "0.95.0" +source = "git+https://github.com/n0-computer/iroh.git?branch=Frando/0rtt-api#3ceb8a2d18c9f8d1c76b46ade0293136c20e3bba" dependencies = [ "curve25519-dalek", "data-encoding", @@ -1548,7 +1472,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -1607,8 +1531,8 @@ dependencies = [ [[package]] name = "iroh-relay" -version = "0.94.0" -source = "git+https://github.com/n0-computer/iroh.git?branch=main#30c23e8dbaa02d17ab57ba41f0aa5271b0a411dc" +version = "0.95.0" +source = "git+https://github.com/n0-computer/iroh.git?branch=Frando/0rtt-api#3ceb8a2d18c9f8d1c76b46ade0293136c20e3bba" dependencies = [ "blake3", "bytes", @@ -1621,7 +1545,7 @@ dependencies = [ "http-body-util", "hyper", "hyper-util", - "iroh-base 0.94.1", + "iroh-base", "iroh-metrics", "iroh-quinn", "iroh-quinn-proto", @@ -1682,7 +1606,7 @@ version = "0.8.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -1694,7 +1618,7 @@ dependencies = [ "getrandom 0.3.4", "hex", "iroh", - "iroh-base 0.94.0", + "iroh-base", "irpc", "irpc-derive", "n0-error", @@ -1839,15 +1763,6 @@ version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" -[[package]] -name = "miniz_oxide" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" -dependencies = [ - "adler2", -] - [[package]] name = "mio" version = "1.1.0" @@ -1897,7 +1812,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -1921,19 +1836,6 @@ dependencies = [ "web-time", ] -[[package]] -name = "n0-snafu" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1815107e577a95bfccedb4cfabc73d709c0db6d12de3f14e0f284a8c5036dc4f" -dependencies = [ - "anyhow", - "btparse", - "color-backtrace", - "snafu", - "tracing-error", -] - [[package]] name = "n0-watcher" version = "0.5.0" @@ -1945,18 +1847,6 @@ dependencies = [ "n0-future", ] -[[package]] -name = "nested_enum_utils" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43fa9161ed44d30e9702fe42bd78693bceac0fed02f647da749f36109023d3a3" -dependencies = [ - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "netdev" version = "0.38.2" @@ -2114,16 +2004,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.107", -] - -[[package]] -name = "object" -version = "0.37.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" -dependencies = [ - "memchr", + "syn", ] [[package]] @@ -2235,7 +2116,7 @@ checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -2359,7 +2240,7 @@ checksum = "e0232bd009a197ceec9cc881ba46f727fcd8060a2d8d6a9dde7a69030a6fe2bb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -2633,12 +2514,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rustc-demangle" -version = "0.1.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" - [[package]] name = "rustc-hash" version = "2.1.1" @@ -2859,7 +2734,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -2991,28 +2866,6 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" -[[package]] -name = "snafu" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e84b3f4eacbf3a1ce05eac6763b4d629d60cbc94d632e4092c54ade71f1e1a2" -dependencies = [ - "backtrace", - "snafu-derive", -] - -[[package]] -name = "snafu-derive" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1c97747dbf44bb1ca44a561ece23508e99cb592e862f22222dcf42f51d1e451" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 2.0.107", -] - [[package]] name = "socket2" version = "0.5.10" @@ -3041,7 +2894,7 @@ checksum = "c87e960f4dca2788eeb86bbdde8dd246be8948790b7618d656e68f9b720a86e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3099,7 +2952,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3108,17 +2961,6 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.107" @@ -3147,7 +2989,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3224,7 +3066,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3235,7 +3077,7 @@ checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3323,7 +3165,7 @@ checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3500,7 +3342,7 @@ checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3513,16 +3355,6 @@ dependencies = [ "valuable", ] -[[package]] -name = "tracing-error" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b1581020d7a273442f5b45074a6a57d5757ad0a47dac0e9f0bd57b81936f3db" -dependencies = [ - "tracing", - "tracing-subscriber", -] - [[package]] name = "tracing-log" version = "0.2.0" @@ -3705,7 +3537,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.107", + "syn", "wasm-bindgen-shared", ] @@ -3740,7 +3572,7 @@ checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3950,7 +3782,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -3961,7 +3793,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -4456,7 +4288,7 @@ checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", "synstructure", ] @@ -4483,7 +4315,7 @@ checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -4503,7 +4335,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", "synstructure", ] @@ -4524,7 +4356,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] [[package]] @@ -4557,5 +4389,5 @@ checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.107", + "syn", ] diff --git a/Cargo.toml b/Cargo.toml index d9d5cb0..cd1be96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,10 +106,11 @@ tracing = { version = "0.1.41", default-features = false } n0-future = { version = "0.3", default-features = false } n0-error = { version = "0.1.0" } tracing-subscriber = { version = "0.3.20" } -iroh = { version = "0.94" } -iroh-base = { version = "0.94" } +iroh = { version = "0.95" } +iroh-base = { version = "0.95" } quinn = { package = "iroh-quinn", version = "0.14.0", default-features = false } futures-util = { version = "0.3", features = ["sink"] } [patch.crates-io] -iroh = { git = "https://github.com/n0-computer/iroh.git", branch = "main" } +iroh = { git = "https://github.com/n0-computer/iroh.git", branch = "Frando/0rtt-api" } +iroh-base = { git = "https://github.com/n0-computer/iroh.git", branch = "Frando/0rtt-api" } diff --git a/irpc-iroh/examples/0rtt.rs b/irpc-iroh/examples/0rtt.rs index f5ef25e..0023b01 100644 --- a/irpc-iroh/examples/0rtt.rs +++ b/irpc-iroh/examples/0rtt.rs @@ -187,9 +187,11 @@ mod cli { mod ping { use futures_util::FutureExt; - use iroh::Endpoint; + use iroh::{endpoint::ZeroRttStatus, Endpoint}; use irpc::{channel::oneshot, rpc::RemoteService, rpc_requests, Client, WithChannels}; - use irpc_iroh::{Iroh0RttProtocol, IrohProtocol, IrohRemoteConnection}; + use irpc_iroh::{ + Iroh0RttProtocol, IrohProtocol, IrohRemoteConnection, IrohZrttRemoteConnection, + }; use n0_error::{Result, StdResultExt}; use n0_future::future; use serde::{Deserialize, Serialize}; @@ -261,11 +263,20 @@ mod ping { .await .std_context("failed to connect to remote service")?; match connecting.into_0rtt() { - Ok((conn, zero_rtt_accepted)) => { + Ok(conn) => { info!("0-RTT possible from our side"); - let fut: future::Boxed = Box::pin(zero_rtt_accepted); + let fut: future::Boxed = Box::pin({ + let conn = conn.clone(); + async move { + match conn.handshake_completed().await { + Err(_) => false, + Ok(ZeroRttStatus::Accepted(_)) => true, + Ok(ZeroRttStatus::Rejected(_)) => false, + } + } + }); Ok(EchoApi { - inner: Client::boxed(IrohRemoteConnection::new(conn)), + inner: Client::boxed(IrohZrttRemoteConnection::new(conn)), zero_rtt_accepted: fut.shared(), }) } diff --git a/irpc-iroh/src/lib.rs b/irpc-iroh/src/lib.rs index d662109..8deb25b 100644 --- a/irpc-iroh/src/lib.rs +++ b/irpc-iroh/src/lib.rs @@ -1,11 +1,17 @@ use std::{ - fmt, io, + fmt, + future::Future, + io, sync::{atomic::AtomicU64, Arc}, }; use iroh::{ - endpoint::{Connecting, Connection, ConnectionError, RecvStream, SendStream}, + endpoint::{ + Accepting, ConnectingError, Connection, ConnectionError, IncomingZeroRttConnection, + OutgoingZeroRttConnection, RecvStream, RemoteEndpointIdError, SendStream, VarInt, + }, protocol::{AcceptError, ProtocolHandler}, + EndpointId, }; use irpc::{ channel::oneshot, @@ -63,6 +69,32 @@ impl irpc::rpc::RemoteConnection for IrohRemoteConnection { } } +#[derive(Debug, Clone)] +pub struct IrohZrttRemoteConnection(OutgoingZeroRttConnection); + +impl IrohZrttRemoteConnection { + pub fn new(connection: OutgoingZeroRttConnection) -> Self { + Self(connection) + } +} + +impl irpc::rpc::RemoteConnection for IrohZrttRemoteConnection { + fn clone_boxed(&self) -> Box { + Box::new(self.clone()) + } + + fn open_bi( + &self, + ) -> n0_future::future::Boxed> + { + let conn = self.0.clone(); + Box::pin(async move { + let (send, recv) = conn.open_bi().await?; + Ok((send, recv)) + }) + } +} + /// A connection to a remote service. /// /// Initially this does just have the endpoint and the address. Once a @@ -166,17 +198,14 @@ impl IrohProtocol { } impl ProtocolHandler for IrohProtocol { - fn accept( - &self, - connection: Connection, - ) -> impl std::future::Future> + Send { + async fn accept(&self, connection: Connection) -> Result<(), AcceptError> { let handler = self.handler.clone(); let request_id = self .request_id .fetch_add(1, std::sync::atomic::Ordering::AcqRel); - let fut = handle_connection(connection, handler).map_err(AcceptError::from_err); + let fut = handle_connection(&connection, handler).map_err(AcceptError::from_err); let span = trace_span!("rpc", id = request_id); - Box::pin(fut.instrument(span)) + fut.instrument(span).await } } @@ -215,30 +244,32 @@ impl Iroh0RttProtocol { } impl ProtocolHandler for Iroh0RttProtocol { - async fn on_connecting(&self, connecting: Connecting) -> Result { - let (conn, _zero_rtt_accepted) = connecting - .into_0rtt() - .expect("accept into 0.5 RTT always succeeds"); - Ok(conn) - } - - fn accept( - &self, - connection: Connection, - ) -> impl std::future::Future> + Send { + async fn on_accepting(&self, accepting: Accepting) -> Result { + let zrtt_conn = accepting.into_0rtt(); let handler = self.handler.clone(); let request_id = self .request_id .fetch_add(1, std::sync::atomic::Ordering::AcqRel); - let fut = handle_connection(connection, handler).map_err(AcceptError::from_err); - let span = trace_span!("rpc", id = request_id); - Box::pin(fut.instrument(span)) + handle_connection(&zrtt_conn, handler) + .map_err(AcceptError::from_err) + .instrument(trace_span!("rpc", id = request_id)) + .await?; + let conn = zrtt_conn + .handshake_completed() + .await + .map_err(|err| AcceptError::from(ConnectingError::from(err)))?; + Ok(conn) + } + + async fn accept(&self, _connection: Connection) -> Result<(), AcceptError> { + // Noop, handled in [`Self::on_accepting`] + Ok(()) } } /// Handles a single iroh connection with the provided `handler`. pub async fn handle_connection( - connection: Connection, + connection: &impl IncomingRemoteConnection, handler: Handler, ) -> io::Result<()> { if let Ok(remote) = connection.remote_id() { @@ -246,21 +277,66 @@ pub async fn handle_connection( } debug!("connection accepted"); loop { - let Some((msg, rx, tx)) = read_request_raw(&connection).await? else { + let Some((msg, rx, tx)) = read_request_raw(connection).await? else { return Ok(()); }; handler(msg, rx, tx).await?; } } +/// Reads a single request from a connection, and a message with channels. pub async fn read_request( - connection: &Connection, + connection: &impl IncomingRemoteConnection, ) -> std::io::Result> { Ok(read_request_raw::(connection) .await? .map(|(msg, rx, tx)| S::with_remote_channels(msg, rx, tx))) } +/// Abstracts over [`Connection`] and [`IncomingZeroRttConnection`]. +/// +/// You don't need to implement this trait yourself. It is used by [`read_request`] and +/// [`handle_connection`] to work with both fully authenticated connections and with +/// 0-RTT connections. +pub trait IncomingRemoteConnection { + /// Accepts a single bidirectional stream. + fn accept_bi( + &self, + ) -> impl Future> + Send; + /// Close the connection. + fn close(&self, error_code: VarInt, reason: &[u8]); + /// Returns the remote's endpoint id. + /// + /// This may only fail for 0-RTT connections. + fn remote_id(&self) -> Result; +} + +impl IncomingRemoteConnection for IncomingZeroRttConnection { + async fn accept_bi(&self) -> Result<(SendStream, RecvStream), ConnectionError> { + self.accept_bi().await + } + + fn close(&self, error_code: VarInt, reason: &[u8]) { + self.close(error_code, reason) + } + fn remote_id(&self) -> Result { + self.remote_id() + } +} + +impl IncomingRemoteConnection for Connection { + async fn accept_bi(&self) -> Result<(SendStream, RecvStream), ConnectionError> { + self.accept_bi().await + } + + fn close(&self, error_code: VarInt, reason: &[u8]) { + self.close(error_code, reason) + } + fn remote_id(&self) -> Result { + Ok(self.remote_id()) + } +} + /// Reads a single request from the connection. /// /// This accepts a bi-directional stream from the connection and reads and parses the request. @@ -269,7 +345,7 @@ pub async fn read_request( /// Returns None if the remote closed the connection with error code `0`. /// Returns an error for all other failure cases. pub async fn read_request_raw( - connection: &Connection, + connection: &impl IncomingRemoteConnection, ) -> std::io::Result> { let (send, mut recv) = match connection.accept_bi().await { Ok((s, r)) => (s, r), @@ -324,7 +400,7 @@ pub async fn listen(endpoint: iroh::Endpoint, han let handler = handler.clone(); let fut = async move { match incoming.await { - Ok(connection) => match handle_connection(connection, handler).await { + Ok(connection) => match handle_connection(&connection, handler).await { Err(err) => warn!("connection closed with error: {err:?}"), Ok(()) => debug!("connection closed"), },