reth_network_types/peers/state.rs
1//! State of connection to a peer.
2
3/// Represents the kind of connection established to the peer, if any
4#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
5pub enum PeerConnectionState {
6 /// Not connected currently.
7 #[default]
8 Idle,
9 /// Disconnect of an incoming connection in progress
10 DisconnectingIn,
11 /// Disconnect of an outgoing connection in progress
12 DisconnectingOut,
13 /// Connected via incoming connection.
14 In,
15 /// Connected via outgoing connection.
16 Out,
17 /// Pending outgoing connection.
18 PendingOut,
19}
20
21// === impl PeerConnectionState ===
22
23impl PeerConnectionState {
24 /// Sets the disconnect state
25 #[inline]
26 pub fn disconnect(&mut self) {
27 match self {
28 Self::In => *self = Self::DisconnectingIn,
29 Self::Out => *self = Self::DisconnectingOut,
30 _ => {}
31 }
32 }
33
34 /// Returns true if this is the idle state.
35 #[inline]
36 pub const fn is_idle(&self) -> bool {
37 matches!(self, Self::Idle)
38 }
39
40 /// Returns true if this is an active incoming connection.
41 #[inline]
42 pub const fn is_incoming(&self) -> bool {
43 matches!(self, Self::In)
44 }
45
46 /// Returns whether we're currently connected with this peer
47 #[inline]
48 pub const fn is_connected(&self) -> bool {
49 matches!(self, Self::In | Self::Out | Self::PendingOut)
50 }
51
52 /// Returns if there's currently no connection to that peer.
53 #[inline]
54 pub const fn is_unconnected(&self) -> bool {
55 matches!(self, Self::Idle)
56 }
57
58 /// Returns true if there's currently an outbound dial to that peer.
59 #[inline]
60 pub const fn is_pending_out(&self) -> bool {
61 matches!(self, Self::PendingOut)
62 }
63}