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

This commit is contained in:
jake 2019-09-15 13:17:49 -07:00
parent 929f0e7f85
commit d86f5caca1
4 changed files with 20 additions and 9 deletions

View File

@ -36,8 +36,16 @@ fn recv_from_clientpool<STATE, S, R, E>(state: &mut STATE,
}, },
ClientPoolAction::Packet(client_id, pkt) => { ClientPoolAction::Packet(client_id, pkt) => {
let to_send = state.handle(client_id, &pkt); let to_send = state.handle(client_id, &pkt);
for s in to_send { match to_send {
pool_send.send(ClientAction::Packet(s.0, s.1)).unwrap(); 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);
}
} }
} }
} }

View File

@ -23,6 +23,7 @@ pub trait ServerState {
type PacketError; type PacketError;
fn on_connect(&mut self, id: ClientId) -> Vec<OnConnect<Self::SendPacket>>; 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>;
} }

View File

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

View File

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