2019-04-14 23:28:29 +00:00
|
|
|
use client::{error::Error as ClientError, Client};
|
|
|
|
use common::comp;
|
2019-05-21 22:31:38 +00:00
|
|
|
use log::info;
|
2019-04-14 23:28:29 +00:00
|
|
|
use std::{
|
2019-05-09 17:58:16 +00:00
|
|
|
net::ToSocketAddrs,
|
2019-04-14 23:28:29 +00:00
|
|
|
sync::mpsc::{channel, Receiver, TryRecvError},
|
2019-06-06 14:48:41 +00:00
|
|
|
thread,
|
2019-05-06 14:26:10 +00:00
|
|
|
time::Duration,
|
2019-04-14 23:28:29 +00:00
|
|
|
};
|
|
|
|
|
2019-06-04 20:41:50 +00:00
|
|
|
#[cfg(feature = "discord")]
|
2019-06-20 06:16:08 +00:00
|
|
|
use crate::{discord, discord::DiscordUpdate};
|
2019-06-04 20:41:50 +00:00
|
|
|
|
2019-04-14 23:28:29 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Error parsing input string or error resolving host name.
|
2019-04-14 23:28:29 +00:00
|
|
|
BadAddress(std::io::Error),
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parsing yielded an empty iterator (specifically to_socket_addrs()).
|
2019-04-14 23:28:29 +00:00
|
|
|
NoAddress,
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parsing/host name resolution successful but could not connect.
|
2019-04-14 23:28:29 +00:00
|
|
|
ConnectionFailed(ClientError),
|
2019-08-07 19:42:44 +00:00
|
|
|
InvalidAuth,
|
2019-05-07 11:09:50 +00:00
|
|
|
ClientCrashed,
|
2019-07-01 11:19:26 +00:00
|
|
|
ServerIsFull,
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Used to asynchronously parse the server address, resolve host names,
|
|
|
|
// and create the client (which involves establishing a connection to the server).
|
2019-04-14 23:28:29 +00:00
|
|
|
pub struct ClientInit {
|
|
|
|
rx: Receiver<Result<Client, Error>>,
|
|
|
|
}
|
|
|
|
impl ClientInit {
|
2019-08-08 03:56:02 +00:00
|
|
|
pub fn new(connection_args: (String, u16, bool), player: comp::Player, password: String, wait: bool) -> Self {
|
2019-04-14 23:28:29 +00:00
|
|
|
let (server_address, default_port, prefer_ipv6) = connection_args;
|
|
|
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
2019-05-17 07:55:51 +00:00
|
|
|
thread::spawn(move || {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Sleep the thread to wait for the single-player server to start up.
|
2019-05-06 14:26:10 +00:00
|
|
|
if wait {
|
2019-05-21 22:31:17 +00:00
|
|
|
info!("Waiting for server to come up...");
|
2019-05-07 11:09:50 +00:00
|
|
|
thread::sleep(Duration::from_millis(500));
|
2019-05-06 14:26:10 +00:00
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parse 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.
|
2019-04-14 23:28:29 +00:00
|
|
|
match server_address
|
|
|
|
.to_socket_addrs()
|
|
|
|
.or((server_address.as_ref(), default_port).to_socket_addrs())
|
|
|
|
{
|
2019-05-17 09:22:32 +00:00
|
|
|
Ok(socket_address) => {
|
2019-04-14 23:28:29 +00:00
|
|
|
let (first_addrs, second_addrs) =
|
2019-05-17 09:22:32 +00:00
|
|
|
socket_address.partition::<Vec<_>, _>(|a| a.is_ipv6() == prefer_ipv6);
|
2019-04-14 23:28:29 +00:00
|
|
|
|
|
|
|
let mut last_err = None;
|
|
|
|
|
|
|
|
for socket_addr in first_addrs.into_iter().chain(second_addrs) {
|
2019-05-19 00:45:02 +00:00
|
|
|
match Client::new(socket_addr, player.view_distance) {
|
2019-04-21 18:12:29 +00:00
|
|
|
Ok(mut client) => {
|
2019-08-08 16:01:15 +00:00
|
|
|
/*match client.register(player, password) {
|
2019-08-08 03:56:02 +00:00
|
|
|
Err(ClientError::InvalidAuth) => {
|
|
|
|
last_err = Some(Error::InvalidAuth);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-08-08 16:01:15 +00:00
|
|
|
println!("Auth success");*/
|
|
|
|
client.register(player, password);
|
2019-04-15 00:23:26 +00:00
|
|
|
let _ = tx.send(Ok(client));
|
2019-06-19 19:25:08 +00:00
|
|
|
|
2019-06-04 20:41:50 +00:00
|
|
|
#[cfg(feature = "discord")]
|
|
|
|
{
|
2019-06-20 06:16:08 +00:00
|
|
|
if !server_address.eq("127.0.0.1") {
|
|
|
|
discord::send_all(vec![
|
|
|
|
DiscordUpdate::Details(server_address),
|
|
|
|
DiscordUpdate::State("Playing...".into()),
|
|
|
|
]);
|
2019-06-04 20:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-19 19:25:08 +00:00
|
|
|
|
2019-04-14 23:28:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
match err {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Assume the connection failed and try next address.
|
2019-04-14 23:28:29 +00:00
|
|
|
ClientError::Network(_) => {
|
|
|
|
last_err = Some(Error::ConnectionFailed(err))
|
|
|
|
}
|
2019-07-01 11:19:26 +00:00
|
|
|
ClientError::TooManyPlayers => {
|
|
|
|
last_err = Some(Error::ServerIsFull);
|
|
|
|
break;
|
|
|
|
}
|
2019-08-07 19:42:44 +00:00
|
|
|
ClientError::InvalidAuth => {
|
|
|
|
last_err = Some(Error::InvalidAuth);
|
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// TODO: Handle errors?
|
2019-04-14 23:28:29 +00:00
|
|
|
_ => panic!(
|
|
|
|
"Unexpected non-network error when creating client: {:?}",
|
|
|
|
err
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parsing/host name resolution successful but no connection succeeded.
|
2019-04-15 00:23:26 +00:00
|
|
|
let _ = tx.send(Err(last_err.unwrap_or(Error::NoAddress)));
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Error parsing input string or error resolving host name.
|
2019-04-15 00:23:26 +00:00
|
|
|
let _ = tx.send(Err(Error::BadAddress(err)));
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 07:55:51 +00:00
|
|
|
});
|
2019-04-14 23:28:29 +00:00
|
|
|
|
|
|
|
ClientInit { rx }
|
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Poll if the thread is complete.
|
|
|
|
/// Returns None if the thread is still running, otherwise returns the Result of client creation.
|
2019-04-14 23:28:29 +00:00
|
|
|
pub fn poll(&self) -> Option<Result<Client, Error>> {
|
|
|
|
match self.rx.try_recv() {
|
|
|
|
Ok(result) => Some(result),
|
|
|
|
Err(TryRecvError::Empty) => None,
|
2019-05-07 11:09:50 +00:00
|
|
|
Err(TryRecvError::Disconnected) => Some(Err(Error::ClientCrashed)),
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|