合并 0.4.7 双向并发传输封版

# Conflicts:
#	README.md
#	android/app/src/main/java/com/qft/optical/DownloadClient.kt
#	android/app/src/main/java/com/qft/optical/UploadClient.kt
#	docs/PROTOCOL.md
#	windows/src/sender.rs
This commit is contained in:
zhy
2026-08-11 14:27:30 +08:00
18 changed files with 407 additions and 74 deletions
+2 -2
View File
@@ -23,7 +23,7 @@ use std::{
// a nominally larger page that is only decoded 10~18 times per second.
const DEFAULT_CHUNK_SIZE: u16 = 320;
const UI_FONT_NAME: &str = "qft-ui";
const APP_VERSION: &str = "0.4.6";
const APP_VERSION: &str = "0.4.7";
fn main() -> eframe::Result {
let options = eframe::NativeOptions {
@@ -446,7 +446,7 @@ impl QftApp {
ui.horizontal_wrapped(|ui| {
ui.heading("Android → 电脑 · 扫码直连");
ui.separator();
ui.label("UDP 快速发现 · 4/8/16 MiB 自适应分块 · 最高 8 路并行 · 不经过外网");
ui.label("UDP 快速发现 · 64 KiB–16 MiB 自适应分块 · 目标 8 路并行 · 不经过外网");
});
ui.add_space(10.0);
let running = self.receive.server.is_some();
+3 -1
View File
@@ -21,6 +21,8 @@ use std::{
pub const PREFERRED_PORT: u16 = 39393;
pub const DISCOVERY_PORT: u16 = 39392;
pub const PARALLEL_CHUNK_SIZE: u64 = 8 * 1024 * 1024;
const MIN_PARALLEL_CHUNK_SIZE: u64 = 64 * 1024;
const MAX_PARALLEL_CHUNK_SIZE: u64 = 16 * 1024 * 1024;
const IO_BUFFER: usize = 1024 * 1024;
#[derive(Clone, Debug)]
@@ -250,7 +252,7 @@ fn init_parallel_upload(
.parse().context("X-QFT-Size 无效")?;
let chunk_size = headers.get("x-qft-chunk-size")
.and_then(|v| v.parse::<u64>().ok())
.map(|v| v.clamp(1024 * 1024, 16 * 1024 * 1024))
.map(|v| v.clamp(MIN_PARALLEL_CHUNK_SIZE, MAX_PARALLEL_CHUNK_SIZE))
.unwrap_or(PARALLEL_CHUNK_SIZE);
let total_chunks_u64 = if size == 0 { 0 } else { (size + chunk_size - 1) / chunk_size };
if total_chunks_u64 > u32::MAX as u64 { bail!("文件分块数过大") }
+55 -4
View File
@@ -128,6 +128,11 @@ fn handle_client(
token: &str,
events: &Sender<SenderEvent>,
) -> Result<()> {
// The listener is non-blocking so its owner can poll for shutdown. On
// Windows an accepted socket can retain that mode, causing a large Range
// response to fail with WSAEWOULDBLOCK (10035) between write calls.
// Downloads must wait under the explicit read/write timeouts instead.
stream.set_nonblocking(false)?;
stream.set_nodelay(true).ok();
stream.set_read_timeout(Some(Duration::from_secs(20)))?;
stream.set_write_timeout(Some(Duration::from_secs(120)))?;
@@ -192,11 +197,10 @@ fn handle_client(
while left > 0 {
let want = left.min(buf.len() as u64) as usize;
let n = input.read(&mut buf[..want])?;
if n == 0 { break }
if n == 0 { bail!("发送文件读取提前结束:还缺 {left} 字节") }
stream.write_all(&buf[..n])?;
left -= n as u64;
}
if left != 0 { bail!("发送文件读取提前结束:还缺 {left} 字节") }
stream.flush()?;
let _ = events.send(SenderEvent::DownloadCompleted { device, file_name: file_name.to_owned() });
Ok(())
@@ -249,6 +253,7 @@ mod tests {
net::{TcpListener, TcpStream},
sync::mpsc,
thread,
time::Duration,
};
#[test]
@@ -284,13 +289,59 @@ mod tests {
assert_response(&resumed, "HTTP/1.1 206 Partial Content", "cdef");
let header = String::from_utf8_lossy(&resumed[..header_end(&resumed)]);
assert!(header.contains("Content-Range: bytes 2-5/6"));
let bounded = request(address, Some("bytes=1-3"));
assert_response(&bounded, "HTTP/1.1 206 Partial Content", "bcd");
let bounded = request(address, Some("bytes=2-4"));
assert_response(&bounded, "HTTP/1.1 206 Partial Content", "cde");
let header = String::from_utf8_lossy(&bounded[..header_end(&bounded)]);
assert!(header.contains("Content-Range: bytes 2-4/6"));
server.join().unwrap();
fs::remove_file(path).unwrap();
}
#[test]
fn large_download_waits_for_the_client_after_a_nonblocking_accept() {
let path = std::env::temp_dir().join(format!(
"qft-sender-{}-nonblocking.bin",
std::process::id(),
));
let payload = vec![0x5Au8; 2 * 1024 * 1024];
fs::write(&path, &payload).unwrap();
let file_size = payload.len() as u64;
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
let (events, _receiver) = mpsc::channel::<SenderEvent>();
let path_for_server = path.clone();
let server = thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
// Reproduce the Windows mode inheritance that caused Range
// writes to fail with WSAEWOULDBLOCK (10035).
stream.set_nonblocking(true).unwrap();
handle_client(
&mut stream,
&path_for_server,
"large.bin",
file_size,
"session",
&events,
)
});
let mut client = TcpStream::connect(address).unwrap();
client.write_all(
b"GET /qft/download HTTP/1.1\r\nHost: localhost\r\nX-QFT-Token: session\r\n\r\n",
).unwrap();
client.flush().unwrap();
thread::sleep(Duration::from_millis(50));
let mut response = Vec::new();
client.read_to_end(&mut response).unwrap();
server.join().unwrap().unwrap();
let body_start = header_end(&response);
assert!(String::from_utf8_lossy(&response[..body_start]).starts_with("HTTP/1.1 200 OK"));
assert_eq!(&response[body_start..], payload.as_slice());
fs::remove_file(path).unwrap();
}
fn request(address: std::net::SocketAddr, range: Option<&str>) -> Vec<u8> {
let mut stream = TcpStream::connect(address).unwrap();
write!(