1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// Peer connection state machine.
///
/// Tracks the lifecycle of a peer connection from initial contact through
/// active communication to graceful shutdown.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PeerState {
/// Peer has been created but no packets exchanged yet
#[default]
Idle,
/// Client: Sent CONNECT, waiting for VERIFY_CONNECT
Connecting,
/// Server: Received CONNECT, sent VERIFY_CONNECT, waiting for ACK
AcknowledgingConnect,
/// Client: Received VERIFY_CONNECT, sent ACK, waiting for confirmation
ConnectionSucceeded,
/// Both sides have completed handshake - connection is active
Connected,
/// Disconnect command sent, waiting for acknowledgment
Disconnecting,
/// Peer is being cleaned up (zombie state before removal)
Zombie,
}
impl PeerState {
/// Returns true if the peer is in an active state where data can be sent
pub fn is_active(&self) -> bool {
matches!(
self,
PeerState::Connected
| PeerState::ConnectionSucceeded
| PeerState::AcknowledgingConnect
| PeerState::Connecting
)
}
/// Returns true if the connection is fully established
pub fn is_established(&self) -> bool {
matches!(self, PeerState::Connected | PeerState::ConnectionSucceeded)
}
/// Returns true if the peer is disconnecting or already disconnected
pub fn is_disconnecting(&self) -> bool {
matches!(self, PeerState::Disconnecting | PeerState::Zombie)
}
/// Returns true if the peer is in the middle of connection handshake
pub fn is_connecting(&self) -> bool {
matches!(
self,
PeerState::Connecting
| PeerState::AcknowledgingConnect
| PeerState::ConnectionSucceeded
)
}
}