use crate::handshake;
#[derive(Debug)]
pub enum EstablishConnectionError {
Connection(std::io::Error),
Handshake(handshake::HandshakeError),
}
impl From<std::io::Error> for EstablishConnectionError {
fn from(other: std::io::Error) -> Self {
Self::Connection(other)
}
}
impl From<handshake::HandshakeError> for EstablishConnectionError {
fn from(other: handshake::HandshakeError) -> Self {
Self::Handshake(other)
}
}
pub async fn establish_connection(
adr: &str,
key: &[u8],
port: u16,
) -> Result<tokio::net::TcpStream, EstablishConnectionError> {
let mut connection = tokio::net::TcpStream::connect(&adr).await?;
handshake::client::perform(&mut connection, key, port).await?;
Ok(connection)
}