- 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

7
Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[workspace]
members = [
"auth-service",
"database-service",
"packet-service",
]
resolver = "2"

17
packet-service/Cargo.toml Normal file
View File

@@ -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"

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),
}
});
}
}

View File

@@ -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<u8>,
}
impl Packet {
pub fn parse(data: &[u8]) -> Result<Self, Box<dyn Error>> {
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 })
}
}

View File

@@ -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<dyn Error>> {
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(())
}