Improved singleplayer error handling

Former-commit-id: 0e33493bbb588ab0cd5278ec92b5859712446f4b
This commit is contained in:
Joshua Barretto
2019-05-07 12:09:50 +01:00
parent e5db851f30
commit df9b64355a
3 changed files with 10 additions and 4 deletions

View File

@ -15,6 +15,7 @@ pub enum Error {
NoAddress, NoAddress,
// Parsing/host name resolution successful but could not connect // Parsing/host name resolution successful but could not connect
ConnectionFailed(ClientError), ConnectionFailed(ClientError),
ClientCrashed,
} }
// Used to asynchronusly parse the server address, resolve host names, and create the client (which involves establishing a connection to the server) // Used to asynchronusly parse the server address, resolve host names, and create the client (which involves establishing a connection to the server)
@ -31,7 +32,7 @@ impl ClientInit {
let handle = Some(thread::spawn(move || { let handle = Some(thread::spawn(move || {
// Sleep the thread to wait for the single-player server to start up // Sleep the thread to wait for the single-player server to start up
if wait { if wait {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(500));
} }
// Parses ip address or resolves hostname // Parses ip address or resolves hostname
// Note: if you use an ipv6 address the number after the last colon will be used as the port unless you use [] around the address // Note: if you use an ipv6 address the number after the last colon will be used as the port unless you use [] around the address
@ -85,7 +86,7 @@ impl ClientInit {
match self.rx.try_recv() { match self.rx.try_recv() {
Ok(result) => Some(result), Ok(result) => Some(result),
Err(TryRecvError::Empty) => None, Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => panic!("Thread panicked or already finished"), Err(TryRecvError::Disconnected) => Some(Err(Error::ClientCrashed)),
} }
} }
} }

View File

@ -78,6 +78,7 @@ impl PlayState for MainMenuState {
match err { match err {
InitError::BadAddress(_) | InitError::NoAddress => "Server not found", InitError::BadAddress(_) | InitError::NoAddress => "Server not found",
InitError::ConnectionFailed(_) => "Connection failed", InitError::ConnectionFailed(_) => "Connection failed",
InitError::ClientCrashed => "Client crashed",
} }
.to_string(), .to_string(),
); );

View File

@ -1,4 +1,5 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use log::warn;
use common::comp; use common::comp;
use crate::{ use crate::{
menu::char_selection::CharSelectionState, singleplayer::Singleplayer, Direction, GlobalState, menu::char_selection::CharSelectionState, singleplayer::Singleplayer, Direction, GlobalState,
@ -40,8 +41,11 @@ impl PlayState for StartSingleplayerState {
let client = loop { let client = loop {
match client_init.poll() { match client_init.poll() {
Some(Ok(client)) => break client, Some(Ok(client)) => break client,
// Should always work // An error occured!
Some(Err(err)) => {}, Some(Err(err)) => {
warn!("Failed to start singleplayer server: {:?}", err);
return PlayStateResult::Pop;
},
_ => {} _ => {}
} }
}; };