- add: virtual workspace
- add: start of packet-service
This commit is contained in:
38
packet-service/src/main.rs
Normal file
38
packet-service/src/main.rs
Normal 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),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user