diff --git a/core/src/transport/map_err_dial.rs b/core/src/transport/map_err_dial.rs new file mode 100644 index 00000000..c90f7349 --- /dev/null +++ b/core/src/transport/map_err_dial.rs @@ -0,0 +1,89 @@ +// Copyright 2018 Parity Technologies (UK) Ltd. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use futures::prelude::*; +use multiaddr::Multiaddr; +use std::io::Error as IoError; +use transport::{MuxedTransport, Transport}; + +/// See `Transport::map_err_dial`. +#[derive(Debug, Copy, Clone)] +pub struct MapErrDial { + transport: T, + map: F, +} + +impl MapErrDial { + /// Internal function that builds a `MapErrDial`. + #[inline] + pub(crate) fn new(transport: T, map: F) -> MapErrDial { + MapErrDial { transport, map } + } +} + +impl Transport for MapErrDial +where + T: Transport + 'static, // TODO: 'static :-/ + F: FnOnce(IoError, Multiaddr) -> IoError + Clone + 'static, // TODO: 'static :-/ +{ + type Output = T::Output; + type MultiaddrFuture = T::MultiaddrFuture; + type Listener = T::Listener; + type ListenerUpgrade = T::ListenerUpgrade; + type Dial = Box>; + + fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { + match self.transport.listen_on(addr) { + Ok(l) => Ok(l), + Err((transport, addr)) => Err((MapErrDial { transport, map: self.map }, addr)), + } + } + + fn dial(self, addr: Multiaddr) -> Result { + let map = self.map; + + match self.transport.dial(addr.clone()) { + Ok(future) => { + let future = future.into_future().map_err(move |err| map(err, addr)); + Ok(Box::new(future)) + } + Err((transport, addr)) => Err((MapErrDial { transport, map }, addr)), + } + } + + #[inline] + fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { + self.transport.nat_traversal(server, observed) + } +} + +impl MuxedTransport for MapErrDial +where + T: MuxedTransport + 'static, // TODO: 'static :-/ + F: FnOnce(IoError, Multiaddr) -> IoError + Clone + 'static, // TODO: 'static :-/ +{ + type Incoming = T::Incoming; + type IncomingUpgrade = T::IncomingUpgrade; + + #[inline] + fn next_incoming(self) -> Self::Incoming { + self.transport.next_incoming() + } +} diff --git a/core/src/transport/mod.rs b/core/src/transport/mod.rs index 3f7f532f..814449f2 100644 --- a/core/src/transport/mod.rs +++ b/core/src/transport/mod.rs @@ -42,6 +42,7 @@ pub mod dummy; pub mod interruptible; pub mod map; pub mod map_err; +pub mod map_err_dial; pub mod memory; pub mod muxed; pub mod upgrade; @@ -138,6 +139,18 @@ pub trait Transport { map_err::MapErr::new(self, map_err) } + /// Applies a function on the errors generated by the futures of the `Transport` when dialing. + /// + /// Contrary to `map_err`, this gives access to the `Multiaddr` that we tried to dial. + #[inline] + fn map_err_dial(self, map_err: F) -> map_err_dial::MapErrDial + where + Self: Sized, + F: FnOnce(IoError, Multiaddr) -> IoError + Clone + 'static, // TODO: 'static :-/ + { + map_err_dial::MapErrDial::new(self, map_err) + } + /// Builds a new struct that implements `Transport` that contains both `self` and `other`. /// /// The returned object will redirect its calls to `self`, except that if `listen_on` or `dial`