From 5cfe41cd3b7ad14019c1f64faa94c5e86df9fb83 Mon Sep 17 00:00:00 2001 From: Artem Fedorov Date: Mon, 27 Jan 2025 21:37:38 +0100 Subject: [PATCH] Fixing login/logout --- game/src/client_thread/handler.rs | 35 ++++++++++++++++--- game/src/controller.rs | 4 ++- game/src/cp_factory.rs | 13 +++++-- game/src/ls_thread/handler.rs | 2 +- game/src/packets/from_client/auth.rs | 4 +-- game/src/packets/from_client/extended/mod.rs | 6 +++- .../from_client/extended/req_user_ban_info.rs | 25 +++++++++++++ .../from_client/extended/send_client_ini.rs | 25 +++++++++++++ game/src/packets/from_client/logout.rs | 32 +++++++++++++++++ game/src/packets/from_client/mod.rs | 3 +- game/src/packets/handleable/kick_player.rs | 2 +- l2-core/src/shared_packets/common.rs | 1 + l2-core/src/shared_packets/gs_2_ls/auth.rs | 1 + .../src/shared_packets/gs_2_ls/blowfish.rs | 1 + .../shared_packets/gs_2_ls/change_password.rs | 7 +++- .../src/shared_packets/gs_2_ls/gs_status.rs | 3 +- .../gs_2_ls/player_auth_request.rs | 3 +- .../shared_packets/gs_2_ls/player_in_game.rs | 1 + .../shared_packets/gs_2_ls/player_logout.rs | 25 ++++++++++--- .../src/shared_packets/gs_2_ls/reply_chars.rs | 1 + l2-core/src/shared_packets/ls_2_gs/auth_gs.rs | 3 +- l2-core/src/shared_packets/ls_2_gs/init_ls.rs | 1 + .../src/shared_packets/ls_2_gs/kick_player.rs | 3 +- .../ls_2_gs/player_auth_response.rs | 3 +- .../shared_packets/ls_2_gs/request_chars.rs | 3 +- l2-core/src/traits/handlers.rs | 35 +++++++++---------- login/src/client_thread/handler.rs | 2 +- login/src/gs_thread/handler.rs | 4 +-- macro-common/src/lib.rs | 6 ++-- macro-common/src/utils.rs | 2 +- macro-common/tests/sendable_packet.rs | 27 ++++++++++---- 31 files changed, 227 insertions(+), 56 deletions(-) create mode 100644 game/src/packets/from_client/extended/req_user_ban_info.rs create mode 100644 game/src/packets/from_client/extended/send_client_ini.rs create mode 100644 game/src/packets/from_client/logout.rs diff --git a/game/src/client_thread/handler.rs b/game/src/client_thread/handler.rs index 8d65b3f..66657b0 100644 --- a/game/src/client_thread/handler.rs +++ b/game/src/client_thread/handler.rs @@ -1,5 +1,6 @@ use crate::controller::Controller; use crate::cp_factory::build_client_packet; +use crate::ls_thread::LoginHandler; use anyhow::{bail, Error}; use async_trait::async_trait; use entities::dao::char_info::CharacterInfo; @@ -11,13 +12,14 @@ use l2_core::crypt::login::Encryption; use l2_core::dto::InboundConnection; use l2_core::errors::Packet; use l2_core::session::SessionKey; +use l2_core::shared_packets::gs_2_ls::PlayerLogout; use l2_core::traits::handlers::{InboundHandler, PacketHandler, PacketSender}; use l2_core::traits::Shutdown; use std::sync::Arc; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::TcpStream; use tokio::sync::{Mutex, Notify}; -use tracing::{info, instrument}; +use tracing::{error, info, instrument}; #[derive(Debug, Clone, PartialEq)] #[allow(unused)] @@ -45,6 +47,7 @@ pub struct ClientHandler { session_key: Option, pub user: Option, } + impl ClientHandler { pub fn get_protocol(&self) -> Option { self.protocol @@ -82,7 +85,7 @@ impl ClientHandler { pub fn get_account_chars(&self) -> Option<&Vec> { self.account_chars.as_ref() } - + pub fn try_get_account_chars_mut(&mut self) -> anyhow::Result<&mut Vec> { self.account_chars.as_mut().ok_or(anyhow::anyhow!( "Programming error, or possible cheating - missing characters." @@ -160,8 +163,32 @@ impl PacketHandler for ClientHandler { Ok(()) } - fn on_disconnect(&mut self) { - info!("Client disconnected"); + async fn on_disconnect(&mut self) { + info!("Disconnecting Client..."); + let Some(user) = self.user.as_ref() else { + return; + }; + self.controller.logout_account(&user.username); + let packet = match PlayerLogout::new(&user.username) { + Err(e) => { + error!("Cannot build logout packet: {}", e); + //exit function + return; + } + Ok(p) => p, + }; + + if let Err(err) = self + .controller + .message_broker + .notify(LoginHandler::HANDLER_ID, Box::new(packet)) + .await + { + error!( + "Error while sending logout to login server, cause: {:?}", + err + ); + } } fn get_stream_reader_mut(&self) -> &Arc> { diff --git a/game/src/controller.rs b/game/src/controller.rs index 8c733b0..d8029ee 100644 --- a/game/src/controller.rs +++ b/game/src/controller.rs @@ -11,6 +11,7 @@ use l2_core::shared_packets::common::PacketType; use l2_core::traits::IpBan; use std::sync::Arc; use std::time::Duration; +use tracing::info; #[derive(Clone, Debug)] pub struct Controller { @@ -58,8 +59,9 @@ impl Controller { }, ) } - pub fn remove_online_account(&self, account: &str) { + pub fn logout_account(&self, account: &str) { self.online_accounts.remove(account); + info!("Logged out online account: {}", account); } } diff --git a/game/src/cp_factory.rs b/game/src/cp_factory.rs index e50aef8..4b9012f 100644 --- a/game/src/cp_factory.rs +++ b/game/src/cp_factory.rs @@ -1,7 +1,9 @@ use crate::client_thread::ClientHandler; use crate::packets::from_client::auth::AuthLogin; use crate::packets::from_client::char_create::CreateCharRequest; -use crate::packets::from_client::extended::{CheckCharName, GoLobby}; +use crate::packets::from_client::extended::{ + CheckCharName, GoLobby, RequestUserBanInfo, SendClientIni, +}; use crate::packets::from_client::new_char_request::NewCharacterRequest; use crate::packets::from_client::noop::NoOp; use crate::packets::from_client::protocol::ProtocolVersion; @@ -9,6 +11,7 @@ use crate::packets::HandleablePacket; use anyhow::bail; use l2_core::shared_packets::common::ReadablePacket; use tracing::error; +use crate::packets::from_client::logout::Logout; pub fn build_client_packet( data: &[u8], @@ -22,6 +25,7 @@ pub fn build_client_packet( AuthLogin::PACKET_ID => Ok(Box::new(AuthLogin::read(packet_body)?)), NewCharacterRequest::PACKET_ID => Ok(Box::new(NewCharacterRequest::read(packet_body)?)), CreateCharRequest::PACKET_ID => Ok(Box::new(CreateCharRequest::read(packet_body)?)), + Logout::PACKET_ID => Ok(Box::new(Logout::read(packet_body)?)), 0xD0 => build_ex_client_packet(packet_body), _ => { error!("Unknown GS packet ID:0x{:02X}", data[0]); @@ -41,8 +45,13 @@ pub fn build_ex_client_packet( match Some(packet_id) { GoLobby::EX_PACKET_ID => Ok(Box::new(GoLobby::read(packet_body)?)), CheckCharName::EX_PACKET_ID => Ok(Box::new(CheckCharName::read(packet_body)?)), + SendClientIni::EX_PACKET_ID => Ok(Box::new(SendClientIni::read(packet_body)?)), + RequestUserBanInfo::EX_PACKET_ID => Ok(Box::new(RequestUserBanInfo::read(packet_body)?)), _ => { - error!("Unknown extended GS packet ID:0x{:02X}", data[0]); + error!( + "Unknown extended GS packet ID: 0x{:X}", + u16::from_le_bytes([data[0], data[1]]) + ); Ok(Box::new(NoOp::read(data)?)) } } diff --git a/game/src/ls_thread/handler.rs b/game/src/ls_thread/handler.rs index 095621e..2b595ba 100644 --- a/game/src/ls_thread/handler.rs +++ b/game/src/ls_thread/handler.rs @@ -82,7 +82,7 @@ impl PacketHandler for LoginHandler { Ok(()) } - fn on_disconnect(&mut self) { + async fn on_disconnect(&mut self){ self.controller.message_broker.unregister_packet_handler(Self::HANDLER_ID); info!("Login server disconnected"); } diff --git a/game/src/packets/from_client/auth.rs b/game/src/packets/from_client/auth.rs index 2940cec..e08f8c0 100644 --- a/game/src/packets/from_client/auth.rs +++ b/game/src/packets/from_client/auth.rs @@ -112,12 +112,12 @@ impl HandleablePacket for AuthLogin { PlayerLoginResponse::SYSTEM_ERROR_LOGIN_LATER, )?)) .await?; - controller.remove_online_account(&self.login_name); + controller.logout_account(&self.login_name); bail!("Login failed {}", self.login_name); } } } else { - controller.remove_online_account(&self.login_name); + controller.logout_account(&self.login_name); bail!("Account already in game {}", self.login_name); } } diff --git a/game/src/packets/from_client/extended/mod.rs b/game/src/packets/from_client/extended/mod.rs index bd4ee2f..86bf480 100644 --- a/game/src/packets/from_client/extended/mod.rs +++ b/game/src/packets/from_client/extended/mod.rs @@ -1,5 +1,9 @@ mod go_to_lobby; mod check_char_name; +mod send_client_ini; +mod req_user_ban_info; pub use go_to_lobby::*; -pub use check_char_name::*; \ No newline at end of file +pub use check_char_name::*; +pub use send_client_ini::*; +pub use req_user_ban_info::*; \ No newline at end of file diff --git a/game/src/packets/from_client/extended/req_user_ban_info.rs b/game/src/packets/from_client/extended/req_user_ban_info.rs new file mode 100644 index 0000000..0251252 --- /dev/null +++ b/game/src/packets/from_client/extended/req_user_ban_info.rs @@ -0,0 +1,25 @@ +use crate::client_thread::ClientHandler; +use crate::packets::HandleablePacket; +use async_trait::async_trait; +use l2_core::shared_packets::common::ReadablePacket; + +#[derive(Debug, Clone)] +pub struct RequestUserBanInfo; + +impl ReadablePacket for RequestUserBanInfo { + const PACKET_ID: u8 = 0xD0; + const EX_PACKET_ID: Option = Some(0x138); + + fn read(_: &[u8]) -> anyhow::Result { + Ok(Self {}) + } +} + +#[async_trait] +impl HandleablePacket for RequestUserBanInfo { + type HandlerType = ClientHandler; + async fn handle(&self, _: &mut Self::HandlerType) -> anyhow::Result<()> { + //todo: I don't know what this packet is needed for, in L2J it is also not handled + Ok(()) + } +} diff --git a/game/src/packets/from_client/extended/send_client_ini.rs b/game/src/packets/from_client/extended/send_client_ini.rs new file mode 100644 index 0000000..eea8d63 --- /dev/null +++ b/game/src/packets/from_client/extended/send_client_ini.rs @@ -0,0 +1,25 @@ +use crate::client_thread::ClientHandler; +use crate::packets::HandleablePacket; +use async_trait::async_trait; +use l2_core::shared_packets::common::ReadablePacket; + +#[derive(Debug, Clone)] +pub struct SendClientIni; + +impl ReadablePacket for SendClientIni { + const PACKET_ID: u8 = 0xD0; + const EX_PACKET_ID: Option = Some(0x104); + + fn read(_: &[u8]) -> anyhow::Result { + Ok(Self {}) + } +} + +#[async_trait] +impl HandleablePacket for SendClientIni { + type HandlerType = ClientHandler; + async fn handle(&self, _: &mut Self::HandlerType) -> anyhow::Result<()> { + //todo: I don't know what this packet is needed for, in L2J it is also not handled + Ok(()) + } +} diff --git a/game/src/packets/from_client/logout.rs b/game/src/packets/from_client/logout.rs new file mode 100644 index 0000000..5013773 --- /dev/null +++ b/game/src/packets/from_client/logout.rs @@ -0,0 +1,32 @@ +use crate::client_thread::ClientHandler; +use crate::ls_thread::LoginHandler; +use crate::packets::HandleablePacket; +use async_trait::async_trait; +use l2_core::shared_packets::common::ReadablePacket; +use l2_core::shared_packets::gs_2_ls::PlayerLogout; +use l2_core::traits::handlers::PacketHandler; +use l2_core::traits::Shutdown; +use tracing::info; + +#[derive(Debug, Clone)] +pub struct Logout; + +impl ReadablePacket for Logout { + const PACKET_ID: u8 = 0x00; + const EX_PACKET_ID: Option = None; + fn read(_: &[u8]) -> anyhow::Result { + Ok(Self {}) + } +} + +#[async_trait] +impl HandleablePacket for Logout { + type HandlerType = ClientHandler; + async fn handle(&self, handler: &mut Self::HandlerType) -> anyhow::Result<()> { + //todo handle proper logout mechanism: olympiad, + // in battle state, on RB and so on, offline trade, etc... + info!("Player logged out: {:?}", handler.user); + handler.get_shutdown_listener().notify_one(); + Ok(()) + } +} diff --git a/game/src/packets/from_client/mod.rs b/game/src/packets/from_client/mod.rs index 12475fd..daf21ea 100644 --- a/game/src/packets/from_client/mod.rs +++ b/game/src/packets/from_client/mod.rs @@ -3,4 +3,5 @@ pub mod auth; pub mod noop; pub mod new_char_request; pub mod extended; -pub mod char_create; \ No newline at end of file +pub mod char_create; +pub mod logout; \ No newline at end of file diff --git a/game/src/packets/handleable/kick_player.rs b/game/src/packets/handleable/kick_player.rs index 8519046..9365142 100644 --- a/game/src/packets/handleable/kick_player.rs +++ b/game/src/packets/handleable/kick_player.rs @@ -9,7 +9,7 @@ impl HandleablePacket for KickPlayer { type HandlerType = LoginHandler; async fn handle(&self, ph: &mut Self::HandlerType) -> anyhow::Result<()> { let controller = ph.get_controller(); - controller.remove_online_account(&self.account_name); + controller.logout_account(&self.account_name); Ok(()) } } diff --git a/l2-core/src/shared_packets/common.rs b/l2-core/src/shared_packets/common.rs index 5c52c27..c20cc13 100644 --- a/l2-core/src/shared_packets/common.rs +++ b/l2-core/src/shared_packets/common.rs @@ -6,6 +6,7 @@ use macro_common::SendablePacketImpl; use num_enum::TryFromPrimitive; use std::str::FromStr; use std::{fmt::Debug, net::Ipv4Addr}; +use crate as l2_core; pub trait SendablePacket: Debug + Send + Sync { fn get_bytes_mut(&mut self) -> &mut [u8] { diff --git a/l2-core/src/shared_packets/gs_2_ls/auth.rs b/l2-core/src/shared_packets/gs_2_ls/auth.rs index 1cf81aa..5c8ef46 100644 --- a/l2-core/src/shared_packets/gs_2_ls/auth.rs +++ b/l2-core/src/shared_packets/gs_2_ls/auth.rs @@ -4,6 +4,7 @@ use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::write::SendablePacketBuffer; use num_traits::ToBytes; use macro_common::SendablePacketImpl; +use crate as l2_core; #[derive(Clone, Debug, Default, SendablePacketImpl)] pub struct RequestAuthGS { diff --git a/l2-core/src/shared_packets/gs_2_ls/blowfish.rs b/l2-core/src/shared_packets/gs_2_ls/blowfish.rs index 281d8a3..c1a6380 100644 --- a/l2-core/src/shared_packets/gs_2_ls/blowfish.rs +++ b/l2-core/src/shared_packets/gs_2_ls/blowfish.rs @@ -2,6 +2,7 @@ use crate::shared_packets::common::{ReadablePacket, SendablePacket}; use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::write::SendablePacketBuffer; use macro_common::SendablePacketImpl; +use crate as l2_core; #[derive(Clone, Debug, SendablePacketImpl)] pub struct BlowFish { diff --git a/l2-core/src/shared_packets/gs_2_ls/change_password.rs b/l2-core/src/shared_packets/gs_2_ls/change_password.rs index 6bbaf58..89b6834 100644 --- a/l2-core/src/shared_packets/gs_2_ls/change_password.rs +++ b/l2-core/src/shared_packets/gs_2_ls/change_password.rs @@ -1,12 +1,16 @@ +use macro_common::SendablePacketImpl; use crate::shared_packets::common::ReadablePacket; use crate::shared_packets::read::ReadablePacketBuffer; +use crate as l2_core; +use crate::shared_packets::write::SendablePacketBuffer; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, SendablePacketImpl)] pub struct ChangePassword { pub account: String, pub char_name: String, pub current_password: String, pub new_password: String, + buffer: SendablePacketBuffer, } impl ReadablePacket for ChangePassword { @@ -17,6 +21,7 @@ const EX_PACKET_ID: Option = None; let mut buffer = ReadablePacketBuffer::new(data.to_vec()); buffer.read_byte(); Ok(Self { + buffer: SendablePacketBuffer::empty(), account: buffer.read_string(), char_name: buffer.read_string(), current_password: buffer.read_string(), diff --git a/l2-core/src/shared_packets/gs_2_ls/gs_status.rs b/l2-core/src/shared_packets/gs_2_ls/gs_status.rs index 300d075..83a90e1 100644 --- a/l2-core/src/shared_packets/gs_2_ls/gs_status.rs +++ b/l2-core/src/shared_packets/gs_2_ls/gs_status.rs @@ -1,8 +1,9 @@ use macro_common::SendablePacketImpl; use crate::config::gs::GSServer; -use crate::shared_packets::common::{GSStatus, ReadablePacket, SendablePacket}; +use crate::shared_packets::common::{GSStatus, ReadablePacket}; use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::write::SendablePacketBuffer; +use crate as l2_core; #[derive(Clone, Debug, Default, SendablePacketImpl)] pub struct GSStatusUpdate { diff --git a/l2-core/src/shared_packets/gs_2_ls/player_auth_request.rs b/l2-core/src/shared_packets/gs_2_ls/player_auth_request.rs index eba321f..967aade 100644 --- a/l2-core/src/shared_packets/gs_2_ls/player_auth_request.rs +++ b/l2-core/src/shared_packets/gs_2_ls/player_auth_request.rs @@ -1,8 +1,9 @@ use macro_common::SendablePacketImpl; use crate::session::SessionKey; -use crate::shared_packets::common::{ReadablePacket, SendablePacket}; +use crate::shared_packets::common::{ReadablePacket}; use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::write::SendablePacketBuffer; +use crate as l2_core; #[derive(Clone, Debug, SendablePacketImpl)] pub struct PlayerAuthRequest { diff --git a/l2-core/src/shared_packets/gs_2_ls/player_in_game.rs b/l2-core/src/shared_packets/gs_2_ls/player_in_game.rs index 309790c..fc61289 100644 --- a/l2-core/src/shared_packets/gs_2_ls/player_in_game.rs +++ b/l2-core/src/shared_packets/gs_2_ls/player_in_game.rs @@ -2,6 +2,7 @@ use crate::shared_packets::common::{ReadablePacket, SendablePacket}; use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::write::SendablePacketBuffer; use macro_common::SendablePacketImpl; +use crate as l2_core; #[derive(Clone, Debug, SendablePacketImpl)] pub struct PlayerInGame { diff --git a/l2-core/src/shared_packets/gs_2_ls/player_logout.rs b/l2-core/src/shared_packets/gs_2_ls/player_logout.rs index 7e5e0bb..fd5a491 100644 --- a/l2-core/src/shared_packets/gs_2_ls/player_logout.rs +++ b/l2-core/src/shared_packets/gs_2_ls/player_logout.rs @@ -1,19 +1,36 @@ +use crate as l2_core; use crate::shared_packets::common::ReadablePacket; use crate::shared_packets::read::ReadablePacketBuffer; +use crate::shared_packets::write::SendablePacketBuffer; +use macro_common::SendablePacketImpl; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, SendablePacketImpl)] pub struct PlayerLogout { pub acc: String, + pub buffer: SendablePacketBuffer, +} +impl PlayerLogout { + pub fn new(acc: &str) -> anyhow::Result { + let mut inst = Self { + acc: String::new(), + buffer: SendablePacketBuffer::new(), + }; + inst.buffer.write(Self::PACKET_ID)?; + inst.buffer.write_string(Some(acc))?; + Ok(inst) + } } - impl ReadablePacket for PlayerLogout { const PACKET_ID: u8 = 0x03; -const EX_PACKET_ID: Option = None; + const EX_PACKET_ID: Option = None; fn read(data: &[u8]) -> anyhow::Result { let mut buffer = ReadablePacketBuffer::new(data.to_vec()); buffer.read_byte(); let acc = buffer.read_string(); - Ok(Self { acc }) + Ok(Self { + acc, + buffer: SendablePacketBuffer::empty(), + }) } } diff --git a/l2-core/src/shared_packets/gs_2_ls/reply_chars.rs b/l2-core/src/shared_packets/gs_2_ls/reply_chars.rs index 9287bcd..2f08977 100644 --- a/l2-core/src/shared_packets/gs_2_ls/reply_chars.rs +++ b/l2-core/src/shared_packets/gs_2_ls/reply_chars.rs @@ -4,6 +4,7 @@ use crate::shared_packets::write::SendablePacketBuffer; use async_trait::async_trait; use entities::entities::character; use macro_common::SendablePacketImpl; +use crate as l2_core; #[derive(Clone, Debug, SendablePacketImpl)] pub struct ReplyChars { diff --git a/l2-core/src/shared_packets/ls_2_gs/auth_gs.rs b/l2-core/src/shared_packets/ls_2_gs/auth_gs.rs index 0a36d84..da521c4 100644 --- a/l2-core/src/shared_packets/ls_2_gs/auth_gs.rs +++ b/l2-core/src/shared_packets/ls_2_gs/auth_gs.rs @@ -1,9 +1,10 @@ use macro_common::SendablePacketImpl; use crate::shared_packets::{ - common::{ReadablePacket, SendablePacket}, + common::ReadablePacket, read::ReadablePacketBuffer, write::SendablePacketBuffer, }; +use crate as l2_core; #[derive(Debug, Clone, SendablePacketImpl)] pub struct AuthGS { diff --git a/l2-core/src/shared_packets/ls_2_gs/init_ls.rs b/l2-core/src/shared_packets/ls_2_gs/init_ls.rs index 1f89520..83e318b 100644 --- a/l2-core/src/shared_packets/ls_2_gs/init_ls.rs +++ b/l2-core/src/shared_packets/ls_2_gs/init_ls.rs @@ -3,6 +3,7 @@ use crate::shared_packets::common::{LoginServerOpcodes, ReadablePacket, Sendable use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::write::SendablePacketBuffer; use macro_common::SendablePacketImpl; +use crate as l2_core; #[derive(Debug, Clone, SendablePacketImpl)] pub struct InitLS { diff --git a/l2-core/src/shared_packets/ls_2_gs/kick_player.rs b/l2-core/src/shared_packets/ls_2_gs/kick_player.rs index 880dfae..51e4b99 100644 --- a/l2-core/src/shared_packets/ls_2_gs/kick_player.rs +++ b/l2-core/src/shared_packets/ls_2_gs/kick_player.rs @@ -1,9 +1,10 @@ use macro_common::SendablePacketImpl; use crate::shared_packets::read::ReadablePacketBuffer; use crate::shared_packets::{ - common::{ReadablePacket, SendablePacket}, + common::ReadablePacket, write::SendablePacketBuffer, }; +use crate as l2_core; #[derive(Debug, Clone, SendablePacketImpl)] pub struct KickPlayer { diff --git a/l2-core/src/shared_packets/ls_2_gs/player_auth_response.rs b/l2-core/src/shared_packets/ls_2_gs/player_auth_response.rs index 2234d24..4a8a67a 100644 --- a/l2-core/src/shared_packets/ls_2_gs/player_auth_response.rs +++ b/l2-core/src/shared_packets/ls_2_gs/player_auth_response.rs @@ -1,9 +1,10 @@ use crate::shared_packets::{ - common::{ReadablePacket, SendablePacket}, + common::{ReadablePacket}, read::ReadablePacketBuffer, write::SendablePacketBuffer, }; use macro_common::SendablePacketImpl; +use crate as l2_core; #[derive(Debug, Clone, SendablePacketImpl)] pub struct PlayerAuthResponse { diff --git a/l2-core/src/shared_packets/ls_2_gs/request_chars.rs b/l2-core/src/shared_packets/ls_2_gs/request_chars.rs index 6106c54..b734149 100644 --- a/l2-core/src/shared_packets/ls_2_gs/request_chars.rs +++ b/l2-core/src/shared_packets/ls_2_gs/request_chars.rs @@ -1,9 +1,10 @@ use macro_common::SendablePacketImpl; use crate::shared_packets::{ - common::{ReadablePacket, SendablePacket}, + common::ReadablePacket, read::ReadablePacketBuffer, write::SendablePacketBuffer, }; +use crate as l2_core; #[derive(Debug, Clone, SendablePacketImpl)] pub struct RequestChars { diff --git a/l2-core/src/traits/handlers.rs b/l2-core/src/traits/handlers.rs index 0686070..f6f29fb 100644 --- a/l2-core/src/traits/handlers.rs +++ b/l2-core/src/traits/handlers.rs @@ -1,10 +1,12 @@ -use std::fmt::Debug; +use crate::crypt::login::Encryption; use crate::dto; use crate::errors::Packet; use crate::shared_packets::common::SendablePacket; use crate::traits::Shutdown; -use anyhow::{Error}; +use anyhow::Error; use async_trait::async_trait; +use entities::DBPool; +use std::fmt::Debug; use std::sync::Arc; use std::time::Duration; use tokio::io::{AsyncReadExt, AsyncWriteExt}; @@ -13,8 +15,6 @@ use tokio::net::TcpStream; use tokio::sync::Mutex; use tokio::time::sleep; use tracing::{error, info, instrument}; -use entities::DBPool; -use crate::crypt::login::Encryption; pub const PACKET_SIZE_BYTES: usize = 2; @@ -32,10 +32,7 @@ pub trait PacketSender: Send + Sync + Debug { /// /// # Errors /// - when packet is too large - fn add_padding( - &self, - packet: &mut Box, - ) -> Result<(), Error> { + fn add_padding(&self, packet: &mut Box) -> Result<(), Error> { let buffer = packet.get_buffer_mut(); buffer.write_i32(0)?; let padding = (buffer.get_size() - 2) % 8; @@ -85,16 +82,16 @@ pub trait PacketHandler: PacketSender + Shutdown + Send + Sync + Debug { fn new(stream: TcpStream, db_pool: DBPool, lc: Arc) -> Self; async fn on_connect(&mut self) -> Result<(), Packet>; - fn on_disconnect(&mut self); + async fn on_disconnect(&mut self); fn get_stream_reader_mut(&self) -> &Arc>; - + fn get_timeout(&self) -> Option; fn get_db_pool(&self) -> &DBPool; - + async fn on_receive_bytes(&mut self, packet_size: usize, bytes: &mut [u8]) -> Result<(), Error>; - + #[instrument(skip(self))] async fn read_packet(&mut self) -> anyhow::Result<(usize, Vec)> { let mut size_buf = [0; PACKET_SIZE_BYTES]; @@ -109,7 +106,7 @@ pub trait PacketHandler: PacketSender + Shutdown + Send + Sync + Debug { socket.read_exact(&mut body).await?; Ok((size, body)) } - + #[instrument(skip(self))] async fn handle_client(&mut self) { let client_addr = self @@ -124,7 +121,7 @@ pub trait PacketHandler: PacketSender + Shutdown + Send + Sync + Debug { Self::get_handler_name(), e ); - self.on_disconnect(); + self.on_disconnect().await; return; } let shutdown_listener = self.get_shutdown_listener(); //shutdown listener must be cloned only once before the loop @@ -139,7 +136,7 @@ pub trait PacketHandler: PacketSender + Shutdown + Send + Sync + Debug { read_result = read_future =>{ match read_result { Ok((0, _)) => { - self.on_disconnect(); + self.on_disconnect().await; break; } Ok((bytes_read, mut data)) => { @@ -150,13 +147,13 @@ pub trait PacketHandler: PacketSender + Shutdown + Send + Sync + Debug { client_addr, e ); - self.on_disconnect(); + self.on_disconnect().await; break; } } Err(e) => { error!("{}: Failed to read data from client: {}", Self::get_handler_name(), e); - self.on_disconnect(); + self.on_disconnect().await; break; } } @@ -167,13 +164,13 @@ pub trait PacketHandler: PacketSender + Shutdown + Send + Sync + Debug { "{}: No data received within timeout. Dropping connection.", Self::get_handler_name() ); - self.on_disconnect(); + self.on_disconnect().await; break; } // Handle shutdown notification (or other task notifications) () = shutdown_listener.notified() => { info!("{}: Received shutdown notification. Dropping connection.", Self::get_handler_name()); - self.on_disconnect(); + self.on_disconnect().await; break; } } diff --git a/login/src/client_thread/handler.rs b/login/src/client_thread/handler.rs index 02c1f3e..ecea132 100644 --- a/login/src/client_thread/handler.rs +++ b/login/src/client_thread/handler.rs @@ -136,7 +136,7 @@ impl PacketHandler for Client { Ok(()) } - fn on_disconnect(&mut self) { + async fn on_disconnect(&mut self) { info!("Player disconnected: {:?}", self.session_id); } fn get_stream_reader_mut(&self) -> &Arc> { diff --git a/login/src/gs_thread/handler.rs b/login/src/gs_thread/handler.rs index 140a35f..90ec4d3 100644 --- a/login/src/gs_thread/handler.rs +++ b/login/src/gs_thread/handler.rs @@ -115,7 +115,7 @@ impl PacketHandler for GameServer { Ok(()) } - fn on_disconnect(&mut self) { + async fn on_disconnect(&mut self) { info!( "Game server disconnected: ID ({:})", self.server_id.unwrap_or_default() @@ -138,7 +138,7 @@ impl PacketHandler for GameServer { fn get_db_pool(&self) -> &DBPool { &self.db_pool } - + #[instrument(skip(self, bytes))] async fn on_receive_bytes(&mut self, _: usize, bytes: &mut [u8]) -> Result<(), Error> { self.blowfish.decrypt(bytes)?; diff --git a/macro-common/src/lib.rs b/macro-common/src/lib.rs index 750fc93..576e59c 100644 --- a/macro-common/src/lib.rs +++ b/macro-common/src/lib.rs @@ -93,11 +93,11 @@ pub fn derive_sendable(input: TokenStream) -> TokenStream { // Generate the implementation let expanded = quote! { - impl SendablePacket for #name { - fn get_buffer_mut(&mut self) -> &mut SendablePacketBuffer { + impl l2_core::shared_packets::common::SendablePacket for #name { + fn get_buffer_mut(&mut self) -> &mut l2_core::shared_packets::write::SendablePacketBuffer { &mut self.buffer } - fn get_buffer(&self) -> &SendablePacketBuffer { + fn get_buffer(&self) -> &l2_core::shared_packets::write::SendablePacketBuffer { &self.buffer } } diff --git a/macro-common/src/utils.rs b/macro-common/src/utils.rs index ef9cd5d..8a8c76a 100644 --- a/macro-common/src/utils.rs +++ b/macro-common/src/utils.rs @@ -1,7 +1,7 @@ use proc_macro2::{Ident, Span, TokenStream}; use quote::{quote, ToTokens}; use syn::meta::ParseNestedMeta; -use syn::parse::{Parser, Result}; +use syn::parse::Result; use syn::LitStr; pub struct ConfigAttributes { diff --git a/macro-common/tests/sendable_packet.rs b/macro-common/tests/sendable_packet.rs index 57b6369..6fd0b59 100644 --- a/macro-common/tests/sendable_packet.rs +++ b/macro-common/tests/sendable_packet.rs @@ -1,13 +1,28 @@ #[cfg(test)] +pub mod l2_core { + pub mod shared_packets { + pub mod common { + use crate::l2_core::shared_packets::write::SendablePacketBuffer; + + pub trait SendablePacket { + fn get_buffer_mut(&mut self) -> &mut SendablePacketBuffer; + fn get_buffer(&self) -> &SendablePacketBuffer; + } + } + pub mod write { + #[derive(Debug)] + pub struct SendablePacketBuffer; + } + } +} +#[cfg(test)] mod tests { + use super::*; + use crate::l2_core::shared_packets::common::SendablePacket; + use crate::l2_core::shared_packets::write::SendablePacketBuffer; use macro_common::SendablePacketImpl; use std::ptr; - #[derive(Debug)] - struct SendablePacketBuffer; - trait SendablePacket { - fn get_buffer_mut(&mut self) -> &mut SendablePacketBuffer; - fn get_buffer(&self) -> &SendablePacketBuffer; - } + #[derive(SendablePacketImpl, Debug)] struct PacketA { buffer: SendablePacketBuffer,