More work.

Added chat service
Updated packet service to pass the tcp stream around in a Arc type.
Updated character position data to not require multiplying the coords
Added more debug logs
Added an interceptor for gRPC comms with the chat server
Updated build and push script for the chat server changes
This commit is contained in:
2025-06-06 17:52:29 -04:00
parent 85d41c0239
commit ad6ba2c8e6
32 changed files with 787 additions and 92 deletions

View File

@@ -23,24 +23,30 @@ pub struct PacketRouter {
impl PacketRouter {
pub async fn handle_connection(
&self,
stream: &mut TcpStream,
stream: Arc<Mutex<TcpStream>>,
pool: Arc<BufferPool>,
connection_id: String,
) -> Result<(), Box<dyn Error + Send + Sync>> {
ACTIVE_CONNECTIONS.inc();
while let Some(mut buffer) = pool.acquire().await {
// Read data into the buffer
let mut header_handle = stream.take(6);
let n = header_handle.read(&mut buffer).await?;
if n == 0 {
break; // Connection closed
}
let packet_size = u16::from_le_bytes(buffer[0..2].try_into()?) as usize;
if packet_size > 6 {
let mut body_handle = stream.take((packet_size - 6) as u64);
let n = body_handle.read(&mut buffer[6..]).await?;
if n == 0 {
break; // Connection closed
let packet_size: usize;
{
let mut locked_stream = stream.lock().await;
locked_stream.read_exact(&mut buffer[..6]).await?;
// let mut header_handle = locked_stream.take(6);
// let n = header_handle.read(&mut buffer).await?;
// if n == 0 {
// break; // Connection closed
// }
packet_size = u16::from_le_bytes(buffer[0..2].try_into()?) as usize;
if packet_size > 6 {
locked_stream.read_exact(&mut buffer[6..packet_size]).await?;
// let mut body_handle = locked_stream.take((packet_size - 6) as u64);
// let n = body_handle.read(&mut buffer[6..]).await?;
// if n == 0 {
// break; // Connection closed
// }
}
}
@@ -52,7 +58,7 @@ impl PacketRouter {
Ok(packet) => {
debug!("Parsed Packet: {:?}", packet);
// Handle the parsed packet (route it, process it, etc.)
self.route_packet(stream, packet, connection_id.clone()).await?;
self.route_packet(stream.clone(), packet, connection_id.clone()).await?;
}
Err(e) => warn!("Failed to parse packet: {}", e),
}
@@ -67,7 +73,8 @@ impl PacketRouter {
let mut auth_client = self.auth_client.lock().await;
auth_client.logout(&session_id).await?;
} else {
warn!("No session found for {}", stream.peer_addr()?);
let mut locked_stream = stream.lock().await;
warn!("No session found for {}", locked_stream.peer_addr()?);
}
}
ACTIVE_CONNECTIONS.dec();
@@ -77,7 +84,7 @@ impl PacketRouter {
#[rustfmt::skip]
pub async fn route_packet(
&self,
stream: &mut TcpStream,
stream: Arc<Mutex<TcpStream>>,
packet: Packet,
connection_id: String,
) -> Result<(), Box<dyn Error + Send + Sync>> {
@@ -88,7 +95,7 @@ impl PacketRouter {
PacketType::PakcsAcceptReq => auth::handle_accept_req(stream, packet).await,
PacketType::PakcsJoinServerTokenReq => auth::handle_join_server_req(stream, packet, self.auth_client.clone(), self.connection_service.clone(), connection_id).await,
// Login Packets
PacketType::PakcsLoginTokenReq => auth::handle_login_req(stream, packet, self.auth_client.clone(), self.connection_service.clone(), connection_id, stream.peer_addr()?).await,
PacketType::PakcsLoginTokenReq => auth::handle_login_req(stream, packet, self.auth_client.clone(), self.connection_service.clone(), connection_id).await,
PacketType::PakcsLogoutReq => auth::handle_logout_req(stream, packet, self.auth_client.clone(), self.connection_service.clone(), connection_id).await,
PacketType::PakcsSrvSelectReq => auth::handle_server_select_req(stream, packet, self.connection_service.clone(), connection_id).await,
PacketType::PakcsChannelListReq => auth::handle_channel_list_req(stream, packet).await,
@@ -103,6 +110,8 @@ impl PacketRouter {
PacketType::PakcsChangeMapReq => world::handle_change_map_req(stream, packet, self.character_client.clone(), self.connection_service.clone(), connection_id).await,
PacketType::PakcsMouseCmd => world::handle_mouse_cmd_req(stream, packet, self.connection_service.clone(), connection_id).await,
// Chat Packets
PacketType::PakcsNormalChat => chat::handle_normal_chat(stream, packet, self.connection_service.clone(), connection_id).await,
// 1 => chat::handle_chat(packet).await?,
// 2 => movement::handle_movement(packet).await?,
_ => {