use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpStream;
use crate::client::{ClientConnection, Signer};
use crate::error::*;
use crate::proto::iocompat::AsyncIoTokioAsStd;
use crate::proto::tcp::{TcpClientConnect, TcpClientStream};
use crate::proto::xfer::{DnsMultiplexer, DnsMultiplexerConnect};
#[derive(Clone, Copy)]
pub struct TcpClientConnection {
name_server: SocketAddr,
bind_addr: Option<SocketAddr>,
timeout: Duration,
}
impl TcpClientConnection {
pub fn new(name_server: SocketAddr) -> ClientResult<Self> {
Self::with_timeout(name_server, Duration::from_secs(5))
}
pub fn with_timeout(name_server: SocketAddr, timeout: Duration) -> ClientResult<Self> {
Self::with_bind_addr_and_timeout(name_server, None, timeout)
}
pub fn with_bind_addr_and_timeout(
name_server: SocketAddr,
bind_addr: Option<SocketAddr>,
timeout: Duration,
) -> ClientResult<Self> {
Ok(Self {
name_server,
bind_addr,
timeout,
})
}
}
impl ClientConnection for TcpClientConnection {
type Sender = DnsMultiplexer<TcpClientStream<AsyncIoTokioAsStd<TcpStream>>, Signer>;
type SenderFuture = DnsMultiplexerConnect<
TcpClientConnect<AsyncIoTokioAsStd<TcpStream>>,
TcpClientStream<AsyncIoTokioAsStd<TcpStream>>,
Signer,
>;
fn new_stream(&self, signer: Option<Arc<Signer>>) -> Self::SenderFuture {
let (tcp_client_stream, handle) =
TcpClientStream::<AsyncIoTokioAsStd<TcpStream>>::with_bind_addr_and_timeout(
self.name_server,
self.bind_addr,
self.timeout,
);
DnsMultiplexer::new(tcp_client_stream, handle, signer)
}
}