use async_trait::async_trait;
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncWrite};
use crate::error::ConnectError;
#[async_trait]
pub trait Transport: Send + Sync + 'static {
type Stream: AsyncRead + AsyncWrite + Unpin + Send + 'static;
async fn connect(&self, host: &str, port: u16) -> Result<Self::Stream, ConnectError>;
fn name(&self) -> &'static str;
fn default_port(&self) -> u16 {
80
}
}
#[derive(Debug, Clone)]
pub struct TransportConfig {
pub connect_timeout: Duration,
pub disable_nagle: bool,
pub keep_alive: Option<Duration>,
}
impl Default for TransportConfig {
fn default() -> Self {
Self {
connect_timeout: Duration::from_secs(30),
disable_nagle: false,
keep_alive: Some(Duration::from_secs(60)),
}
}
}
impl TransportConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn with_connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = timeout;
self
}
#[must_use]
pub const fn with_nagle(mut self, enable: bool) -> Self {
self.disable_nagle = !enable;
self
}
#[must_use]
pub const fn with_keep_alive(mut self, duration: Option<Duration>) -> Self {
self.keep_alive = duration;
self
}
#[must_use]
pub const fn low_latency() -> Self {
Self {
connect_timeout: Duration::from_secs(10),
disable_nagle: true,
keep_alive: Some(Duration::from_secs(30)),
}
}
}