mod _impl {
type Error = Box<dyn std::error::Error + Send + Sync>;
use crate::socket::{BtpConfig, BtpSocket};
use std::net::SocketAddr;
use tokio::net::{TcpStream, lookup_host};
#[cfg(feature = "tls")]
use rustls::pki_types::ServerName;
#[cfg(feature = "tls")]
use tokio_rustls::{TlsConnector, client::TlsStream};
impl BtpSocket<TcpStream, SocketAddr> {
pub async fn connect(conf: BtpConfig) -> Result<BtpSocket<TcpStream, SocketAddr>, Error> {
let sock_addr = lookup_host(&conf.addr)
.await?
.next()
.ok_or("Couldn't find the IP address of the host")?;
let sock = TcpStream::connect(&conf.addr).await.unwrap();
Ok(BtpSocket::from(sock, conf, sock_addr))
}
#[cfg(feature = "tls")]
pub async fn connect_tls(
connector: TlsConnector,
conf: BtpConfig,
) -> Result<BtpSocket<TlsStream<TcpStream>, SocketAddr>, Error> {
let sock_addr = lookup_host(&conf.addr)
.await?
.next()
.ok_or("Couldn't find the IP address of the host")?;
let server_name = ServerName::IpAddress(sock_addr.ip().into());
let sock = TcpStream::connect(&conf.addr).await.unwrap();
let tls_connection = connector.connect(server_name, sock).await?;
Ok(BtpSocket::from(tls_connection, conf, sock_addr))
}
}
}