4 use async_std::net::{TcpStream, ToSocketAddrs};
7 /// Creates a [`Connection`] that uses a TCP transport
14 /// let mut conn = Connection::tcp_client("127.0.0.1:3456").await?;
16 pub async fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(
18 ) -> anyhow::Result<Self> {
19 let stream = TcpStream::connect(&ip_addrs).await?;
20 info!("Established client TCP connection to {}", ip_addrs);
22 stream.set_nodelay(true)?;
23 Ok(Self::from(stream))
27 impl From<TcpStream> for Connection {
28 /// Creates a [`Connection`] using a TCP transport from an async [`TcpStream`].
29 fn from(stream: TcpStream) -> Self {
30 let write_stream = stream.clone();
32 let local_addr = stream
34 .expect("Local address could not be retrieved");
36 let peer_addr = stream
38 .expect("Peer address could not be retrieved");
44 Box::pin(write_stream),