Browse Source

serverstate::handle now returns a result (patch/login converted)

pbs
jake 5 years ago
parent
commit
d86f5caca1
  1. 12
      src/common/mainloop.rs
  2. 3
      src/common/serverstate.rs
  3. 7
      src/login/login.rs
  4. 7
      src/patch/main.rs

12
src/common/mainloop.rs

@ -36,8 +36,16 @@ fn recv_from_clientpool<STATE, S, R, E>(state: &mut STATE,
},
ClientPoolAction::Packet(client_id, pkt) => {
let to_send = state.handle(client_id, &pkt);
for s in to_send {
pool_send.send(ClientAction::Packet(s.0, s.1)).unwrap();
match to_send {
Ok(pkts) => {
for p in pkts {
pool_send.send(ClientAction::Packet(p.0, p.1)).unwrap();
}
},
Err(err) => {
// TODO: break?
println!("[handler error]: {:?} {:?}", client_id, err);
}
}
}
}

3
src/common/serverstate.rs

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

7
src/login/login.rs

@ -133,8 +133,9 @@ impl<DA: DataAccess> ServerState for LoginServerState<DA> {
]
}
fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) -> Box<dyn Iterator<Item = (ClientId, Self::SendPacket)>> {
match pkt {
fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket)
-> Result<Box<dyn Iterator<Item = (ClientId, Self::SendPacket)>>, LoginError> {
Ok(match pkt {
RecvLoginPacket::Login(login) => {
Box::new(self.validate_login(login)
.into_iter()
@ -142,7 +143,7 @@ impl<DA: DataAccess> ServerState for LoginServerState<DA> {
(id, pkt)
}))
}
}
})
}
}

7
src/patch/main.rs

@ -180,8 +180,9 @@ impl ServerState for PatchServerState {
]
}
fn handle(&mut self, id: ClientId, pkt: &RecvPatchPacket) -> Box<dyn Iterator<Item = (ClientId, SendPatchPacket)>> {
match pkt {
fn handle(&mut self, id: ClientId, pkt: &RecvPatchPacket)
-> Result<Box<dyn Iterator<Item = (ClientId, SendPatchPacket)>>, PatchError> {
Ok(match pkt {
RecvPatchPacket::PatchWelcomeReply(_pkt) => {
Box::new(vec![SendPatchPacket::RequestLogin(RequestLogin {})].into_iter().map(move |pkt| (id, pkt)))
},
@ -208,7 +209,7 @@ impl ServerState for PatchServerState {
];
Box::new(p.into_iter().chain(SendFileIterator::new(&self)).map(move |pkt| (id, pkt)))
}
}
})
}
}

Loading…
Cancel
Save