Browse Source

on_connect/on_disconnect return results

pbs
jake 4 years ago
parent
commit
bacf309978
  1. 17
      src/common/mainloop/client.rs
  2. 4
      src/common/serverstate.rs
  3. 2
      src/entity/gateway/postgres/postgres.rs
  4. 10
      src/login/character.rs
  5. 10
      src/login/login.rs
  6. 10
      src/patch/patch.rs
  7. 12
      src/ship/ship.rs

17
src/common/mainloop/client.rs

@ -224,7 +224,10 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
match action { match action {
ClientAction::NewClient(client_id, sender) => { ClientAction::NewClient(client_id, sender) => {
clients.insert(client_id, sender.clone()); clients.insert(client_id, sender.clone());
for action in state.on_connect(client_id).await {
let actions = state.on_connect(client_id).await;
match actions {
Ok(actions) => {
for action in actions {
match action { match action {
OnConnect::Cipher((inc, outc)) => { OnConnect::Cipher((inc, outc)) => {
sender.send(ServerStateAction::Cipher(inc, outc)).await; sender.send(ServerStateAction::Cipher(inc, outc)).await;
@ -234,6 +237,11 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
} }
} }
} }
}
Err(err) => {
warn!("[client {:?} state on_connect error] {:?}", client_id, err);
}
}
}, },
ClientAction::Packet(client_id, pkt) => { ClientAction::Packet(client_id, pkt) => {
let pkts = state.handle(client_id, &pkt).await; let pkts = state.handle(client_id, &pkt).await;
@ -252,6 +260,8 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
}, },
ClientAction::Disconnect(client_id) => { ClientAction::Disconnect(client_id) => {
let pkts = state.on_disconnect(client_id).await; let pkts = state.on_disconnect(client_id).await;
match pkts {
Ok(pkts) => {
for (client_id, pkt) in pkts { for (client_id, pkt) in pkts {
if let Some(client) = clients.get_mut(&client_id) { if let Some(client) = clients.get_mut(&client_id) {
client.send(ServerStateAction::Packet(pkt)).await; client.send(ServerStateAction::Packet(pkt)).await;
@ -262,6 +272,11 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
client.send(ServerStateAction::Disconnect).await; client.send(ServerStateAction::Disconnect).await;
} }
} }
Err(err) => {
warn!("[client {:?} state on_disconnect error] {:?}", client_id, err);
}
}
}
} }
} }
}); });

4
src/common/serverstate.rs

@ -24,9 +24,9 @@ pub trait ServerState {
type RecvPacket: RecvServerPacket; type RecvPacket: RecvServerPacket;
type PacketError; type PacketError;
async fn on_connect(&mut self, id: ClientId) -> Vec<OnConnect<Self::SendPacket>>;
async fn on_connect(&mut self, id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, Self::PacketError>;
async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket)
-> Result<Box<dyn Iterator<Item = (ClientId, Self::SendPacket)> + Send>, Self::PacketError>; -> Result<Box<dyn Iterator<Item = (ClientId, Self::SendPacket)> + Send>, Self::PacketError>;
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, Self::SendPacket)>;
async fn on_disconnect(&mut self, id: ClientId) -> Result<Vec<(ClientId, Self::SendPacket)>, Self::PacketError>;
} }

2
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<UserSettingsEntity, GatewayError> { async fn get_user_settings_by_user(&self, user: &UserAccountEntity) -> Result<UserSettingsEntity, GatewayError> {
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) .bind(user.id.0)
.fetch_one(&self.pool).await?; .fetch_one(&self.pool).await?;
Ok(settings.into()) Ok(settings.into())

10
src/login/character.rs

@ -475,7 +475,7 @@ impl<EG: EntityGateway> ServerState for CharacterServerState<EG> {
type RecvPacket = RecvCharacterPacket; type RecvPacket = RecvCharacterPacket;
type PacketError = CharacterError; type PacketError = CharacterError;
async fn on_connect(&mut self, id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, CharacterError> {
self.clients.insert(id, ClientState::new()); self.clients.insert(id, ClientState::new());
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
@ -485,10 +485,10 @@ impl<EG: EntityGateway> ServerState for CharacterServerState<EG> {
rng.fill(&mut server_key[..]); rng.fill(&mut server_key[..]);
rng.fill(&mut client_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)), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)),
Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key))))
]
])
} }
async fn handle(&mut self, id: ClientId, pkt: &RecvCharacterPacket) async fn handle(&mut self, id: ClientId, pkt: &RecvCharacterPacket)
@ -535,14 +535,14 @@ impl<EG: EntityGateway> ServerState for CharacterServerState<EG> {
}) })
} }
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendCharacterPacket)> {
async fn on_disconnect(&mut self, id: ClientId) -> Result<Vec<(ClientId, SendCharacterPacket)>, CharacterError> {
if let Some(client) = self.clients.remove(&id) { if let Some(client) = self.clients.remove(&id) {
if let Some(mut user) = client.user { if let Some(mut user) = client.user {
user.at_character= false; user.at_character= false;
self.entity_gateway.save_user(&user).await; self.entity_gateway.save_user(&user).await;
} }
} }
Vec::new()
Ok(Vec::new())
} }
} }

