From a768971c6c92f3b024e9614369e92f04d3eaf807 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Wed, 17 Apr 2019 09:22:26 -0600 Subject: [PATCH] Stop singleplayer thread whenever a session is left Former-commit-id: 16fcf35cbc856bebc12944781c2ac1a4daab5550 --- voxygen/src/menu/char_selection/mod.rs | 10 ++++++++-- voxygen/src/session.rs | 10 ++++++++-- voxygen/src/singleplayer.rs | 8 +++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/voxygen/src/menu/char_selection/mod.rs b/voxygen/src/menu/char_selection/mod.rs index 3ba0130622..04504bb6c6 100644 --- a/voxygen/src/menu/char_selection/mod.rs +++ b/voxygen/src/menu/char_selection/mod.rs @@ -45,7 +45,10 @@ impl PlayState for CharSelectionState { // Handle window events for event in global_state.window.fetch_events() { match event { - Event::Close => return PlayStateResult::Shutdown, + Event::Close => { + global_state.singleplayer = None; + return PlayStateResult::Shutdown; + }, // Pass events to ui Event::Ui(event) => { self.char_selection_ui.handle_event(event); @@ -60,7 +63,10 @@ impl PlayState for CharSelectionState { // Maintain the UI for event in self.char_selection_ui.maintain(global_state.window.renderer_mut()) { match event { - ui::Event::Logout => return PlayStateResult::Pop, + ui::Event::Logout => { + global_state.singleplayer = None; + return PlayStateResult::Pop; + }, ui::Event::Play => return PlayStateResult::Push( Box::new(SessionState::new(&mut global_state.window, self.client.clone())) ), diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index fc9ec54126..46003c7278 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -125,7 +125,10 @@ impl PlayState for SessionState { continue; } let _handled = match event { - Event::Close => return PlayStateResult::Shutdown, + Event::Close => { + global_state.singleplayer = None; + return PlayStateResult::Shutdown; + }, // Toggle cursor grabbing Event::KeyDown(Key::ToggleCursor) => { global_state @@ -166,7 +169,10 @@ impl PlayState for SessionState { self.client.borrow_mut().send_chat(msg); }, HudEvent::Logout => return PlayStateResult::Pop, - HudEvent::Quit => return PlayStateResult::Shutdown, + HudEvent::Quit => { + global_state.singleplayer = None; + return PlayStateResult::Shutdown; + }, } } diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index 11cfa45da2..735834e0ee 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -7,7 +7,7 @@ use std::{ thread::JoinHandle }; use std::sync::mpsc::{ - channel, Receiver, Sender + channel, Receiver, Sender, TryRecvError, }; const TPS: u64 = 30; @@ -16,6 +16,8 @@ enum Msg { Stop, } +/// Used to start and stop the background thread running the server +/// when in singleplayer mode. pub struct Singleplayer { server_thread: JoinHandle<()>, sender: Sender, @@ -68,8 +70,8 @@ fn run_server(rec: Receiver) { match rec.try_recv() { Ok(msg) => break, Err(err) => match err { - Empty => (), - Disconnected => break, + TryRecvError::Empty => (), + TryRecvError::Disconnected => break, }, }