]> git.lizzy.rs Git - connect-rs.git/blob - src/tcp/client.rs
rename stitch-net to connect
[connect-rs.git] / src / tcp / client.rs
1 use async_std::task;
2 use log::*;
3
4 use crate::StitchConnection;
5 use async_std::net::{TcpStream, ToSocketAddrs};
6
7 impl StitchConnection {
8     pub fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(
9         ip_addrs: A,
10     ) -> anyhow::Result<StitchConnection> {
11         let read_stream = task::block_on(TcpStream::connect(&ip_addrs))?;
12         info!("Established client TCP connection to {}", ip_addrs);
13
14         Ok(Self::from(read_stream))
15     }
16 }
17
18 impl From<TcpStream> for StitchConnection {
19     fn from(stream: TcpStream) -> Self {
20         let write_stream = stream.clone();
21
22         let local_addr = stream
23             .local_addr()
24             .expect("Local address could not be retrieved");
25
26         let peer_addr = stream
27             .peer_addr()
28             .expect("Peer address could not be retrieved");
29
30         Self::new(
31             local_addr,
32             peer_addr,
33             Box::new(stream),
34             Box::new(write_stream),
35         )
36     }
37 }