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    #[must_use]
46    pub fn from_u8(value: u8) -> Self {
47        match value {
48            0 => Self::Active,
49            1 => Self::Reconnect,
50            2 => Self::Disconnect,
51            3 => Self::Closed,
52            _ => panic!("Invalid `ConnectionMode` value: {value}"),
53        }
54    }
55
56    #[inline]
57    pub fn from_atomic(value: &AtomicU8) -> Self {
58        Self::from_u8(value.load(Ordering::SeqCst))
59    }
60
61    /// Convert a [`ConnectionMode`] to a u8, useful when storing to an `AtomicU8`.
62    #[inline]
63    #[must_use]
64    pub const fn as_u8(self) -> u8 {
65        self as u8
66    }
67
68    /// Returns true if the client is in an active state.
69    #[inline]
70    #[must_use]
71    pub const fn is_active(&self) -> bool {
72        matches!(self, Self::Active)
73    }
74
75    /// Returns true if the client is attempting to reconnect.
76    #[inline]
77    #[must_use]
78    pub const fn is_reconnect(&self) -> bool {
79        matches!(self, Self::Reconnect)
80    }
81
82    /// Returns true if the client is attempting to disconnect.
83    #[inline]
84    #[must_use]
85    pub const fn is_disconnect(&self) -> bool {
86        matches!(self, Self::Disconnect)
87    }
88
89    /// Returns true if the client connection is closed.
90    #[inline]
91    #[must_use]
92    pub const fn is_closed(&self) -> bool {
93        matches!(self, Self::Closed)
94    }
95}