fix: made uploads and downloads more consistent

This commit is contained in:
Andrew Rioux
2024-09-25 12:33:58 -04:00
parent 1dfd7e0499
commit 4ae9f38812
6 changed files with 103 additions and 59 deletions

View File

@@ -30,6 +30,7 @@ pub async fn download_file(
};
let mut current_packet_count = 0;
let mut current_buffer = 0;
while current_packet_count < packet_count {
let mut buffers: Vec<Option<Vec<u8>>> = vec![None; FILE_BUFFER_BUFFER_SIZE];
@@ -39,7 +40,7 @@ pub async fn download_file(
Ok(Response::DownloadFileSegment(fid, bid, buf)) if fid == id => {
buffers[bid as usize] = Some(buf);
}
Ok(Response::GetDownloadFileStatus(fid, up_to)) if fid == id => {
Ok(Response::GetDownloadFileStatus(fid, bid, up_to)) if fid == id && bid == current_buffer => {
let needed = buffers[..up_to as usize]
.iter()
.enumerate()
@@ -51,9 +52,10 @@ pub async fn download_file(
let is_empty = needed.is_empty();
conn.send_command(Command::DownloadFileStatus(id, needed))
conn.send_command(Command::DownloadFileStatus(id, bid, needed))
.await?;
current_buffer += 1;
if is_empty {
current_packet_count += up_to;
break;

View File

@@ -1,6 +1,6 @@
use std::{path::PathBuf, sync::Arc};
use std::{path::PathBuf, sync::Arc, time::Duration};
use tokio::{fs, io::AsyncReadExt};
use tokio::{fs, io::AsyncReadExt, time::timeout};
use sparse_05_common::messages::{
Command, Response, FILE_BUFFER_BUFFER_SIZE, FILE_TRANSFER_PACKET_SIZE,
@@ -30,6 +30,8 @@ pub async fn upload_file(
}
};
let mut current_buffer = 0;
loop {
let mut file_data: Vec<Vec<u8>> = Vec::with_capacity(FILE_BUFFER_BUFFER_SIZE);
let mut buffer = [0u8; FILE_TRANSFER_PACKET_SIZE];
@@ -52,21 +54,30 @@ pub async fn upload_file(
}
loop {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
conn.send_command(Command::GetUploadStatus(id, file_data.len() as u64))
conn.send_command(Command::GetUploadStatus(id, current_buffer, file_data.len() as u64))
.await?;
let buffers_needed = loop {
let resp = conn.get_response().await?;
println!("Requesting status...");
if let Response::UploadFileStatus(iid, buffers) = resp {
if id == iid {
break buffers;
let buffers_needed = loop {
let resp = timeout(Duration::from_millis(125), conn.get_response()).await;
match resp {
Ok(Ok(Response::UploadFileStatus(iid, bid, buffers))) => {
if id == iid && bid == current_buffer {
break buffers;
}
}
Ok(..) => {}
Err(..) => {
conn.send_command(Command::GetUploadStatus(id, current_buffer, file_data.len() as u64))
.await?;
}
}
};
println!("Sent buffer {current_buffer}; packets needed: {:?}", buffers_needed);
if buffers_needed.len() == 0 {
break;
}
@@ -79,8 +90,12 @@ pub async fn upload_file(
);
conn.send_command(comm).await?;
}
println!("Resent packets");
}
current_buffer += 1;
if done {
break;
}