|
3 | 3 | //! ## Example |
4 | 4 | //! |
5 | 5 | //! ```no_run |
6 | | -//! # use std::sync::Arc; |
7 | 6 | //! # use anyhow::Result; |
8 | 7 | //! # use futures_lite::future::Boxed as BoxedFuture; |
9 | 8 | //! # use iroh::{endpoint::Connecting, protocol::{ProtocolHandler, Router}, Endpoint, NodeAddr}; |
10 | 9 | //! # |
11 | 10 | //! # async fn test_compile() -> Result<()> { |
12 | 11 | //! let endpoint = Endpoint::builder().discovery_n0().bind().await?; |
13 | 12 | //! |
14 | | -//! const ALPN: &[u8] = b"/my/alpn"; |
15 | 13 | //! let router = Router::builder(endpoint) |
16 | | -//! .accept(&ALPN, Arc::new(Echo)) |
| 14 | +//! .accept(b"/my/alpn", Echo) |
17 | 15 | //! .spawn() |
18 | 16 | //! .await?; |
19 | 17 | //! # Ok(()) |
|
24 | 22 | //! struct Echo; |
25 | 23 | //! |
26 | 24 | //! impl ProtocolHandler for Echo { |
27 | | -//! fn accept(self: Arc<Self>, connecting: Connecting) -> BoxedFuture<Result<()>> { |
| 25 | +//! fn accept(&self, connecting: Connecting) -> BoxedFuture<Result<()>> { |
28 | 26 | //! Box::pin(async move { |
29 | 27 | //! let connection = connecting.await?; |
30 | 28 | //! let (mut send, mut recv) = connection.accept_bi().await?; |
@@ -111,39 +109,59 @@ pub trait ProtocolHandler: Send + Sync + std::fmt::Debug + 'static { |
111 | 109 | /// Handle an incoming connection. |
112 | 110 | /// |
113 | 111 | /// This runs on a freshly spawned tokio task so this can be long-running. |
114 | | - fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>>; |
| 112 | + fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>>; |
115 | 113 |
|
116 | 114 | /// Called when the node shuts down. |
117 | | - fn shutdown(self: Arc<Self>) -> BoxedFuture<()> { |
| 115 | + fn shutdown(&self) -> BoxedFuture<()> { |
118 | 116 | Box::pin(async move {}) |
119 | 117 | } |
120 | 118 | } |
121 | 119 |
|
| 120 | +impl<T: ProtocolHandler> ProtocolHandler for Arc<T> { |
| 121 | + fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>> { |
| 122 | + self.as_ref().accept(conn) |
| 123 | + } |
| 124 | + |
| 125 | + fn shutdown(&self) -> BoxedFuture<()> { |
| 126 | + self.as_ref().shutdown() |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<T: ProtocolHandler> ProtocolHandler for Box<T> { |
| 131 | + fn accept(&self, conn: Connecting) -> BoxedFuture<Result<()>> { |
| 132 | + self.as_ref().accept(conn) |
| 133 | + } |
| 134 | + |
| 135 | + fn shutdown(&self) -> BoxedFuture<()> { |
| 136 | + self.as_ref().shutdown() |
| 137 | + } |
| 138 | +} |
| 139 | + |
122 | 140 | /// A typed map of protocol handlers, mapping them from ALPNs. |
123 | | -#[derive(Debug, Clone, Default)] |
124 | | -pub struct ProtocolMap(BTreeMap<Vec<u8>, Arc<dyn ProtocolHandler>>); |
| 141 | +#[derive(Debug, Default)] |
| 142 | +pub(crate) struct ProtocolMap(BTreeMap<Vec<u8>, Box<dyn ProtocolHandler>>); |
125 | 143 |
|
126 | 144 | impl ProtocolMap { |
127 | 145 | /// Returns the registered protocol handler for an ALPN as a [`Arc<dyn ProtocolHandler>`]. |
128 | | - pub fn get(&self, alpn: &[u8]) -> Option<Arc<dyn ProtocolHandler>> { |
129 | | - self.0.get(alpn).cloned() |
| 146 | + pub(crate) fn get(&self, alpn: &[u8]) -> Option<&dyn ProtocolHandler> { |
| 147 | + self.0.get(alpn).map(|p| &**p) |
130 | 148 | } |
131 | 149 |
|
132 | 150 | /// Inserts a protocol handler. |
133 | | - pub fn insert(&mut self, alpn: Vec<u8>, handler: Arc<dyn ProtocolHandler>) { |
| 151 | + pub(crate) fn insert(&mut self, alpn: Vec<u8>, handler: Box<dyn ProtocolHandler>) { |
134 | 152 | self.0.insert(alpn, handler); |
135 | 153 | } |
136 | 154 |
|
137 | 155 | /// Returns an iterator of all registered ALPN protocol identifiers. |
138 | | - pub fn alpns(&self) -> impl Iterator<Item = &Vec<u8>> { |
| 156 | + pub(crate) fn alpns(&self) -> impl Iterator<Item = &Vec<u8>> { |
139 | 157 | self.0.keys() |
140 | 158 | } |
141 | 159 |
|
142 | 160 | /// Shuts down all protocol handlers. |
143 | 161 | /// |
144 | 162 | /// Calls and awaits [`ProtocolHandler::shutdown`] for all registered handlers concurrently. |
145 | | - pub async fn shutdown(&self) { |
146 | | - let handlers = self.0.values().cloned().map(ProtocolHandler::shutdown); |
| 163 | + pub(crate) async fn shutdown(&self) { |
| 164 | + let handlers = self.0.values().map(|p| p.shutdown()); |
147 | 165 | join_all(handlers).await; |
148 | 166 | } |
149 | 167 | } |
@@ -201,7 +219,8 @@ impl RouterBuilder { |
201 | 219 |
|
202 | 220 | /// Configures the router to accept the [`ProtocolHandler`] when receiving a connection |
203 | 221 | /// with this `alpn`. |
204 | | - pub fn accept(mut self, alpn: impl AsRef<[u8]>, handler: Arc<dyn ProtocolHandler>) -> Self { |
| 222 | + pub fn accept<T: ProtocolHandler>(mut self, alpn: impl AsRef<[u8]>, handler: T) -> Self { |
| 223 | + let handler = Box::new(handler); |
205 | 224 | self.protocols.insert(alpn.as_ref().to_vec(), handler); |
206 | 225 | self |
207 | 226 | } |
|
0 commit comments