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