mirror of
https://github.com/LCE-Hub/LCE-Emerald-Launcher.git
synced 2026-07-19 08:47:19 +00:00
fix(lceonline): \r as well as game connection timeout
This commit is contained in:
parent
75e16fbab9
commit
3e07befcc7
|
|
@ -10,7 +10,7 @@ async fn read_line(stream: &mut TcpStream) -> Result<String, String> {
|
|||
loop {
|
||||
stream.read_exact(&mut byte).await.map_err(|e| e.to_string())?;
|
||||
if byte[0] == b'\n' { break; }
|
||||
buf.push(byte[0]);
|
||||
if byte[0] != b'\r' { buf.push(byte[0]); }
|
||||
}
|
||||
String::from_utf8(buf).map_err(|e| e.to_string())
|
||||
}
|
||||
|
|
@ -32,18 +32,23 @@ async fn run_host_relay(
|
|||
.map_err(|e| format!("Proxy connect failed: {}", e))?;
|
||||
|
||||
write_line(&mut host_conn, &format!("HOST {} 0", auth_token)).await?;
|
||||
let game_stream = loop {
|
||||
match TcpStream::connect(format!("127.0.0.1:{}", game_port)).await {
|
||||
Ok(s) => break s,
|
||||
Err(_) => {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => return Err("Cancelled".into()),
|
||||
_ = tokio::time::sleep(std::time::Duration::from_secs(1)) => {}
|
||||
let game_stream = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(30),
|
||||
async {
|
||||
loop {
|
||||
match TcpStream::connect(format!("127.0.0.1:{}", game_port)).await {
|
||||
Ok(s) => return Ok::<_, String>(s),
|
||||
Err(_) => {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => return Err("Cancelled".into()),
|
||||
_ = tokio::time::sleep(std::time::Duration::from_secs(1)) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
).await
|
||||
.map_err(|_| "Timed out waiting for game to start".to_string())??;
|
||||
let client_line = read_line(&mut host_conn).await?;
|
||||
let client_parts: Vec<&str> = client_line.split_whitespace().collect();
|
||||
if client_parts.len() < 2 || client_parts[0] != "CLIENT" {
|
||||
|
|
@ -167,7 +172,9 @@ pub async fn start_host_relay(
|
|||
let session_id = "__host__".to_string();
|
||||
{
|
||||
let mut tokens = proxy_state.cancel_tokens.lock().await;
|
||||
tokens.insert(session_id.clone(), cancel.clone());
|
||||
if let Some(old) = tokens.insert(session_id.clone(), cancel.clone()) {
|
||||
old.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
let result = run_host_relay(&proxy_state, &addr, &auth_token, game_port, cancel).await;
|
||||
|
|
@ -205,11 +212,12 @@ pub async fn start_relay_proxy(
|
|||
#[tauri::command]
|
||||
pub async fn stop_proxy(proxy_state: State<'_, ProxyGuard>, session_id: String) -> Result<(), String> {
|
||||
let mut tokens = proxy_state.cancel_tokens.lock().await;
|
||||
if let Some(token) = tokens.remove(&session_id) {
|
||||
let found = tokens.remove(&session_id);
|
||||
if let Some(token) = found {
|
||||
token.cancel();
|
||||
let mut port = proxy_state.local_port.lock().await;
|
||||
*port = None;
|
||||
}
|
||||
let mut port = proxy_state.local_port.lock().await;
|
||||
*port = None;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue