rak_rs/connection/state.rs
1/// Connection States
2/// These are all possible states of a raknet session, and while accessible externally
3/// Please note that these are not states relied on within the original implementation of
4/// raknet, which preserve both "Unconnected" and "Connected"
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
6pub enum ConnectionState {
7 /// The Session is not yet connected, but is actively trying to connect.
8 /// Clients in this state are considered to be actively trying to connect.
9 Connecting,
10
11 /// The Session is connected and ready to send and receive packets.
12 /// This is the state after a connection has been established.
13 ///
14 /// This state is applied once the `ConnectionHandshake` has been completed.
15 Connected,
16
17 /// The session is being timed out because it has not sent a packet in a while.
18 /// The interval for this can be set in the Session Options.
19 TimingOut,
20
21 /// The session has been disconnected but is still in the process of cleaning up.
22 /// This is the state after a disconnect has been requested, but the client still wants
23 /// to send packets until its done.
24 Disconnecting,
25
26 /// The session has been disconnected and is ready to be removed.
27 /// This is the state after a disconnect has been requested and the client has
28 /// This is almost never used.
29 Disconnected,
30
31 /// The session is replying to the server but is not actually connected. This is
32 /// the state where ping and pong packets are being sent. Similarly, this is
33 /// the "Unconnected" state, hence "UnconnectedPing"
34 Unidentified,
35
36 /// The session has been identified and is ready to be connected.
37 /// This is the state after a connection has been established.
38 Identified,
39
40 /// The session is not connected and is not trying to connect.
41 /// During this state the session will be dropped. This state occurs when a client
42 /// has completely stopped responding to packets or their socket is destroyed.
43 /// This is not the same as the [`Disconnected`] state.
44 ///
45 /// [`Disconnected`]: crate::connection::state::ConnectionState::Disconnected
46 Offline,
47}
48
49impl ConnectionState {
50 /// Returns whether or not the Session is reliable.
51 /// Reliable sessions are sessions that are not:
52 /// - Offline
53 /// - Disconnected
54 /// - TimingOut
55 pub fn is_reliable(&self) -> bool {
56 match self {
57 Self::Disconnected | Self::TimingOut | Self::Offline => false,
58 _ => true,
59 }
60 }
61
62 /// Returns whether or not the Session is available to recieve
63 /// packets. Sessions in this state are:
64 /// - Connected
65 /// - Connecting
66 /// - Unidentified
67 /// - Disconnecting
68 pub fn is_available(&self) -> bool {
69 match self {
70 Self::Connected | Self::Connecting | Self::Unidentified | Self::Disconnecting => true,
71 _ => false,
72 }
73 }
74
75 /// Returns whether or not the Session is in any "connected" state.
76 /// Sessions in this state are:
77 /// - Connected
78 /// - Connecting
79 pub fn is_connected(&self) -> bool {
80 match self {
81 Self::Connected | Self::Connecting => true,
82 _ => false,
83 }
84 }
85}
86
87impl std::fmt::Display for ConnectionState {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 match self {
90 Self::Connecting => write!(f, "Connecting"),
91 Self::Connected => write!(f, "Connected"),
92 Self::TimingOut => write!(f, "TimingOut"),
93 Self::Disconnecting => write!(f, "Disconnecting"),
94 Self::Disconnected => write!(f, "Disconnected"),
95 Self::Unidentified => write!(f, "Unidentified"),
96 Self::Identified => write!(f, "Identified"),
97 Self::Offline => write!(f, "Offline"),
98 }
99 }
100}