]> git.lizzy.rs Git - connect-rs.git/blobdiff - src/tcp/client.rs
make async-oriented, remove block_on
[connect-rs.git] / src / tcp / client.rs
index a0994d4694940788869e30a296e42e41cd220958..221825da27399ad7ab477a13be1a979b7179abcf 100644 (file)
@@ -1,21 +1,21 @@
-use async_std::task;
 use log::*;
 
-use crate::StitchConnection;
+use crate::Connection;
 use async_std::net::{TcpStream, ToSocketAddrs};
 
-impl StitchConnection {
-    pub fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(
+impl Connection {
+    pub async fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(
         ip_addrs: A,
-    ) -> anyhow::Result<StitchConnection> {
-        let read_stream = task::block_on(TcpStream::connect(&ip_addrs))?;
+    ) -> anyhow::Result<Self> {
+        let stream = TcpStream::connect(&ip_addrs).await?;
         info!("Established client TCP connection to {}", ip_addrs);
 
-        Ok(Self::from(read_stream))
+        stream.set_nodelay(true)?;
+        Ok(Self::from(stream))
     }
 }
 
-impl From<TcpStream> for StitchConnection {
+impl From<TcpStream> for Connection {
     fn from(stream: TcpStream) -> Self {
         let write_stream = stream.clone();
 
@@ -30,8 +30,8 @@ impl From<TcpStream> for StitchConnection {
         Self::new(
             local_addr,
             peer_addr,
-            Box::new(stream),
-            Box::new(write_stream),
+            Box::pin(stream),
+            Box::pin(write_stream),
         )
     }
 }