From bacf30997844dec197a28a1e380f0401db630653 Mon Sep 17 00:00:00 2001 From: jake Date: Fri, 30 Oct 2020 21:20:03 -0600 Subject: [PATCH] on_connect/on_disconnect return results --- src/common/mainloop/client.rs | 43 +++++++++++++++++-------- src/common/serverstate.rs | 4 +-- src/entity/gateway/postgres/postgres.rs | 2 +- src/login/character.rs | 10 +++--- src/login/login.rs | 10 +++--- src/patch/patch.rs | 10 +++--- src/ship/ship.rs | 12 +++---- 7 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/common/mainloop/client.rs b/src/common/mainloop/client.rs index d8f3af9..383f0a6 100644 --- a/src/common/mainloop/client.rs +++ b/src/common/mainloop/client.rs @@ -224,15 +224,23 @@ async fn state_client_loop(state: Arc>, match action { ClientAction::NewClient(client_id, sender) => { clients.insert(client_id, sender.clone()); - for action in state.on_connect(client_id).await { - match action { - OnConnect::Cipher((inc, outc)) => { - sender.send(ServerStateAction::Cipher(inc, outc)).await; - }, - OnConnect::Packet(pkt) => { - sender.send(ServerStateAction::Packet(pkt)).await; + let actions = state.on_connect(client_id).await; + match actions { + Ok(actions) => { + for action in actions { + match action { + OnConnect::Cipher((inc, outc)) => { + sender.send(ServerStateAction::Cipher(inc, outc)).await; + }, + OnConnect::Packet(pkt) => { + sender.send(ServerStateAction::Packet(pkt)).await; + } + } } } + Err(err) => { + warn!("[client {:?} state on_connect error] {:?}", client_id, err); + } } }, ClientAction::Packet(client_id, pkt) => { @@ -252,14 +260,21 @@ async fn state_client_loop(state: Arc>, }, ClientAction::Disconnect(client_id) => { let pkts = state.on_disconnect(client_id).await; - for (client_id, pkt) in pkts { - if let Some(client) = clients.get_mut(&client_id) { - client.send(ServerStateAction::Packet(pkt)).await; - } - } + match pkts { + Ok(pkts) => { + for (client_id, pkt) in pkts { + if let Some(client) = clients.get_mut(&client_id) { + client.send(ServerStateAction::Packet(pkt)).await; + } + } - if let Some(client) = clients.get_mut(&client_id) { - client.send(ServerStateAction::Disconnect).await; + if let Some(client) = clients.get_mut(&client_id) { + client.send(ServerStateAction::Disconnect).await; + } + } + Err(err) => { + warn!("[client {:?} state on_disconnect error] {:?}", client_id, err); + } } } } diff --git a/src/common/serverstate.rs b/src/common/serverstate.rs index d22c9c6..ba9caa6 100644 --- a/src/common/serverstate.rs +++ b/src/common/serverstate.rs @@ -24,9 +24,9 @@ pub trait ServerState { type RecvPacket: RecvServerPacket; type PacketError; - async fn on_connect(&mut self, id: ClientId) -> Vec>; + async fn on_connect(&mut self, id: ClientId) -> Result>, Self::PacketError>; async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) -> Result + Send>, Self::PacketError>; - async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, Self::SendPacket)>; + async fn on_disconnect(&mut self, id: ClientId) -> Result, Self::PacketError>; } diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index 5318cc2..266a98b 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -144,7 +144,7 @@ impl EntityGateway for PostgresGateway { } async fn get_user_settings_by_user(&self, user: &UserAccountEntity) -> Result { - let settings = sqlx::query_as::<_, PgUserSettings>("select * from user_settings where id = $1") + let settings = sqlx::query_as::<_, PgUserSettings>("select * from user_settings where user_account = $1") .bind(user.id.0) .fetch_one(&self.pool).await?; Ok(settings.into()) diff --git a/src/login/character.rs b/src/login/character.rs index a855912..5bfc2e1 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -475,7 +475,7 @@ impl ServerState for CharacterServerState { type RecvPacket = RecvCharacterPacket; type PacketError = CharacterError; - async fn on_connect(&mut self, id: ClientId) -> Vec> { + async fn on_connect(&mut self, id: ClientId) -> Result>, CharacterError> { self.clients.insert(id, ClientState::new()); let mut rng = rand::thread_rng(); @@ -485,10 +485,10 @@ impl ServerState for CharacterServerState { rng.fill(&mut server_key[..]); rng.fill(&mut client_key[..]); - vec![OnConnect::Packet(SendCharacterPacket::LoginWelcome(LoginWelcome::new(server_key, client_key))), + Ok(vec![OnConnect::Packet(SendCharacterPacket::LoginWelcome(LoginWelcome::new(server_key, client_key))), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)), Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) - ] + ]) } async fn handle(&mut self, id: ClientId, pkt: &RecvCharacterPacket) @@ -535,14 +535,14 @@ impl ServerState for CharacterServerState { }) } - async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendCharacterPacket)> { + async fn on_disconnect(&mut self, id: ClientId) -> Result, CharacterError> { if let Some(client) = self.clients.remove(&id) { if let Some(mut user) = client.user { user.at_character= false; self.entity_gateway.save_user(&user).await; } } - Vec::new() + Ok(Vec::new()) } } diff --git a/src/login/login.rs b/src/login/login.rs index 9bcb0c7..6066dd2 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -132,7 +132,7 @@ impl ServerState for LoginServerState { type RecvPacket = RecvLoginPacket; type PacketError = LoginError; - async fn on_connect(&mut self, _id: ClientId) -> Vec> { + async fn on_connect(&mut self, _id: ClientId) -> Result>, LoginError> { let mut rng = rand::thread_rng(); let mut server_key = [0u8; 48]; @@ -140,10 +140,10 @@ impl ServerState for LoginServerState { rng.fill(&mut server_key[..]); rng.fill(&mut client_key[..]); - vec![OnConnect::Packet(SendLoginPacket::LoginWelcome(LoginWelcome::new(server_key, client_key))), + Ok(vec![OnConnect::Packet(SendLoginPacket::LoginWelcome(LoginWelcome::new(server_key, client_key))), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)), Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) - ] + ]) } async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) @@ -159,14 +159,14 @@ impl ServerState for LoginServerState { }) } - async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendLoginPacket)> { + async fn on_disconnect(&mut self, id: ClientId) -> Result, LoginError> { if let Some(username) = self.clients.remove(&id) { if let Ok(mut user) = self.entity_gateway.get_user_by_name(username).await { user.at_login = false; self.entity_gateway.save_user(&user).await; } } - Vec::new() + Ok(Vec::new()) } } diff --git a/src/patch/patch.rs b/src/patch/patch.rs index ec88b5c..65cbc3f 100644 --- a/src/patch/patch.rs +++ b/src/patch/patch.rs @@ -160,14 +160,14 @@ impl ServerState for PatchServerState { type RecvPacket = RecvPatchPacket; type PacketError = PatchError; - async fn on_connect(&mut self, _id: ClientId) -> Vec> { + async fn on_connect(&mut self, _id: ClientId) -> Result>, PatchError> { let mut rng = rand::thread_rng(); let key_in: u32 = rng.gen(); let key_out: u32 = rng.gen(); - vec![OnConnect::Packet(SendPatchPacket::PatchWelcome(PatchWelcome::new(key_out, key_in))), + Ok(vec![OnConnect::Packet(SendPatchPacket::PatchWelcome(PatchWelcome::new(key_out, key_in))), OnConnect::Cipher((Box::new(PSOPCCipher::new(key_in)), Box::new(PSOPCCipher::new(key_out)))) - ] + ]) } async fn handle(&mut self, id: ClientId, pkt: &RecvPatchPacket) @@ -202,8 +202,8 @@ impl ServerState for PatchServerState { }) } - async fn on_disconnect(&mut self, _id: ClientId) -> Vec<(ClientId, SendPatchPacket)> { - Vec::new() + async fn on_disconnect(&mut self, _id: ClientId) -> Result, PatchError> { + Ok(Vec::new()) } } diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 1c5830b..7a098fe 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -456,7 +456,7 @@ impl ServerState for ShipServerState { type RecvPacket = RecvShipPacket; type PacketError = ShipError; - async fn on_connect(&mut self, _id: ClientId) -> Vec> { + async fn on_connect(&mut self, _id: ClientId) -> Result>, ShipError> { let mut rng = rand::thread_rng(); let mut server_key = [0u8; 48]; @@ -464,10 +464,10 @@ impl ServerState for ShipServerState { rng.fill(&mut server_key[..]); rng.fill(&mut client_key[..]); - vec![OnConnect::Packet(SendShipPacket::ShipWelcome(ShipWelcome::new(server_key, client_key))), + Ok(vec![OnConnect::Packet(SendShipPacket::ShipWelcome(ShipWelcome::new(server_key, client_key))), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)), Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) - ] + ]) } async fn handle(&mut self, id: ClientId, pkt: &RecvShipPacket) @@ -577,7 +577,7 @@ impl ServerState for ShipServerState { }) } - async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendShipPacket)> { + async fn on_disconnect(&mut self, id: ClientId) -> Result, ShipError> { // TODO: don't unwrap! let client = self.clients.get(&id).unwrap(); let area_client = self.client_location.get_local_client(id).unwrap(); @@ -605,9 +605,9 @@ impl ServerState for ShipServerState { self.entity_gateway.save_user(&client.user).await; } - neighbors.into_iter().map(|n| { + Ok(neighbors.into_iter().map(|n| { (n.client, pkt.clone()) - }).collect() + }).collect()) } }