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