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