nautilus_network/
mode.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::sync::atomic::{AtomicU8, Ordering};
17
18use strum::{AsRefStr, Display, EnumString};
19
20/// Connection mode for a socket client.
21///
22/// The client can be in one of four modes (managed via an atomic flag).
23#[derive(Clone, Copy, Debug, Default, Display, Hash, PartialEq, Eq, AsRefStr, EnumString)]
24#[repr(u8)]
25#[strum(serialize_all = "UPPERCASE")]
26pub enum ConnectionMode {
27    #[default]
28    /// The client is fully connected and operational.
29    /// All tasks (reading, writing, heartbeat) are running normally.
30    Active = 0,
31    /// The client has been disconnected or has been explicitly signaled to reconnect.
32    /// In this state, active tasks are paused until a new connection is established.
33    Reconnect = 1,
34    /// The client has been explicitly signaled to disconnect.
35    /// No further reconnection attempts will be made, and cleanup procedures are initiated.
36    Disconnect = 2,
37    /// The client is permanently closed.
38    /// All associated tasks have been terminated and the connection is no longer available.
39    Closed = 3,
40}
41
42impl ConnectionMode {
43    /// Convert a u8 to [`ConnectionMode`], useful when loading from an `AtomicU8`.
44    #[inline]
45    pub fn from_u8(value: u8) -> Self {
46        match value {
47            0 => Self::Active,
48            1 => Self::Reconnect,
49            2 => Self::Disconnect,
50            3 => Self::Closed,
51            _ => panic!("Invalid `ConnectionMode` value: {value}"),
52        }
53    }
54
55    #[inline]
56    pub fn from_atomic(value: &AtomicU8) -> Self {
57        Self::from_u8(value.load(Ordering::SeqCst))
58    }
59
60    /// Convert a [`ConnectionMode`] to a u8, useful when storing to an `AtomicU8`.
61    #[inline]
62    pub const fn as_u8(self) -> u8 {
63        self as u8
64    }
65
66    /// Returns true if the client is in an active state.
67    #[inline]
68    pub fn is_active(&self) -> bool {
69        matches!(self, Self::Active)
70    }
71
72    /// Returns true if the client is attempting to reconnect.
73    #[inline]
74    pub fn is_reconnect(&self) -> bool {
75        matches!(self, Self::Reconnect)
76    }
77
78    /// Returns true if the client is attempting to disconnect.
79    #[inline]
80    pub fn is_disconnect(&self) -> bool {
81        matches!(self, Self::Disconnect)
82    }
83
84    /// Returns true if the client connection is closed.
85    #[inline]
86    pub fn is_closed(&self) -> bool {
87        matches!(self, Self::Closed)
88    }
89}