Browse Source

change serverstate on_connect and on_disconnect to async

pbs
jake 4 years ago
parent
commit
05596cec4f
  1. 4
      src/common/mainloop/client.rs
  2. 4
      src/common/serverstate.rs
  3. 4
      src/login/login.rs
  4. 4
      src/patch/patch.rs
  5. 4
      src/ship/ship.rs

4
src/common/mainloop/client.rs

@ -224,7 +224,7 @@ 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) {
for action in state.on_connect(client_id).await {
match action {
OnConnect::Cipher((inc, outc)) => {
sender.send(ServerStateAction::Cipher(inc, outc)).await;
@ -251,7 +251,7 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
}
},
ClientAction::Disconnect(client_id) => {
let pkts = state.on_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;

4
src/common/serverstate.rs

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

4
src/login/login.rs

@ -113,7 +113,7 @@ impl<EG: EntityGateway> ServerState for LoginServerState<EG> {
type RecvPacket = RecvLoginPacket;
type PacketError = LoginError;
fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48];
@ -140,7 +140,7 @@ impl<EG: EntityGateway> ServerState for LoginServerState<EG> {
})
}
fn on_disconnect(&mut self, _id: ClientId) -> Vec<(ClientId, SendLoginPacket)> {
async fn on_disconnect(&mut self, _id: ClientId) -> Vec<(ClientId, SendLoginPacket)> {
Vec::new()
}
}

4
src/patch/patch.rs

@ -160,7 +160,7 @@ impl ServerState for PatchServerState {
type RecvPacket = RecvPatchPacket;
type PacketError = PatchError;
fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
let mut rng = rand::thread_rng();
let key_in: u32 = rng.gen();
let key_out: u32 = rng.gen();
@ -202,7 +202,7 @@ impl ServerState for PatchServerState {
})
}
fn on_disconnect(&mut self, _id: ClientId) -> Vec<(ClientId, SendPatchPacket)> {
async fn on_disconnect(&mut self, _id: ClientId) -> Vec<(ClientId, SendPatchPacket)> {
Vec::new()
}
}

4
src/ship/ship.rs

@ -456,7 +456,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
type RecvPacket = RecvShipPacket;
type PacketError = ShipError;
fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
async fn on_connect(&mut self, _id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48];
@ -577,7 +577,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
})
}
fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendShipPacket)> {
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendShipPacket)> {
// TODO: don't unwrap!
let client = self.clients.get(&id).unwrap();
let area_client = self.client_location.get_local_client(id).unwrap();

Loading…
Cancel
Save