[improve] set game dir on client -> updates Play/Install button based on games existing
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
#![allow(clippy::missing_errors_doc)]
|
||||
|
||||
use std::{net::SocketAddr, time::Duration};
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use lanspread_db::db::Game;
|
||||
use lanspread_proto::{Message as _, Request, Response};
|
||||
use lanspread_utils::maybe_addr;
|
||||
use s2n_quic::{client::Connect, provider::limits::Limits, Client as QuicClient};
|
||||
use s2n_quic::{client::Connect, provider::limits::Limits, Client as QuicClient, Connection};
|
||||
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
|
||||
|
||||
static CERT_PEM: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../cert.pem"));
|
||||
@ -21,7 +23,39 @@ pub enum ClientCommand {
|
||||
ServerAddr(SocketAddr),
|
||||
}
|
||||
|
||||
/// # Errors
|
||||
async fn initial_server_alive_check(conn: &mut Connection) -> bool {
|
||||
let stream = match conn.open_bidirectional_stream().await {
|
||||
Ok(stream) => stream,
|
||||
Err(e) => {
|
||||
log::error!("failed to open stream: {e}");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
let (mut rx, mut tx) = stream.split();
|
||||
|
||||
// send ping
|
||||
if let Err(e) = tx.send(Request::Ping.encode()).await {
|
||||
log::error!("failed to send ping to server: {e}");
|
||||
return false;
|
||||
}
|
||||
let _ = tx.close().await;
|
||||
|
||||
// receive pong
|
||||
if let Ok(Some(response)) = rx.receive().await {
|
||||
let response = Response::decode(response);
|
||||
if let Response::Pong = response {
|
||||
log::info!("server is alive");
|
||||
return true;
|
||||
}
|
||||
|
||||
log::error!("server sent invalid response to ping: {response:?}");
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub async fn run(
|
||||
mut rx_control: UnboundedReceiver<ClientCommand>,
|
||||
tx_notify_ui: UnboundedSender<ClientEvent>,
|
||||
@ -58,6 +92,10 @@ pub async fn run(
|
||||
|
||||
conn.keep_alive(true)?;
|
||||
|
||||
if !initial_server_alive_check(&mut conn).await {
|
||||
continue;
|
||||
}
|
||||
|
||||
log::info!(
|
||||
"connected: (server: {}) (client: {})",
|
||||
maybe_addr!(conn.remote_addr()),
|
||||
@ -131,6 +169,7 @@ pub async fn run(
|
||||
String::from_utf8_lossy(&data)
|
||||
);
|
||||
}
|
||||
Response::Pong => (), // ignore (should never happen)
|
||||
}
|
||||
|
||||
if let Err(err) = tx.close().await {
|
||||
|
Reference in New Issue
Block a user