use std::net::SocketAddr;
use eyre::Result;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use tokio::time::timeout;
use crate::performance::handshake::{HELLO_TIMEOUT, client_hello_io, server_hello_io};
use crate::report::PeerIdentity;
pub use crate::performance::handshake::ClientHelloResult;
pub async fn client_hello(addr: &str, identity: &PeerIdentity) -> Option<ClientHelloResult> {
let stream = TcpStream::connect(addr).await.ok()?;
match timeout(HELLO_TIMEOUT, client_hello_inner(stream, identity)).await {
Ok(Ok(r)) => Some(r),
_ => None,
}
}
async fn client_hello_inner(
mut stream: TcpStream,
identity: &PeerIdentity,
) -> Result<ClientHelloResult> {
let (mut read_half, mut write_half) = stream.split();
let result = client_hello_io(&mut read_half, &mut write_half, identity).await?;
let _ = stream.shutdown().await;
Ok(result)
}
pub async fn server_hello(stream: &mut TcpStream, peer: SocketAddr) -> Result<()> {
let (mut read_half, mut write_half) = stream.split();
server_hello_io(&mut read_half, &mut write_half, peer).await?;
let _ = stream.shutdown().await;
Ok(())
}