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 = "tokio")]
24pub mod tokio;
25
26use std::{
27 fmt, io,
28 net::{SocketAddr, TcpListener, TcpStream},
29 task::{Context, Poll},
30};
31
32use futures::{
33 future::BoxFuture,
34 io::{AsyncRead, AsyncWrite},
35 Stream,
36};
37use if_watch::{IfEvent, IpNet};
38
39/// An incoming connection returned from [`Provider::poll_accept()`].
40pub struct Incoming<S> {
41 pub stream: S,
42 pub local_addr: SocketAddr,
43 pub remote_addr: SocketAddr,
44}
45
46/// The interface for non-blocking TCP I/O providers.
47pub trait Provider: Clone + Send + 'static {
48 /// The type of TCP streams obtained from [`Provider::new_stream`]
49 /// and [`Provider::poll_accept`].
50 type Stream: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug;
51 /// The type of TCP listeners obtained from [`Provider::new_listener`].
52 type Listener: Send + Unpin;
53 /// The type of IfWatcher obtained from [`Provider::new_if_watcher`].
54 type IfWatcher: Stream<Item = io::Result<IfEvent>> + Send + Unpin;
55
56 /// Create a new IfWatcher responsible for detecting IP address changes.
57 fn new_if_watcher() -> io::Result<Self::IfWatcher>;
58
59 /// An iterator over all currently discovered addresses.
60 fn addrs(_: &Self::IfWatcher) -> Vec<IpNet>;
61
62 /// Creates a new listener wrapping the given [`TcpListener`] that
63 /// can be polled for incoming connections via [`Self::poll_accept()`].
64 fn new_listener(_: TcpListener) -> io::Result<Self::Listener>;
65
66 /// Creates a new stream for an outgoing connection, wrapping the
67 /// given [`TcpStream`]. The given `TcpStream` is initiating a
68 /// connection, but implementations must wait for the connection
69 /// setup to complete, i.e. for the stream to be writable.
70 fn new_stream(_: TcpStream) -> BoxFuture<'static, io::Result<Self::Stream>>;
71
72 /// Polls a [`Self::Listener`] for an incoming connection, ensuring a task wakeup,
73 /// if necessary.
74 fn poll_accept(
75 _: &mut Self::Listener,
76 _: &mut Context<'_>,
77 ) -> Poll<io::Result<Incoming<Self::Stream>>>;
78}