Proper error handling when connecting to the server.

This commit is contained in:
Konstantin Seiler 2020-01-29 09:45:06 +11:00
parent e9c3357e41
commit 65d1c1bf8e
2 changed files with 44 additions and 20 deletions

View file

@ -28,9 +28,27 @@ pub fn connect(
let (addr, connect_url) = match *proxy { let (addr, connect_url) = match *proxy {
Some(ref url) => { Some(ref url) => {
info!("Using proxy \"{}\"", url); info!("Using proxy \"{}\"", url);
(url.to_socket_addrs().unwrap().next().unwrap(), Some(addr)) match url.to_socket_addrs().and_then(|mut iter| {
iter.next().ok_or(io::Error::new(
io::ErrorKind::NotFound,
"Can't resolve proxy server address",
))
}) {
Ok(socket_addr) => (socket_addr, Some(addr)),
Err(error) => return Box::new(futures::future::err(error)),
}
}
None => {
match addr.to_socket_addrs().and_then(|mut iter| {
iter.next().ok_or(io::Error::new(
io::ErrorKind::NotFound,
"Can't resolve server address",
))
}) {
Ok(socket_addr) => (socket_addr, None),
Err(error) => return Box::new(futures::future::err(error)),
}
} }
None => (addr.to_socket_addrs().unwrap().next().unwrap(), None),
}; };
let socket = TcpStream::connect(&addr, handle); let socket = TcpStream::connect(&addr, handle);

View file

@ -460,27 +460,33 @@ impl Future for Main {
progress = true; progress = true;
} }
if let Async::Ready(session) = self.connect.poll().unwrap() { match self.connect.poll() {
self.connect = Box::new(futures::future::empty()); Ok(Async::Ready(session)) => {
let mixer_config = self.mixer_config.clone(); self.connect = Box::new(futures::future::empty());
let mixer = (self.mixer)(Some(mixer_config)); let mixer_config = self.mixer_config.clone();
let player_config = self.player_config.clone(); let mixer = (self.mixer)(Some(mixer_config));
let connect_config = self.connect_config.clone(); let player_config = self.player_config.clone();
let connect_config = self.connect_config.clone();
let audio_filter = mixer.get_audio_filter(); let audio_filter = mixer.get_audio_filter();
let backend = self.backend; let backend = self.backend;
let device = self.device.clone(); let device = self.device.clone();
let (player, event_channel) = let (player, event_channel) =
Player::new(player_config, session.clone(), audio_filter, move || { Player::new(player_config, session.clone(), audio_filter, move || {
(backend)(device) (backend)(device)
}); });
let (spirc, spirc_task) = Spirc::new(connect_config, session, player, mixer); let (spirc, spirc_task) = Spirc::new(connect_config, session, player, mixer);
self.spirc = Some(spirc); self.spirc = Some(spirc);
self.spirc_task = Some(spirc_task); self.spirc_task = Some(spirc_task);
self.player_event_channel = Some(event_channel); self.player_event_channel = Some(event_channel);
progress = true; progress = true;
}
Ok(Async::NotReady) => (),
Err(_) => {
self.connect = Box::new(futures::future::empty());
}
} }
if let Async::Ready(Some(())) = self.signal.poll().unwrap() { if let Async::Ready(Some(())) = self.signal.poll().unwrap() {