samod_core/network/
connection_event.rs

1use crate::ConnectionId;
2
3use super::{PeerInfo, connection_info::ConnectionInfo};
4
5/// Events related to connection lifecycle and handshake process.
6///
7/// These events are emitted during connection establishment, handshake
8/// completion, and connection failures. They allow applications to track
9/// the state of network connections and respond to connectivity changes.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum ConnectionEvent {
12    /// Handshake completed successfully with a peer.
13    ///
14    /// This event is emitted when the connection handshake process
15    /// finishes successfully and the connection transitions to the
16    /// established state. After this event, the connection is ready
17    /// for document synchronization.
18    HandshakeCompleted {
19        connection_id: ConnectionId,
20        peer_info: PeerInfo,
21    },
22    /// Connection failed or was disconnected.
23    ///
24    /// This event is emitted when a connection fails or when a connection is
25    /// explicitly disconnected. This can happen due to network errors, protocol
26    /// violations, or explicit disconnection.
27    ConnectionFailed {
28        connection_id: ConnectionId,
29        error: String,
30    },
31
32    /// This event is emitted whenever some part of the connection state changes
33    StateChanged {
34        connection_id: ConnectionId,
35        // The new state
36        new_state: ConnectionInfo,
37    },
38}
39
40impl ConnectionEvent {
41    /// Get the connection ID associated with this event.
42    pub fn connection_id(&self) -> ConnectionId {
43        match self {
44            ConnectionEvent::HandshakeCompleted { connection_id, .. } => *connection_id,
45            ConnectionEvent::ConnectionFailed { connection_id, .. } => *connection_id,
46            ConnectionEvent::StateChanged { connection_id, .. } => *connection_id,
47        }
48    }
49}