4 use async_std::net::{TcpStream, ToSocketAddrs};
7 /// Creates a [`Connection`] that uses a TCP transport.
11 /// Please see the [tcp-client](https://github.com/sachanganesh/connect-rs/blob/main/examples/tcp-client/src/main.rs)
12 /// example program for a more thorough showcase.
17 /// let mut conn = Connection::tcp_client("127.0.0.1:3456").await?;
19 pub async fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(
21 ) -> anyhow::Result<Self> {
22 let stream = TcpStream::connect(&ip_addrs).await?;
23 info!("Established client TCP connection to {}", ip_addrs);
25 stream.set_nodelay(true)?;
26 Ok(Self::from(stream))
30 impl From<TcpStream> for Connection {
31 /// Creates a [`Connection`] using a TCP transport from an async [`TcpStream`].
32 fn from(stream: TcpStream) -> Self {
33 let write_stream = stream.clone();
35 let local_addr = stream
37 .expect("Local address could not be retrieved");
39 let peer_addr = stream
41 .expect("Peer address could not be retrieved");
47 Box::pin(write_stream),