litep2p/transport/manager/
types.rs

1// Copyright 2023 litep2p developers
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
21use crate::{
22    transport::manager::address::{AddressRecord, AddressStore},
23    types::ConnectionId,
24};
25
26use multiaddr::Multiaddr;
27
28use std::collections::{HashMap, HashSet};
29
30/// Supported protocols.
31#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
32pub enum SupportedTransport {
33    /// TCP.
34    Tcp,
35
36    /// QUIC.
37    #[cfg(feature = "quic")]
38    Quic,
39
40    /// WebRTC
41    #[cfg(feature = "webrtc")]
42    WebRtc,
43
44    /// WebSocket
45    #[cfg(feature = "websocket")]
46    WebSocket,
47}
48
49/// Peer state.
50#[derive(Debug)]
51pub enum PeerState {
52    /// `Litep2p` is connected to peer.
53    Connected {
54        /// Address record.
55        record: AddressRecord,
56
57        /// Dial address, if it exists.
58        ///
59        /// While the local node was dialing a remote peer, the remote peer might've dialed
60        /// the local node and connection was established successfully. This dial address
61        /// is stored for processing later when the dial attempt concluded as either
62        /// successful/failed.
63        dial_record: Option<AddressRecord>,
64    },
65
66    /// Connection to peer is opening over one or more addresses.
67    Opening {
68        /// Address records used for dialing.
69        records: HashMap<Multiaddr, AddressRecord>,
70
71        /// Connection ID.
72        connection_id: ConnectionId,
73
74        /// Active transports.
75        transports: HashSet<SupportedTransport>,
76    },
77
78    /// Peer is being dialed.
79    Dialing {
80        /// Address record.
81        record: AddressRecord,
82    },
83
84    /// `Litep2p` is not connected to peer.
85    Disconnected {
86        /// Dial address, if it exists.
87        ///
88        /// While the local node was dialing a remote peer, the remote peer might've dialed
89        /// the local node and connection was established successfully. The connection might've
90        /// been closed before the dial concluded which means that
91        /// [`crate::transport::manager::TransportManager`] must be prepared to handle the dial
92        /// failure even after the connection has been closed.
93        dial_record: Option<AddressRecord>,
94    },
95}
96
97/// Peer context.
98#[derive(Debug)]
99pub struct PeerContext {
100    /// Peer state.
101    pub state: PeerState,
102
103    /// Secondary connection, if it's open.
104    pub secondary_connection: Option<AddressRecord>,
105
106    /// Known addresses of peer.
107    pub addresses: AddressStore,
108}