You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
distant/distant-core/src/manager/client/ext/tcp.rs

51 lines
1.6 KiB
Rust

use crate::{DistantManagerClient, DistantManagerClientConfig};
use async_trait::async_trait;
use distant_net::{Codec, FramedTransport, TcpTransport};
use std::{convert, net::SocketAddr};
use tokio::{io, time::Duration};
#[async_trait]
pub trait TcpDistantManagerClientExt {
/// Connect to a remote TCP server using the provided information
async fn connect<C>(
config: DistantManagerClientConfig,
addr: SocketAddr,
codec: C,
) -> io::Result<DistantManagerClient>
where
C: Codec + Send + 'static;
/// Connect to a remote TCP server, timing out after duration has passed
async fn connect_timeout<C>(
config: DistantManagerClientConfig,
addr: SocketAddr,
codec: C,
duration: Duration,
) -> io::Result<DistantManagerClient>
where
C: Codec + Send + 'static,
{
tokio::time::timeout(duration, Self::connect(config, addr, codec))
.await
.map_err(|x| io::Error::new(io::ErrorKind::TimedOut, x))
.and_then(convert::identity)
}
}
#[async_trait]
impl TcpDistantManagerClientExt for DistantManagerClient {
/// Connect to a remote TCP server using the provided information
async fn connect<C>(
config: DistantManagerClientConfig,
addr: SocketAddr,
codec: C,
) -> io::Result<DistantManagerClient>
where
C: Codec + Send + 'static,
{
let transport = TcpTransport::connect(addr).await?;
let transport = FramedTransport::new(transport, codec);
Self::new(config, transport)
}
}