This commit is contained in:
Luuk van Oijen 2023-11-23 10:59:47 +01:00
parent 04c1b76402
commit cfea87251b

View File

@ -181,117 +181,113 @@ impl Server {
socket.readable().await.expect("Failed to wait for socket to become readable!"); socket.readable().await.expect("Failed to wait for socket to become readable!");
let mut tmp = vec![0u8; 1]; let mut tmp = vec![0u8; 1];
while socket.peek(&mut tmp).await.expect("Failed to peek socket!") == 0 {
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
}
// Authentication works a little differently than normal // Authentication works a little differently than normal
// Not sure why, but the BeamMP source code shows they // Not sure why, but the BeamMP source code shows they
// also only read a single byte during authentication // also only read a single byte during authentication
socket.read_exact(&mut tmp).await.expect("Failed to read from socket!"); if socket.read_exact(&mut tmp).await.is_ok() {
let code = tmp[0]; let code = tmp[0];
match code as char { match code as char {
'C' => { 'C' => {
info!("hi"); info!("hi");
let mut client = Client::new(socket).await; let mut client = Client::new(socket).await;
match client.authenticate(&cfg_ref).await { match client.authenticate(&cfg_ref).await {
Ok(is_client) if is_client => { Ok(is_client) if is_client => {
info!("bye"); info!("bye");
ci_ref.send(client).await; ci_ref.send(client).await;
}, },
Ok(_is_client) => { Ok(_is_client) => {
debug!("Downloader?"); debug!("Downloader?");
}, },
Err(e) => { Err(e) => {
error!("Authentication error occured, kicking player..."); error!("Authentication error occured, kicking player...");
error!("{:?}", e); error!("{:?}", e);
client.kick("Failed to authenticate player!").await; client.kick("Failed to authenticate player!").await;
// client.disconnect(); // client.disconnect();
}
}
},
'D' => {
// Download connection (for old protocol)
// This crashes the client after sending over 1 mod.
// I have no idea why, perhaps I'm missing something that I'm supposed to send it.
// TODO: Implement this: https://github.com/BeamMP/BeamMP-Server/blob/master/src/TNetwork.cpp#L775
socket.readable().await;
let mut tmp = [0u8; 1];
socket.read_exact(&mut tmp).await;
let id = tmp[0] as usize;
debug!("[D] HandleDownload connection for client id: {}", id);
let mut sent_mods = Vec::new();
'download: while let Ok(_) = socket.writable().await {
{
let lock = CLIENT_MOD_PROGRESS.lock().await;
if lock.get(&(id as u8)).is_none() { continue; }
}
let mod_id = {
let lock = CLIENT_MOD_PROGRESS.lock().await;
*lock.get(&(id as u8)).unwrap()
};
if sent_mods.contains(&mod_id) { continue; }
debug!("[D] Starting download!");
let mut mod_name = {
if mod_id < 0 {
break 'download;
} }
if mod_id as usize >= cfg_ref.mods.len() {
break 'download;
}
let bmod = &cfg_ref.mods[mod_id as usize]; // TODO: This is a bit uhh yeah
debug!("[D] Mod name: {}", bmod.0);
bmod.0.clone()
};
if mod_name.starts_with("/") == false {
mod_name = format!("/{mod_name}");
} }
},
'D' => {
// Download connection (for old protocol)
// This crashes the client after sending over 1 mod.
// I have no idea why, perhaps I'm missing something that I'm supposed to send it.
// TODO: Implement this: https://github.com/BeamMP/BeamMP-Server/blob/master/src/TNetwork.cpp#L775
debug!("[D] Starting transfer of mod {mod_name}!"); socket.readable().await;
let mut tmp = [0u8; 1];
let mod_path = format!("Resources/Client{mod_name}"); socket.read_exact(&mut tmp).await;
if let Ok(file_data) = std::fs::read(mod_path) { let id = tmp[0] as usize;
debug!("[D] HandleDownload connection for client id: {}", id);
let mut sent_mods = Vec::new();
'download: while let Ok(_) = socket.writable().await {
{ {
trace!("[D] Sending packets!"); let lock = CLIENT_MOD_PROGRESS.lock().await;
if let Err(e) = socket.write(&file_data[(file_data.len()/2)..]).await { if lock.get(&(id as u8)).is_none() { continue; }
error!("{:?}", e);
}
trace!("[D] Packets sent!");
} }
} let mod_id = {
let lock = CLIENT_MOD_PROGRESS.lock().await;
*lock.get(&(id as u8)).unwrap()
};
if sent_mods.contains(&mod_id) { continue; }
debug!("[D] Starting download!");
let mut mod_name = {
if mod_id < 0 {
break 'download;
}
if mod_id as usize >= cfg_ref.mods.len() {
break 'download;
}
sent_mods.push(mod_id); let bmod = &cfg_ref.mods[mod_id as usize]; // TODO: This is a bit uhh yeah
} debug!("[D] Mod name: {}", bmod.0);
debug!("[D] Done!");
}, bmod.0.clone()
'G' => { };
// This is probably an HTTP GET request!
let mut tmp = [0u8; 3]; if mod_name.starts_with("/") == false {
socket.read_exact(&mut tmp).await.expect("Failed to read from socket!"); mod_name = format!("/{mod_name}");
if tmp[0] as char == 'E' && tmp[1] as char == 'T' && tmp[2] as char == ' ' { }
trace!("HTTP GET request found!");
handle_http_get(socket).await; debug!("[D] Starting transfer of mod {mod_name}!");
} else {
trace!("Unknown G packet received, not sure what to do!"); let mod_path = format!("Resources/Client{mod_name}");
} if let Ok(file_data) = std::fs::read(mod_path) {
}, {
_ => { trace!("[D] Sending packets!");
tokio::task::yield_now().await; if let Err(e) = socket.write(&file_data[(file_data.len()/2)..]).await {
}, error!("{:?}", e);
}; }
trace!("[D] Packets sent!");
}
}
sent_mods.push(mod_id);
}
debug!("[D] Done!");
},
'G' => {
// This is probably an HTTP GET request!
let mut tmp = [0u8; 3];
socket.read_exact(&mut tmp).await.expect("Failed to read from socket!");
if tmp[0] as char == 'E' && tmp[1] as char == 'T' && tmp[2] as char == ' ' {
trace!("HTTP GET request found!");
handle_http_get(socket).await;
} else {
trace!("Unknown G packet received, not sure what to do!");
}
},
_ => {
tokio::task::yield_now().await;
},
};
}
}); });
info!("Client pushed to joinset!"); info!("Client pushed to joinset!");
} }
Err(e) => error!("Failed to accept incoming connection: {:?}", e), Err(e) => error!("Failed to accept incoming connection: {:?}", e),
} }
}, },
_ = tokio::time::sleep(tokio::time::Duration::from_millis(50)) => { _ = tokio::time::sleep(tokio::time::Duration::from_millis(50)) => {},
error!("time out!");
},
} }
if set.is_empty() == false { if set.is_empty() == false {
@ -299,12 +295,8 @@ impl Server {
// Because join_next() is cancel safe, we can simply cancel it after N duration // Because join_next() is cancel safe, we can simply cancel it after N duration
// so at worst this client acceptance loop blocks for N duration // so at worst this client acceptance loop blocks for N duration
tokio::select! { tokio::select! {
_ = tokio::time::sleep(tokio::time::Duration::from_millis(10)) => { _ = set.join_next() => {},
error!("join_next timed out!"); _ = tokio::time::sleep(tokio::time::Duration::from_millis(10)) => {},
},
_ = set.join_next() => {
info!("join_next ran!");
},
} }
} }
} }