-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add BoxedMuxed transport #459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2f69c0
Add BoxedMuxed transport
tomaka fcaeb43
Extend to both muxed and non-muxed versions
tomaka 3022621
Style
tomaka bf6592a
Implement Debug for boxed transports
tomaka 77a6019
Merge branch 'master' into boxed-transport
tomaka 0f6a634
Merge remote-tracking branch 'upstream/master' into boxed-transport
tomaka 7e35fb3
Merge branch 'master' into boxed-transport
tomaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| // 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::fmt; | ||
| use std::io::Error as IoError; | ||
| use std::sync::Arc; | ||
| use transport::{MuxedTransport, Transport}; | ||
|
|
||
| /// See the `Transport::boxed` method. | ||
| #[inline] | ||
| pub fn boxed<T>(transport: T) -> Boxed<T::Output> | ||
| where | ||
| T: Transport + Clone + Send + Sync + 'static, | ||
| T::Dial: Send + 'static, | ||
| T::Listener: Send + 'static, | ||
| T::ListenerUpgrade: Send + 'static, | ||
| T::MultiaddrFuture: Send + 'static, | ||
| { | ||
| Boxed { | ||
| inner: Arc::new(transport) as Arc<_>, | ||
| } | ||
| } | ||
| /// See the `Transport::boxed_muxed` method. | ||
| #[inline] | ||
| pub fn boxed_muxed<T>(transport: T) -> BoxedMuxed<T::Output> | ||
| where | ||
| T: MuxedTransport + Clone + Send + Sync + 'static, | ||
| T::Dial: Send + 'static, | ||
| T::Listener: Send + 'static, | ||
| T::ListenerUpgrade: Send + 'static, | ||
| T::MultiaddrFuture: Send + 'static, | ||
| T::Incoming: Send + 'static, | ||
| T::IncomingUpgrade: Send + 'static, | ||
| { | ||
| BoxedMuxed { | ||
| inner: Arc::new(transport) as Arc<_>, | ||
| } | ||
| } | ||
|
|
||
| pub type MultiaddrFuture = Box<Future<Item = Multiaddr, Error = IoError> + Send>; | ||
| pub type Dial<O> = Box<Future<Item = (O, MultiaddrFuture), Error = IoError> + Send>; | ||
| pub type Listener<O> = Box<Stream<Item = ListenerUpgrade<O>, Error = IoError> + Send>; | ||
| pub type ListenerUpgrade<O> = Box<Future<Item = (O, MultiaddrFuture), Error = IoError> + Send>; | ||
| pub type Incoming<O> = Box<Future<Item = IncomingUpgrade<O>, Error = IoError> + Send>; | ||
| pub type IncomingUpgrade<O> = Box<Future<Item = (O, MultiaddrFuture), Error = IoError> + Send>; | ||
|
|
||
| trait Abstract<O> { | ||
| fn listen_on(&self, addr: Multiaddr) -> Result<(Listener<O>, Multiaddr), Multiaddr>; | ||
| fn dial(&self, addr: Multiaddr) -> Result<Dial<O>, Multiaddr>; | ||
| fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>; | ||
| } | ||
|
|
||
| impl<T, O> Abstract<O> for T | ||
| where | ||
| T: Transport<Output = O> + Clone + 'static, | ||
| T::Dial: Send + 'static, | ||
| T::Listener: Send + 'static, | ||
| T::ListenerUpgrade: Send + 'static, | ||
| T::MultiaddrFuture: Send + 'static, | ||
| { | ||
| fn listen_on(&self, addr: Multiaddr) -> Result<(Listener<O>, Multiaddr), Multiaddr> { | ||
| let (listener, new_addr) = | ||
| Transport::listen_on(self.clone(), addr).map_err(|(_, addr)| addr)?; | ||
| let fut = listener.map(|upgrade| { | ||
| let fut = upgrade.map(|(out, addr)| (out, Box::new(addr) as MultiaddrFuture)); | ||
| Box::new(fut) as ListenerUpgrade<O> | ||
| }); | ||
| Ok((Box::new(fut) as Box<_>, new_addr)) | ||
| } | ||
|
|
||
| fn dial(&self, addr: Multiaddr) -> Result<Dial<O>, Multiaddr> { | ||
| let fut = Transport::dial(self.clone(), addr) | ||
| .map_err(|(_, addr)| addr)? | ||
| .map(|(out, addr)| (out, Box::new(addr) as MultiaddrFuture)); | ||
| Ok(Box::new(fut) as Box<_>) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> { | ||
| Transport::nat_traversal(self, server, observed) | ||
| } | ||
| } | ||
|
|
||
| trait AbstractMuxed<O>: Abstract<O> { | ||
| fn next_incoming(&self) -> Incoming<O>; | ||
| } | ||
|
|
||
| impl<T, O> AbstractMuxed<O> for T | ||
| where | ||
| T: MuxedTransport<Output = O> + Clone + 'static, | ||
| T::Dial: Send + 'static, | ||
| T::Listener: Send + 'static, | ||
| T::ListenerUpgrade: Send + 'static, | ||
| T::MultiaddrFuture: Send + 'static, | ||
| T::Incoming: Send + 'static, | ||
| T::IncomingUpgrade: Send + 'static, | ||
| { | ||
| fn next_incoming(&self) -> Incoming<O> { | ||
| let fut = MuxedTransport::next_incoming(self.clone()).map(|upgrade| { | ||
| let fut = upgrade.map(|(out, addr)| (out, Box::new(addr) as MultiaddrFuture)); | ||
| Box::new(fut) as IncomingUpgrade<O> | ||
| }); | ||
| Box::new(fut) as Box<_> | ||
| } | ||
| } | ||
|
|
||
| /// See the `Transport::boxed` method. | ||
| pub struct Boxed<O> { | ||
| inner: Arc<Abstract<O> + Send + Sync>, | ||
| } | ||
|
|
||
| impl<O> fmt::Debug for Boxed<O> { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "BoxedTransport") | ||
| } | ||
| } | ||
|
|
||
| impl<O> Clone for Boxed<O> { | ||
| #[inline] | ||
| fn clone(&self) -> Self { | ||
| Boxed { | ||
| inner: self.inner.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<O> Transport for Boxed<O> { | ||
| type Output = O; | ||
| type MultiaddrFuture = MultiaddrFuture; | ||
| type Listener = Listener<O>; | ||
| type ListenerUpgrade = ListenerUpgrade<O>; | ||
| type Dial = Dial<O>; | ||
|
|
||
| #[inline] | ||
| fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { | ||
| match self.inner.listen_on(addr) { | ||
| Ok(listen) => Ok(listen), | ||
| Err(addr) => Err((self, addr)), | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> { | ||
| match self.inner.dial(addr) { | ||
| Ok(dial) => Ok(dial), | ||
| Err(addr) => Err((self, addr)), | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> { | ||
| self.inner.nat_traversal(server, observed) | ||
| } | ||
| } | ||
|
|
||
| /// See the `Transport::boxed_muxed` method. | ||
| pub struct BoxedMuxed<O> { | ||
| inner: Arc<AbstractMuxed<O> + Send + Sync>, | ||
| } | ||
|
|
||
| impl<O> fmt::Debug for BoxedMuxed<O> { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "BoxedMuxedTransport") | ||
| } | ||
| } | ||
|
|
||
| impl<O> Clone for BoxedMuxed<O> { | ||
| #[inline] | ||
| fn clone(&self) -> Self { | ||
| BoxedMuxed { | ||
| inner: self.inner.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<O> Transport for BoxedMuxed<O> { | ||
| type Output = O; | ||
| type MultiaddrFuture = MultiaddrFuture; | ||
| type Listener = Listener<O>; | ||
| type ListenerUpgrade = ListenerUpgrade<O>; | ||
| type Dial = Dial<O>; | ||
|
|
||
| #[inline] | ||
| fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { | ||
| match self.inner.listen_on(addr) { | ||
| Ok(listen) => Ok(listen), | ||
| Err(addr) => Err((self, addr)), | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> { | ||
| match self.inner.dial(addr) { | ||
| Ok(dial) => Ok(dial), | ||
| Err(addr) => Err((self, addr)), | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> { | ||
| self.inner.nat_traversal(server, observed) | ||
| } | ||
| } | ||
|
|
||
| impl<O> MuxedTransport for BoxedMuxed<O> { | ||
| type Incoming = Incoming<O>; | ||
| type IncomingUpgrade = IncomingUpgrade<O>; | ||
|
|
||
| #[inline] | ||
| fn next_incoming(self) -> Self::Incoming { | ||
| self.inner.next_incoming() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this trait?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same as
Transport, but since it doesn't have associated types and doesn't take by ownership, it can be put in aBox.