nautilus_network/net.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//! Network abstractions for dependency injection.
17//!
18//! This module provides traits and types that allow our networking components
19//! to work with both real networking (`tokio::net`) and simulated networking (`turmoil::net`)
20//! through dependency injection.
21
22use std::{future::Future, io::Result};
23
24use tokio::io::{AsyncRead, AsyncWrite};
25
26/// Trait for network types that can establish TCP connections.
27pub trait TcpConnector: Send + Sync {
28 type Stream: AsyncRead + AsyncWrite + Send + Unpin + 'static;
29
30 /// Connect to the specified address.
31 fn connect(&self, addr: &str) -> impl Future<Output = Result<Self::Stream>> + Send;
32}
33
34/// Production TCP connector using `tokio::net`.
35#[derive(Default, Clone, Debug)]
36pub struct RealTcpConnector;
37
38impl TcpConnector for RealTcpConnector {
39 type Stream = tokio::net::TcpStream;
40
41 fn connect(&self, addr: &str) -> impl Future<Output = Result<Self::Stream>> + Send {
42 tokio::net::TcpStream::connect(addr.to_string())
43 }
44}