lesavka/client/src/handshake.rs

88 lines
2.8 KiB
Rust
Raw Normal View History

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};
use std::time::Duration;
2025-12-01 01:21:27 -03:00
use tokio::time::timeout;
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
2026-01-28 17:52:00 -03:00
#[derive(Default, Clone, Debug)]
2025-07-04 01:56:59 -05:00
pub struct PeerCaps {
pub camera: bool,
pub microphone: bool,
2026-01-28 17:52:00 -03:00
pub camera_output: Option<String>,
pub camera_codec: Option<String>,
pub camera_width: Option<u32>,
pub camera_height: Option<u32>,
pub camera_fps: Option<u32>,
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)) => {
2026-01-28 17:52:00 -03:00
let rsp = rsp.get_ref();
2025-12-01 01:21:27 -03:00
let caps = PeerCaps {
2026-01-28 17:52:00 -03:00
camera: rsp.camera,
microphone: rsp.microphone,
camera_output: if rsp.camera_output.is_empty() {
None
} else {
Some(rsp.camera_output.clone())
},
camera_codec: if rsp.camera_codec.is_empty() {
None
} else {
Some(rsp.camera_codec.clone())
},
camera_width: if rsp.camera_width == 0 {
None
} else {
Some(rsp.camera_width)
},
camera_height: if rsp.camera_height == 0 {
None
} else {
Some(rsp.camera_height)
},
camera_fps: if rsp.camera_fps == 0 {
None
} else {
Some(rsp.camera_fps)
},
2025-12-01 01:21:27 -03:00
};
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
}
}