nautilus_network/
mode.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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
16//! Connection mode enumeration for socket clients.
17
18use std::sync::atomic::{AtomicU8, Ordering};
19
20use strum::{AsRefStr, Display, EnumString};
21
22/// Connection mode for a socket client.
23///
24/// The client can be in one of four modes (managed via an atomic flag).
25#[derive(Clone, Copy, Debug, Default, Display, Hash, PartialEq, Eq, AsRefStr, EnumString)]
26#[repr(u8)]
27#[strum(serialize_all = "UPPERCASE")]
28pub enum ConnectionMode {
29    #[default]
30    /// The client is fully connected and operational.
31    /// All tasks (reading, writing, heartbeat) are running normally.
32    Active = 0,
33    /// The client has been disconnected or has been explicitly signaled to reconnect.
34    /// In this state, active tasks are paused until a new connection is established.
35    Reconnect = 1,
36    /// The client has been explicitly signaled to disconnect.
37    /// No further reconnection attempts will be made, and cleanup procedures are initiated.
38    Disconnect = 2,
39    /// The client is permanently closed.
40    /// All associated tasks have been terminated and the connection is no longer available.
41    Closed = 3,
42}
43
44impl ConnectionMode {
45    /// Convert a u8 to [`ConnectionMode`], useful when loading from an `AtomicU8`.
46    ///
47    /// # Panics
48    ///
49    /// Panics if `value` is not a valid `ConnectionMode` discriminant (must be between 0 and 3 inclusive).
50    #[inline]
51    #[must_use]
52    pub fn from_u8(value: u8) -> Self {
53        match value {
54            0 => Self::Active,
55            1 => Self::Reconnect,
56            2 => Self::Disconnect,
57            3 => Self::Closed,
58            _ => panic!("Invalid `ConnectionMode` value: {value}"),
59        }
60    }
61
62    /// Load a [`ConnectionMode`] from an [`AtomicU8`] using sequential consistency ordering.
63    #[inline]
64    #[must_use]
65    pub fn from_atomic(value: &AtomicU8) -> Self {
66        Self::from_u8(value.load(Ordering::SeqCst))
67    }
68
69    /// Convert a [`ConnectionMode`] to a u8, useful when storing to an `AtomicU8`.
70    #[inline]
71    #[must_use]
72    pub const fn as_u8(self) -> u8 {
73        self as u8
74    }
75
76    /// Returns true if the client is in an active state.
77    #[inline]
78    #[must_use]
79    pub const fn is_active(&self) -> bool {
80        matches!(self, Self::Active)
81    }
82
83    /// Returns true if the client is attempting to reconnect.
84    #[inline]
85    #[must_use]
86    pub const fn is_reconnect(&self) -> bool {
87        matches!(self, Self::Reconnect)
88    }
89
90    /// Returns true if the client is attempting to disconnect.
91    #[inline]
92    #[must_use]
93    pub const fn is_disconnect(&self) -> bool {
94        matches!(self, Self::Disconnect)
95    }
96
97    /// Returns true if the client connection is closed.
98    #[inline]
99    #[must_use]
100    pub const fn is_closed(&self) -> bool {
101        matches!(self, Self::Closed)
102    }
103}