libp2p_tcp/provider.rs
1// Copyright 2020 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! The interface for providers of non-blocking TCP implementations.
22
23#[cfg(feature = "async-io")]
24pub mod async_io;
25
26#[cfg(feature = "tokio")]
27pub mod tokio;
28
29use futures::future::BoxFuture;
30use futures::io::{AsyncRead, AsyncWrite};
31use futures::Stream;
32use if_watch::{IfEvent, IpNet};
33use std::net::{SocketAddr, TcpListener, TcpStream};
34use std::task::{Context, Poll};
35use std::{fmt, io};
36
37/// An incoming connection returned from [`Provider::poll_accept()`].
38pub struct Incoming<S> {
39 pub stream: S,
40 pub local_addr: SocketAddr,
41 pub remote_addr: SocketAddr,
42}
43
44/// The interface for non-blocking TCP I/O providers.
45pub trait Provider: Clone + Send + 'static {
46 /// The type of TCP streams obtained from [`Provider::new_stream`]
47 /// and [`Provider::poll_accept`].
48 type Stream: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug;
49 /// The type of TCP listeners obtained from [`Provider::new_listener`].
50 type Listener: Send + Unpin;
51 /// The type of IfWatcher obtained from [`Provider::new_if_watcher`].
52 type IfWatcher: Stream<Item = io::Result<IfEvent>> + Send + Unpin;
53
54 /// Create a new IfWatcher responsible for detecting IP address changes.
55 fn new_if_watcher() -> io::Result<Self::IfWatcher>;
56
57 /// An iterator over all currently discovered addresses.
58 fn addrs(_: &Self::IfWatcher) -> Vec<IpNet>;
59
60 /// Creates a new listener wrapping the given [`TcpListener`] that
61 /// can be polled for incoming connections via [`Self::poll_accept()`].
62 fn new_listener(_: TcpListener) -> io::Result<Self::Listener>;
63
64 /// Creates a new stream for an outgoing connection, wrapping the
65 /// given [`TcpStream`]. The given `TcpStream` is initiating a
66 /// connection, but implementations must wait for the connection
67 /// setup to complete, i.e. for the stream to be writable.
68 fn new_stream(_: TcpStream) -> BoxFuture<'static, io::Result<Self::Stream>>;
69
70 /// Polls a [`Self::Listener`] for an incoming connection, ensuring a task wakeup,
71 /// if necessary.
72 fn poll_accept(
73 _: &mut Self::Listener,
74 _: &mut Context<'_>,
75 ) -> Poll<io::Result<Incoming<Self::Stream>>>;
76}