From d86f5caca14a661a49ccd890819d7c8a20eedd3b Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 15 Sep 2019 13:17:49 -0700 Subject: [PATCH] serverstate::handle now returns a result (patch/login converted) --- src/common/mainloop.rs | 12 ++++++++++-- src/common/serverstate.rs | 3 ++- src/login/login.rs | 7 ++++--- src/patch/main.rs | 7 ++++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/common/mainloop.rs b/src/common/mainloop.rs index 1e6448b..341a66f 100644 --- a/src/common/mainloop.rs +++ b/src/common/mainloop.rs @@ -36,8 +36,16 @@ fn recv_from_clientpool(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); + } } } } diff --git a/src/common/serverstate.rs b/src/common/serverstate.rs index 2ea7742..40980af 100644 --- a/src/common/serverstate.rs +++ b/src/common/serverstate.rs @@ -23,6 +23,7 @@ pub trait ServerState { type PacketError; fn on_connect(&mut self, id: ClientId) -> Vec>; - fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) -> Box>; + fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) + -> Result>, Self::PacketError>; } diff --git a/src/login/login.rs b/src/login/login.rs index c157ad8..d5c98a8 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -133,8 +133,9 @@ impl ServerState for LoginServerState { ] } - fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) -> Box> { - match pkt { + fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket) + -> Result>, LoginError> { + Ok(match pkt { RecvLoginPacket::Login(login) => { Box::new(self.validate_login(login) .into_iter() @@ -142,7 +143,7 @@ impl ServerState for LoginServerState { (id, pkt) })) } - } + }) } } diff --git a/src/patch/main.rs b/src/patch/main.rs index 39b52d8..b15df0b 100644 --- a/src/patch/main.rs +++ b/src/patch/main.rs @@ -180,8 +180,9 @@ impl ServerState for PatchServerState { ] } - fn handle(&mut self, id: ClientId, pkt: &RecvPatchPacket) -> Box> { - match pkt { + fn handle(&mut self, id: ClientId, pkt: &RecvPatchPacket) + -> Result>, 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))) } - } + }) } }