Expand description
Tools for spawning an accept loop that routes incoming requests to the right protocol.
+Expand description
Tools for spawning an accept loop that routes incoming requests to the right protocol.
§Example
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
-const ALPN: &[u8] = b"/my/alpn";
let router = Router::builder(endpoint)
- .accept(&ALPN, Arc::new(Echo))
+ .accept(b"/my/alpn", Echo)
.spawn()
.await?;
@@ -13,7 +12,7 @@
struct Echo;
impl ProtocolHandler for Echo {
- fn accept(self: Arc<Self>, connecting: Connecting) -> BoxedFuture<Result<()>> {
+ fn accept(&self, connecting: Connecting) -> BoxedFuture<Result<()>> {
Box::pin(async move {
let connection = connecting.await?;
let (mut send, mut recv) = connection.accept_bi().await?;
diff --git a/pr/3010/docs/iroh/protocol/struct.ProtocolMap.html b/pr/3010/docs/iroh/protocol/struct.ProtocolMap.html
index 86a41dbef4..3771075c5a 100644
--- a/pr/3010/docs/iroh/protocol/struct.ProtocolMap.html
+++ b/pr/3010/docs/iroh/protocol/struct.ProtocolMap.html
@@ -1,10 +1,10 @@
-ProtocolMap in iroh::protocol - Rust Struct iroh::protocol::ProtocolMap
source · pub struct ProtocolMap(/* private fields */);
Expand description
A typed map of protocol handlers, mapping them from ALPNs.
-Implementations§
source§impl ProtocolMap
sourcepub fn get(&self, alpn: &[u8]) -> Option<&dyn ProtocolHandler>
Returns the registered protocol handler for an ALPN as a Arc<dyn ProtocolHandler>.
-sourcepub fn insert(&mut self, alpn: Vec<u8>, handler: impl ProtocolHandler)
Inserts a protocol handler.
-sourcepub fn alpns(&self) -> impl Iterator<Item = &Vec<u8>>
Returns an iterator of all registered ALPN protocol identifiers.
-sourcepub async fn shutdown(&self)
Shuts down all protocol handlers.
+ProtocolMap in iroh::protocol - Rust Struct iroh::protocol::ProtocolMap
source · pub struct ProtocolMap(/* private fields */);
Expand description
A typed map of protocol handlers, mapping them from ALPNs.
+Implementations§
source§impl ProtocolMap
sourcepub fn get(&self, alpn: &[u8]) -> Option<&dyn ProtocolHandler>
Returns the registered protocol handler for an ALPN as a Arc<dyn ProtocolHandler>.
+sourcepub fn insert(&mut self, alpn: Vec<u8>, handler: impl ProtocolHandler)
Inserts a protocol handler.
+sourcepub fn alpns(&self) -> impl Iterator<Item = &Vec<u8>>
Returns an iterator of all registered ALPN protocol identifiers.
+sourcepub async fn shutdown(&self)
Shuts down all protocol handlers.
Calls and awaits ProtocolHandler::shutdown for all registered handlers concurrently.
-Trait Implementations§
source§impl Debug for ProtocolMap
source§impl Default for ProtocolMap
source§fn default() -> ProtocolMap
Returns the “default value” for a type. Read moreAuto Trait Implementations§
§impl Freeze for ProtocolMap
§impl !RefUnwindSafe for ProtocolMap
§impl Send for ProtocolMap
§impl Sync for ProtocolMap
§impl Unpin for ProtocolMap
§impl !UnwindSafe for ProtocolMap
Blanket Implementations§
Trait Implementations§
source§impl Debug for ProtocolMap
source§impl Default for ProtocolMap
source§fn default() -> ProtocolMap
Returns the “default value” for a type. Read moreAuto Trait Implementations§
§impl Freeze for ProtocolMap
§impl !RefUnwindSafe for ProtocolMap
§impl Send for ProtocolMap
§impl Sync for ProtocolMap
§impl Unpin for ProtocolMap
§impl !UnwindSafe for ProtocolMap
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§fn implicit(
diff --git a/pr/3010/docs/iroh/protocol/struct.Router.html b/pr/3010/docs/iroh/protocol/struct.Router.html
index 1b04d2cf8a..9b05eb59d2 100644
--- a/pr/3010/docs/iroh/protocol/struct.Router.html
+++ b/pr/3010/docs/iroh/protocol/struct.Router.html
@@ -1,4 +1,4 @@
-Router in iroh::protocol - Rust pub struct Router { /* private fields */ }
Expand description
The built router.
+Router in iroh::protocol - Rust pub struct Router { /* private fields */ }
Expand description
The built router.
Construct this using Router::builder.
When dropped, this will abort listening the tasks, so make sure to store it.
Even with this abort-on-drop behaviour, it’s recommended to call and await
@@ -16,16 +16,16 @@ wait for [tokio::signal::ctrl_c()]:
// wait until the user wants to
tokio::signal::ctrl_c().await?;
router.shutdown().await?;
-Implementations§
source§impl Router
sourcepub fn builder(endpoint: Endpoint) -> RouterBuilder
sourcepub fn is_shutdown(&self) -> bool
Checks if the router is already shutdown.
-Implementations§
source§impl Router
sourcepub fn builder(endpoint: Endpoint) -> RouterBuilder
sourcepub fn is_shutdown(&self) -> bool
Checks if the router is already shutdown.
+sourcepub async fn shutdown(&self) -> Result<()>
Shuts down the accept loop cleanly.
When this function returns, all ProtocolHandlers will be shutdown and
Endpoint::close will have been called.
If already shutdown, it returns Ok.
If some ProtocolHandler panicked in the accept loop, this will propagate
that panic into the result here.
-Trait Implementations§
Auto Trait Implementations§
§impl Freeze for Router
§impl !RefUnwindSafe for Router
§impl Send for Router
§impl Sync for Router
§impl Unpin for Router
§impl !UnwindSafe for Router
Blanket Implementations§
Trait Implementations§
Auto Trait Implementations§
§impl Freeze for Router
§impl !RefUnwindSafe for Router
§impl Send for Router
§impl Sync for Router
§impl Unpin for Router
§impl !UnwindSafe for Router
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§fn implicit(
diff --git a/pr/3010/docs/iroh/protocol/struct.RouterBuilder.html b/pr/3010/docs/iroh/protocol/struct.RouterBuilder.html
index 85a219505e..4fada4cbac 100644
--- a/pr/3010/docs/iroh/protocol/struct.RouterBuilder.html
+++ b/pr/3010/docs/iroh/protocol/struct.RouterBuilder.html
@@ -1,14 +1,14 @@
-RouterBuilder in iroh::protocol - Rust Struct iroh::protocol::RouterBuilder
source · pub struct RouterBuilder { /* private fields */ }
Expand description
Builder for creating a Router for accepting protocols.
-Implementations§
source§impl RouterBuilder
sourcepub fn accept(
+RouterBuilder in iroh::protocol - Rust Struct iroh::protocol::RouterBuilder
source · pub struct RouterBuilder { /* private fields */ }
Expand description
Builder for creating a Router for accepting protocols.
+Implementations§
source§impl RouterBuilder
sourcepub fn accept(
self,
alpn: impl AsRef<[u8]>,
handler: impl ProtocolHandler
) -> Self
Configures the router to accept the ProtocolHandler when receiving a connection
with this alpn.
-Trait Implementations§
Auto Trait Implementations§
§impl Freeze for RouterBuilder
§impl !RefUnwindSafe for RouterBuilder
§impl Send for RouterBuilder
§impl Sync for RouterBuilder
§impl Unpin for RouterBuilder
§impl !UnwindSafe for RouterBuilder
Blanket Implementations§
Trait Implementations§
Auto Trait Implementations§
§impl Freeze for RouterBuilder
§impl !RefUnwindSafe for RouterBuilder
§impl Send for RouterBuilder
§impl Sync for RouterBuilder
§impl Unpin for RouterBuilder
§impl !UnwindSafe for RouterBuilder
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§fn implicit(
diff --git a/pr/3010/docs/iroh/protocol/trait.ProtocolHandler.html b/pr/3010/docs/iroh/protocol/trait.ProtocolHandler.html
index 89afd26248..0595597756 100644
--- a/pr/3010/docs/iroh/protocol/trait.ProtocolHandler.html
+++ b/pr/3010/docs/iroh/protocol/trait.ProtocolHandler.html
@@ -1,4 +1,4 @@
-ProtocolHandler in iroh::protocol - Rust Trait iroh::protocol::ProtocolHandler
source · pub trait ProtocolHandler: Send + Sync + Debug + 'static {
+ProtocolHandler in iroh::protocol - Rust Trait iroh::protocol::ProtocolHandler
source · pub trait ProtocolHandler: Send + Sync + Debug + 'static {
// Required method
fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>>;
@@ -10,7 +10,7 @@
Implement this trait on a struct that should handle incoming connections.
The protocol handler must then be registered on the node for an ALPN protocol with
crate::protocol::RouterBuilder::accept.
-
Required Methods§
sourcefn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>>
Handle an incoming connection.
+Required Methods§
sourcefn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>>
Handle an incoming connection.
This runs on a freshly spawned tokio task so this can be long-running.
-Provided Methods§
Provided Methods§
Implementors§
\ No newline at end of file
diff --git a/pr/3010/docs/src/iroh/protocol.rs.html b/pr/3010/docs/src/iroh/protocol.rs.html
index e81cb29ce9..7d1491928b 100644
--- a/pr/3010/docs/src/iroh/protocol.rs.html
+++ b/pr/3010/docs/src/iroh/protocol.rs.html
@@ -353,14 +353,11 @@
353
354
355
-356
-357
//! Tools for spawning an accept loop that routes incoming requests to the right protocol.
//!
//! ## Example
//!
//! ```no_run
-//! # use std::sync::Arc;
//! # use anyhow::Result;
//! # use futures_lite::future::Boxed as BoxedFuture;
//! # use iroh::{endpoint::Connecting, protocol::{ProtocolHandler, Router}, Endpoint, NodeAddr};
@@ -368,9 +365,8 @@
//! # async fn test_compile() -> Result<()> {
//! let endpoint = Endpoint::builder().discovery_n0().bind().await?;
//!
-//! const ALPN: &[u8] = b"/my/alpn";
//! let router = Router::builder(endpoint)
-//! .accept(&ALPN, Arc::new(Echo))
+//! .accept(b"/my/alpn", Echo)
//! .spawn()
//! .await?;
//! # Ok(())
@@ -381,7 +377,7 @@
//! struct Echo;
//!
//! impl ProtocolHandler for Echo {
-//! fn accept(self: Arc<Self>, connecting: Connecting) -> BoxedFuture<Result<()>> {
+//! fn accept(&self, connecting: Connecting) -> BoxedFuture<Result<()>> {
//! Box::pin(async move {
//! let connection = connecting.await?;
//! let (mut send, mut recv) = connection.accept_bi().await?;