Browse Source

on_connect/on_disconnect return results

pbs
jake 4 years ago
parent
commit
bacf309978
  1. 43
      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

43
src/common/mainloop/client.rs

@ -224,15 +224,23 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
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, S, R, E>(state: Arc<Mutex<STATE>>,
},
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);
}
}
}
}

4
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<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)
-> 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> {
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())

10
src/login/character.rs

@ -475,7 +475,7 @@ impl<EG: EntityGateway> ServerState for CharacterServerState<EG> {
type RecvPacket = RecvCharacterPacket;
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());
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 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<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(mut user) = client.user {
user.at_character= false;
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 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 server_key = [0u8; 48];
@ -140,10 +140,10 @@ impl<EG: EntityGateway> ServerState for LoginServerState<EG> {
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<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 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())
}
}

10
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<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, 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<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 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 server_key = [0u8; 48];
@ -464,10 +464,10 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
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<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!
let client = self.clients.get(&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;
}
neighbors.into_iter().map(|n| {
Ok(neighbors.into_iter().map(|n| {
(n.client, pkt.clone())
}).collect()
}).collect())
}
}

Loading…
Cancel
Save