diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7bdb19e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = [ + "auth-service", + "database-service", + "packet-service", +] +resolver = "2" \ No newline at end of file diff --git a/packet-service/Cargo.toml b/packet-service/Cargo.toml new file mode 100644 index 0000000..19c595c --- /dev/null +++ b/packet-service/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "packet-service" +version = "0.1.0" +edition = "2021" + +[features] +mocks = [] +consul = [] + +[dependencies] +dotenv = "0.15" +tokio = { version = "1.41.1", features = ["full"] } +serde = { version = "1.0", features = ["derive"] } +bytes = { version = "1.8.0", features = ["std", "serde"] } +tracing = "0.1" +tracing-subscriber = "0.3.18" + diff --git a/packet-service/src/main.rs b/packet-service/src/main.rs new file mode 100644 index 0000000..3e2be88 --- /dev/null +++ b/packet-service/src/main.rs @@ -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> { + 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), + } + }); + } +} diff --git a/packet-service/src/packet.rs b/packet-service/src/packet.rs new file mode 100644 index 0000000..59ffe7e --- /dev/null +++ b/packet-service/src/packet.rs @@ -0,0 +1,31 @@ +use serde::{Serialize, Deserialize}; +use std::error::Error; +use tracing::debug; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Packet { + pub packet_size: u16, + pub packet_type: u16, + pub packet_crc: u16, + pub payload: Vec, +} + +impl Packet { + pub fn parse(data: &[u8]) -> Result> { + if data.len() < 6 { + return Err("Invalid packet: Too short".into()); + } + + let packet_size = u16::from_be_bytes(data[0..2].try_into()?); + let packet_type = u16::from_be_bytes(data[2..4].try_into()?); + let packet_crc = u16::from_be_bytes(data[4..6].try_into()?); + let payload = data[6..].to_vec(); + + debug!("Packet Size: {}", packet_size); + debug!("Packet Type: {}", packet_type); + debug!("Packet CRC: {}", packet_crc); + debug!("Payload: {:?}", String::from_utf8(payload.clone())?); + + Ok(Packet { packet_size, packet_type, packet_crc, payload }) + } +} diff --git a/packet-service/src/router.rs b/packet-service/src/router.rs new file mode 100644 index 0000000..c2c138d --- /dev/null +++ b/packet-service/src/router.rs @@ -0,0 +1,15 @@ +use crate::packet::Packet; +// use crate::handlers::{chat, movement}; +use std::error::Error; + +pub async fn route_packet(data: &[u8]) -> Result<(), Box> { + let packet = Packet::parse(data)?; + + match packet.packet_type { + // 1 => chat::handle_chat(packet).await?, + // 2 => movement::handle_movement(packet).await?, + _ => return Err("Unknown packet type".into()), + } + + Ok(()) +}