2025-06-01 21:26:57 -05:00
|
|
|
|
//! navka-server – echoes HID reports back to every client
|
2025-06-01 16:04:00 -05:00
|
|
|
|
#![forbid(unsafe_code)]
|
2025-06-01 13:31:22 -05:00
|
|
|
|
|
2025-06-01 16:04:00 -05:00
|
|
|
|
use anyhow::Result;
|
2025-06-01 21:26:57 -05:00
|
|
|
|
use tokio::{sync::mpsc, task};
|
|
|
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
|
|
|
|
|
use tonic::{Request, Response, Status, transport::Server};
|
|
|
|
|
|
use tonic::async_trait;
|
|
|
|
|
|
|
2025-06-01 16:04:00 -05:00
|
|
|
|
use navka_common::navka::{
|
2025-06-01 21:26:57 -05:00
|
|
|
|
relay_server::{Relay, RelayServer},
|
|
|
|
|
|
HidReport,
|
2025-06-01 16:04:00 -05:00
|
|
|
|
};
|
2025-06-01 13:31:22 -05:00
|
|
|
|
|
2025-06-01 21:26:57 -05:00
|
|
|
|
struct RelaySvc {
|
|
|
|
|
|
// shared broadcast channel (unused for now, but kept for future fan-out)
|
|
|
|
|
|
_hub_tx: mpsc::Sender<HidReport>,
|
2025-06-01 16:04:00 -05:00
|
|
|
|
}
|
2025-06-01 13:31:22 -05:00
|
|
|
|
|
2025-06-01 21:26:57 -05:00
|
|
|
|
#[async_trait]
|
2025-06-01 13:31:22 -05:00
|
|
|
|
impl Relay for RelaySvc {
|
2025-06-01 21:26:57 -05:00
|
|
|
|
type StreamStream = ReceiverStream<Result<HidReport, Status>>;
|
2025-06-01 13:31:22 -05:00
|
|
|
|
|
|
|
|
|
|
async fn stream(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
request: Request<tonic::Streaming<HidReport>>,
|
|
|
|
|
|
) -> Result<Response<Self::StreamStream>, Status> {
|
|
|
|
|
|
let mut inbound = request.into_inner();
|
|
|
|
|
|
|
2025-06-01 21:26:57 -05:00
|
|
|
|
// each connected client gets its own outbound channel
|
|
|
|
|
|
let (tx, rx) = mpsc::channel::<Result<HidReport, Status>>(32);
|
|
|
|
|
|
let outbound = ReceiverStream::new(rx);
|
|
|
|
|
|
|
|
|
|
|
|
// task: read → echo
|
|
|
|
|
|
task::spawn(async move {
|
|
|
|
|
|
while let Some(report) = inbound.message().await? {
|
|
|
|
|
|
// errors here just mean the client hung up – stop the loop
|
|
|
|
|
|
if tx.send(Ok(report)).await.is_err() {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-06-01 13:31:22 -05:00
|
|
|
|
}
|
2025-06-01 21:26:57 -05:00
|
|
|
|
Ok::<(), Status>(())
|
2025-06-01 13:31:22 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-01 21:26:57 -05:00
|
|
|
|
Ok(Response::new(outbound))
|
2025-06-01 13:31:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
2025-06-01 16:04:00 -05:00
|
|
|
|
async fn main() -> Result<()> {
|
2025-06-01 21:26:57 -05:00
|
|
|
|
// placeholder hub – you’ll reuse this when you forward to the OTG gadget
|
|
|
|
|
|
let (hub_tx, _hub_rx) = mpsc::channel::<HidReport>(32);
|
2025-06-01 16:04:00 -05:00
|
|
|
|
|
2025-06-01 21:26:57 -05:00
|
|
|
|
println!("🌀 navka-server listening on 0.0.0.0:50051");
|
2025-06-01 13:31:22 -05:00
|
|
|
|
Server::builder()
|
2025-06-01 21:26:57 -05:00
|
|
|
|
.add_service(RelayServer::new(RelaySvc { _hub_tx: hub_tx }))
|
|
|
|
|
|
.serve("0.0.0.0:50051".parse().unwrap())
|
2025-06-01 13:31:22 -05:00
|
|
|
|
.await?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|