- add: virtual workspace
- add: start of packet-service
This commit is contained in:
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"auth-service",
|
||||||
|
"database-service",
|
||||||
|
"packet-service",
|
||||||
|
]
|
||||||
|
resolver = "2"
|
||||||
17
packet-service/Cargo.toml
Normal file
17
packet-service/Cargo.toml
Normal 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"
|
||||||
|
|
||||||
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),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
31
packet-service/src/packet.rs
Normal file
31
packet-service/src/packet.rs
Normal 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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
15
packet-service/src/router.rs
Normal file
15
packet-service/src/router.rs
Normal 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(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user