diff --git a/Cargo.lock b/Cargo.lock index 5ff3613..83e483a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1381,8 +1381,7 @@ dependencies = [ [[package]] name = "iroh" version = "0.95.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2374ba3cdaac152dc6ada92d971f7328e6408286faab3b7350842b2ebbed4789" +source = "git+https://github.com/n0-computer/iroh.git?branch=connection-state#a1cca0a22697639b41826eacb8906c98f4c3431d" dependencies = [ "aead", "backon", @@ -1398,7 +1397,7 @@ dependencies = [ "http", "igd-next", "instant", - "iroh-base", + "iroh-base 0.95.1 (git+https://github.com/n0-computer/iroh.git?branch=connection-state)", "iroh-metrics", "iroh-quinn", "iroh-quinn-proto", @@ -1438,6 +1437,17 @@ name = "iroh-base" version = "0.95.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25a8c5fb1cc65589f0d7ab44269a76f615a8c4458356952c9b0ef1c93ea45ff8" +dependencies = [ + "derive_more 2.0.1", + "n0-error", + "serde", + "url", +] + +[[package]] +name = "iroh-base" +version = "0.95.1" +source = "git+https://github.com/n0-computer/iroh.git?branch=connection-state#a1cca0a22697639b41826eacb8906c98f4c3431d" dependencies = [ "curve25519-dalek", "data-encoding", @@ -1535,8 +1545,7 @@ dependencies = [ [[package]] name = "iroh-relay" version = "0.95.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43fbdf2aeffa7d6ede1a31f6570866c2199b1cee96a0b563994623795d1bac2c" +source = "git+https://github.com/n0-computer/iroh.git?branch=connection-state#a1cca0a22697639b41826eacb8906c98f4c3431d" dependencies = [ "blake3", "bytes", @@ -1549,7 +1558,7 @@ dependencies = [ "http-body-util", "hyper", "hyper-util", - "iroh-base", + "iroh-base 0.95.1 (git+https://github.com/n0-computer/iroh.git?branch=connection-state)", "iroh-metrics", "iroh-quinn", "iroh-quinn-proto", @@ -1624,7 +1633,7 @@ dependencies = [ "getrandom 0.3.4", "hex", "iroh", - "iroh-base", + "iroh-base 0.95.1 (registry+https://github.com/rust-lang/crates.io-index)", "irpc", "irpc-derive", "n0-error", diff --git a/Cargo.toml b/Cargo.toml index c62fd37..3087e55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -113,3 +113,6 @@ 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 = "connection-state" } diff --git a/irpc-iroh/src/lib.rs b/irpc-iroh/src/lib.rs index a7b695f..3a8dc1a 100644 --- a/irpc-iroh/src/lib.rs +++ b/irpc-iroh/src/lib.rs @@ -1,18 +1,14 @@ use std::{ - fmt, - future::Future, - io, + fmt, io, sync::{atomic::AtomicU64, Arc}, }; use iroh::{ endpoint::{ - Accepting, ConnectingError, Connection, ConnectionError, IncomingZeroRttConnection, - OutgoingZeroRttConnection, RecvStream, RemoteEndpointIdError, SendStream, VarInt, - ZeroRttStatus, + Accepting, ConnectingError, Connection, ConnectionError, ConnectionState, + OutgoingZeroRttConnection, RecvStream, SendStream, ZeroRttStatus, }, protocol::{AcceptError, ProtocolHandler}, - EndpointId, }; use irpc::{ channel::oneshot, @@ -289,12 +285,13 @@ impl ProtocolHandler for Iroh0RttProtocol< /// Handles a single iroh connection with the provided `handler`. pub async fn handle_connection( - connection: &impl IncomingRemoteConnection, + connection: &Connection, handler: Handler, ) -> io::Result<()> { - if let Ok(remote) = connection.remote_id() { - tracing::Span::current().record("remote", tracing::field::display(remote.fmt_short())); - } + // We might not have a handshaked connection yet, in which case we don't know the remote endpoint id. + // if let Ok(remote) = connection.remote_id() { + // tracing::Span::current().record("remote", tracing::field::display(remote.fmt_short())); + // } debug!("connection accepted"); loop { let Some((msg, rx, tx)) = read_request_raw(connection).await? else { @@ -306,57 +303,13 @@ pub async fn handle_connection( /// Reads a single request from a connection, and a message with channels. pub async fn read_request( - connection: &impl IncomingRemoteConnection, + connection: &Connection, ) -> 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. @@ -365,7 +318,7 @@ impl IncomingRemoteConnection for Connection { /// 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: &impl IncomingRemoteConnection, + connection: &Connection, ) -> std::io::Result> { let (send, mut recv) = match connection.accept_bi().await { Ok((s, r)) => (s, r),