Files
roowho2/src/bin/roowhod.rs
h7x4 81cabd0723
Some checks failed
Build and test / build (push) Successful in 1m5s
Build and test / check (push) Failing after 1m23s
Build and test / test (push) Failing after 1m24s
Build and test / docs (push) Failing after 1m41s
proto/rwhod: improve de/serialization and datatypes
2026-01-04 23:44:55 +09:00

42 lines
1.4 KiB
Rust

use std::net::SocketAddrV4;
use anyhow::Context;
use chrono::Timelike;
use roowho2_lib::proto::{Whod, WhodStatusUpdate};
const RWHOD_BROADCAST_PORT: u16 = 513;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let addr = SocketAddrV4::new(std::net::Ipv4Addr::UNSPECIFIED, RWHOD_BROADCAST_PORT);
let socket = tokio::net::UdpSocket::bind(addr).await?;
socket.set_broadcast(true)?;
let mut buf = [0u8; Whod::MAX_SIZE];
loop {
let (len, src) = socket.recv_from(&mut buf).await?;
if len < Whod::HEADER_SIZE {
eprintln!(
"Received too short packet from {src}: {len} bytes (needs to be at least {} bytes)",
Whod::HEADER_SIZE
);
continue;
}
let result: WhodStatusUpdate = Whod::from_bytes(&buf[..len])
.context("Failed to parse whod packet")?
.try_into()
.map(|mut status_update: WhodStatusUpdate| {
let timestamp = chrono::Utc::now()
.with_nanosecond(0)
.unwrap_or(chrono::Utc::now());
status_update.recvtime = Some(timestamp);
status_update
})
.map_err(|e| anyhow::anyhow!("Invalid whod packet: {}", e))?;
println!("Received whod packet from {src}:\n{result:#?}");
buf = [0u8; Whod::MAX_SIZE];
}
}