use std::{fmt, io, net::SocketAddr};
use tokio::{
io::{AsyncRead, AsyncWrite},
net::TcpStream,
};
use super::*;
pub async fn connect(socket_addr: SocketAddr) -> io::Result<Context> {
connect_slave(socket_addr, Slave::tcp_device()).await
}
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)
}
pub fn attach<T>(transport: T) -> Context
where
T: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug + 'static,
{
attach_slave(transport, Slave::tcp_device())
}
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),
}
}