(tools, client, server) feat: Remove gRPC support, add TCP back and reorganized project

This commit is contained in:
2025-08-30 17:07:03 +08:00
parent 8fd5e24865
commit 362aa799b9
28 changed files with 378 additions and 490 deletions

View File

@ -0,0 +1,68 @@
use std::io;
use std::sync::LazyLock;
use tokio::net::UdpSocket;
use tokio::sync::Mutex;
use super::SERVER_ADDR;
pub(crate) static UDP_SERVER: LazyLock<Mutex<UdpServer>> =
LazyLock::new(|| Mutex::new(UdpServer::new()));
pub(crate) struct UdpServer {
is_running: bool,
}
impl UdpServer {
fn new() -> Self {
Self { is_running: false }
}
pub(crate) async fn start(&mut self) {
if self.is_running {
log::warn!("UDP server is already running");
return;
}
match UdpSocket::bind(SERVER_ADDR).await {
Ok(socket) => {
self.is_running = true;
tokio::spawn(async move {
Self::handle_client(&socket)
.await
.unwrap_or_else(|e| log::error!("Failed to process data: {e}"))
});
log::info!("UDP Server started on {}", SERVER_ADDR);
}
Err(e) => log::error!("Failed to bind to address: {e}"),
}
}
pub(crate) async fn stop(&mut self) {
if !self.is_running {
return;
}
self.is_running = false;
}
async fn handle_client(socket: &UdpSocket) -> io::Result<()> {
loop {
let mut buffer = [0; 1024];
let (len, addr) = socket.recv_from(&mut buffer).await?;
if len == 0 {
break;
}
log::info!("Received message from client {addr}");
// TODO: Deserialize data
let buffer = &buffer[..len];
socket.send_to(buffer, addr).await?;
}
Ok(())
}
}