1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// SPDX-FileCopyrightText: Copyright (c) 2017-2024 slowtec GmbH <post@slowtec.de>
// SPDX-License-Identifier: MIT OR Apache-2.0
//! TCP client connections
use std::{fmt, io, net::SocketAddr};
use tokio::{
io::{AsyncRead, AsyncWrite},
net::TcpStream,
};
use super::*;
/// Establish a direct connection to a Modbus TCP coupler.
pub async fn connect(socket_addr: SocketAddr) -> io::Result<Context> {
connect_slave(socket_addr, Slave::tcp_device()).await
}
/// Connect to a physical, broadcast, or custom Modbus device,
/// probably through a Modbus TCP gateway that is forwarding
/// messages to/from the corresponding slave device.
pub async fn connect_slave(socket_addr: SocketAddr, slave: Slave) -> io::Result<Context> {
let transport = TcpStream::connect(socket_addr).await?;
let context = attach_slave(transport, slave);
Ok(context)
}
/// Attach a new client context to a direct transport connection.
///
/// The connection could either be an ordinary [`TcpStream`] or a TLS connection.
pub fn attach<T>(transport: T) -> Context
where
T: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug + 'static,
{
attach_slave(transport, Slave::tcp_device())
}
/// Attach a new client context to a transport connection.
///
/// The connection could either be an ordinary [`TcpStream`] or a TLS connection.
pub fn attach_slave<T>(transport: T, slave: Slave) -> Context
where
T: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug + 'static,
{
let client = crate::service::tcp::Client::new(transport, slave);
Context {
client: Box::new(client),
}
}