10
src/login/login.rs

@ -132,7 +132,7 @@ impl<EG: EntityGateway> ServerState for LoginServerState<EG> {
type RecvPacket = RecvLoginPacket; type RecvPacket = RecvLoginPacket;
type PacketError = LoginError; type PacketError = LoginError;
async fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, LoginError> {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48]; let mut server_key = [0u8; 48];
@ -140,10 +140,10 @@ impl<EG: EntityGateway> ServerState for LoginServerState<EG> {
rng.fill(&mut server_key[..]); rng.fill(&mut server_key[..]);
rng.fill(&mut client_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)), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)),
Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key))))
]
])
} }
async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket)
@ -159,14 +159,14 @@ impl<EG: EntityGateway> ServerState for LoginServerState<EG> {
}) })
} }
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendLoginPacket)> {
async fn on_disconnect(&mut self, id: ClientId) -> Result<Vec<(ClientId, SendLoginPacket)>, LoginError> {
if let Some(username) = self.clients.remove(&id) { if let Some(username) = self.clients.remove(&id) {
if let Ok(mut user) = self.entity_gateway.get_user_by_name(username).await { if let Ok(mut user) = self.entity_gateway.get_user_by_name(username).await {
user.at_login = false; user.at_login = false;
self.entity_gateway.save_user(&user).await; self.entity_gateway.save_user(&user).await;
} }
} }
Vec::new()
Ok(Vec::new())
} }
} }

10
src/patch/patch.rs

@ -160,14 +160,14 @@ impl ServerState for PatchServerState {
type RecvPacket = RecvPatchPacket; type RecvPacket = RecvPatchPacket;
type PacketError = PatchError; type PacketError = PatchError;
async fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, PatchError> {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let key_in: u32 = rng.gen(); let key_in: u32 = rng.gen();
let key_out: 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)))) OnConnect::Cipher((Box::new(PSOPCCipher::new(key_in)), Box::new(PSOPCCipher::new(key_out))))
]
])
} }
async fn handle(&mut self, id: ClientId, pkt: &RecvPatchPacket) 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<Vec<(ClientId, SendPatchPacket)>, PatchError> {
Ok(Vec::new())
} }
} }

12
src/ship/ship.rs

@ -456,7 +456,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
type RecvPacket = RecvShipPacket; type RecvPacket = RecvShipPacket;
type PacketError = ShipError; type PacketError = ShipError;
async fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, ShipError> {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48]; let mut server_key = [0u8; 48];
@ -464,10 +464,10 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
rng.fill(&mut server_key[..]); rng.fill(&mut server_key[..]);
rng.fill(&mut client_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)), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)),
Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key))))
]
])
} }
async fn handle(&mut self, id: ClientId, pkt: &RecvShipPacket) async fn handle(&mut self, id: ClientId, pkt: &RecvShipPacket)
@ -577,7 +577,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
}) })
} }
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendShipPacket)> {
async fn on_disconnect(&mut self, id: ClientId) -> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
// TODO: don't unwrap! // TODO: don't unwrap!
let client = self.clients.get(&id).unwrap(); let client = self.clients.get(&id).unwrap();
let area_client = self.client_location.get_local_client(id).unwrap(); let area_client = self.client_location.get_local_client(id).unwrap();
@ -605,9 +605,9 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
self.entity_gateway.save_user(&client.user).await; self.entity_gateway.save_user(&client.user).await;
} }
neighbors.into_iter().map(|n| {
Ok(neighbors.into_iter().map(|n| {
(n.client, pkt.clone()) (n.client, pkt.clone())
}).collect()
}).collect())
} }
} }

Loading…
Cancel
Save