Skip to main content

nautilus_network/socket/
types.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16//! Socket types and type aliases.
17
18use std::sync::Arc;
19
20use bytes::Bytes;
21use tokio::io::{ReadHalf, WriteHalf};
22use tokio_tungstenite::MaybeTlsStream;
23
24use crate::net::TcpStream;
25
26/// The write half of a plain or TLS TCP stream.
27pub type TcpWriter = WriteHalf<MaybeTlsStream<TcpStream>>;
28
29/// The read half of a plain or TLS TCP stream.
30pub type TcpReader = ReadHalf<MaybeTlsStream<TcpStream>>;
31
32/// A thread-safe callback for complete suffix-framed messages.
33pub type TcpMessageHandler = Arc<dyn Fn(&[u8]) + Send + Sync>;
34
35/// A command processed by the socket writer task.
36#[derive(Debug)]
37pub enum WriterCommand<W = TcpWriter> {
38    /// Replaces the writer after reconnection and reports whether buffered messages were drained.
39    Update(W, tokio::sync::oneshot::Sender<bool>),
40    /// Replaces the writer, sends reconnect replay first, then drains buffered messages.
41    UpdateWithReplay(W, Vec<Bytes>, tokio::sync::oneshot::Sender<bool>),
42    /// Sends data to the server.
43    Send(Bytes),
44    /// Sends data once, either directly or through reconnect replay.
45    ///
46    /// A newer buffered command with the same key supersedes an older one. If reconnect replay
47    /// already contains the exact data, the buffered command is discarded after replay.
48    SendOrReplay { key: u64, data: Bytes },
49}