2025-07-04 01:56:59 -05:00
|
|
|
|
// client/src/handshake.rs
|
|
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
|
|
|
|
|
|
|
use lesavka_common::lesavka::{self as pb, handshake_client::HandshakeClient};
|
2025-12-01 11:38:51 -03:00
|
|
|
|
use std::time::Duration;
|
2025-12-01 01:21:27 -03:00
|
|
|
|
use tokio::time::timeout;
|
2025-12-01 11:38:51 -03:00
|
|
|
|
use tonic::{Code, transport::Endpoint};
|
2025-12-01 01:21:27 -03:00
|
|
|
|
use tracing::{info, warn};
|
2025-07-04 01:56:59 -05:00
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Clone, Copy, Debug)]
|
|
|
|
|
|
pub struct PeerCaps {
|
2025-12-01 11:38:51 -03:00
|
|
|
|
pub camera: bool,
|
|
|
|
|
|
pub microphone: bool,
|
2025-07-04 01:56:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn negotiate(uri: &str) -> PeerCaps {
|
2025-12-01 01:21:27 -03:00
|
|
|
|
info!(%uri, "🤝 dial handshake");
|
|
|
|
|
|
|
|
|
|
|
|
let ep = Endpoint::from_shared(uri.to_owned())
|
|
|
|
|
|
.expect("handshake endpoint")
|
|
|
|
|
|
.tcp_nodelay(true)
|
|
|
|
|
|
.http2_keep_alive_interval(Duration::from_secs(15))
|
|
|
|
|
|
.connect_timeout(Duration::from_secs(5));
|
|
|
|
|
|
|
|
|
|
|
|
let channel = timeout(Duration::from_secs(8), ep.connect())
|
2025-07-04 01:56:59 -05:00
|
|
|
|
.await
|
2025-12-01 01:21:27 -03:00
|
|
|
|
.expect("handshake connect timeout")
|
|
|
|
|
|
.expect("handshake connect failed");
|
2025-07-04 01:56:59 -05:00
|
|
|
|
|
2025-12-01 01:21:27 -03:00
|
|
|
|
info!("🤝 handshake channel connected");
|
|
|
|
|
|
let mut cli = HandshakeClient::new(channel);
|
|
|
|
|
|
info!("🤝 fetching capabilities…");
|
|
|
|
|
|
|
|
|
|
|
|
match timeout(Duration::from_secs(5), cli.get_capabilities(pb::Empty {})).await {
|
|
|
|
|
|
Ok(Ok(rsp)) => {
|
|
|
|
|
|
let caps = PeerCaps {
|
2025-12-01 11:38:51 -03:00
|
|
|
|
camera: rsp.get_ref().camera,
|
2025-12-01 01:21:27 -03:00
|
|
|
|
microphone: rsp.get_ref().microphone,
|
|
|
|
|
|
};
|
|
|
|
|
|
info!(?caps, "🤝 handshake ok");
|
|
|
|
|
|
caps
|
|
|
|
|
|
}
|
2025-12-01 01:24:12 -03:00
|
|
|
|
Ok(Err(e)) if e.code() == Code::Unimplemented => {
|
2025-12-01 01:21:27 -03:00
|
|
|
|
warn!("🤝 handshake not implemented on server – assuming defaults");
|
2025-07-04 01:56:59 -05:00
|
|
|
|
PeerCaps::default()
|
|
|
|
|
|
}
|
2025-12-01 01:29:41 -03:00
|
|
|
|
Ok(Err(e)) => {
|
|
|
|
|
|
warn!("🤝 handshake failed: {e} – assuming defaults");
|
|
|
|
|
|
PeerCaps::default()
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(_) => {
|
|
|
|
|
|
warn!("🤝 handshake timed out – assuming defaults");
|
|
|
|
|
|
PeerCaps::default()
|
|
|
|
|
|
}
|
2025-07-04 01:56:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|