mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
fix: dont busy wait when server is not available
This commit is contained in:
@@ -6,7 +6,11 @@ use checkers::network::client::Client;
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let mut client: Client = Client::new().await;
|
||||
let Some(mut client) = Client::new().await else {
|
||||
println!("Not worky, ending");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
client.update();
|
||||
|
||||
println!("=== Write to stding to send text messages ===");
|
||||
|
||||
@@ -24,7 +24,7 @@ pub struct Client {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub async fn new() -> Client {
|
||||
pub async fn new() -> Option<Client> {
|
||||
let settings = ClientSettings {
|
||||
server_url: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6767),
|
||||
name: "Szymon".into(),
|
||||
@@ -38,17 +38,20 @@ impl Client {
|
||||
|
||||
let conn_sender_outgoing = conn.get_sender();
|
||||
|
||||
conn.connect_to_server(settings.server_url).await;
|
||||
if !conn.connect_to_server(settings.server_url).await {
|
||||
error!("Could not connect with the server: {}", settings.server_url);
|
||||
return None;
|
||||
}
|
||||
|
||||
tokio::spawn(async move {
|
||||
conn.start_listening().await;
|
||||
});
|
||||
|
||||
Client {
|
||||
Some(Client {
|
||||
conn_reciever_incoming: Some(conn_reciever),
|
||||
conn_sender_outgoing,
|
||||
settings,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
|
||||
Reference in New Issue
Block a user