- add: virtual workspace

- add: start of packet-service
This commit is contained in:
2024-11-26 13:16:20 -05:00
parent 815cb210dc
commit 47899d47cd
5 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
use std::env;
use std::str::FromStr;
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt};
use tracing::{info, error};
use tracing::Level;
mod packet;
mod router;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_max_level(Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())).unwrap_or_else(|_| Level::INFO))
.init();
let listener = TcpListener::bind("127.0.0.1:4000").await?;
info!("Packet service listening on 127.0.0.1:4000");
loop {
let (mut socket, addr) = listener.accept().await?;
info!("New connection from {}", addr);
tokio::spawn(async move {
let mut buffer = vec![0u8; 1024];
match socket.read(&mut buffer).await {
Ok(n) if n > 0 => {
if let Err(e) = router::route_packet(&buffer[..n]).await {
error!("Failed to route packet: {}", e);
}
}
Ok(_) => info!("Connection closed by {}", addr),
Err(e) => error!("Failed to read from socket: {}", e),
}
});
}
}