4 Commits

Author SHA1 Message Date
zhy200308 cb92397300 修正为仅打包Rust桌面端 2026-08-11 11:48:09 +08:00
zhy200308 dffee5365d Merge remote-tracking branch 'origin/main' 2026-08-11 11:31:44 +08:00
zhy200308 a4edf734b6 优化大文件稳定并发传输与打包流程 2026-08-11 11:27:40 +08:00
kmcc f260ff4266 Merge pull request 'New test' (#1) from new-test into main
Reviewed-on: https://git.xskjhy.top/kmcc/QFT/pulls/1
2026-08-11 10:44:48 +08:00
13 changed files with 670 additions and 101 deletions
+15 -1
View File
@@ -103,8 +103,10 @@ v0.3 的稳定性优化继续保留:
- 扫码连接后 UDP `QFTDISC2` 快速发现; - 扫码连接后 UDP `QFTDISC2` 快速发现;
- gateway / endpoint / 当前子网 fallback; - gateway / endpoint / 当前子网 fallback;
- HTTP 请求明确绑定扫码建立的 `Network`; - HTTP 请求明确绑定扫码建立的 `Network`;
- 单文件最高 8 worker 并行; - 配网请求在传输页持续持有,仅返回首页时释放;短暂网络波动会自动接入新的 `Network`;
- 上传与下载单文件均最高 8 worker 并行;
- 4/8/16 MiB 自适应上传块; - 4/8/16 MiB 自适应上传块;
- 16 MiB 有界 Range 并行下载,完成位图支持掉线/重启后只补缺块;
- Windows positioned writes + `.qftpart` + SHA-256 + 最终 rename。 - Windows positioned writes + `.qftpart` + SHA-256 + 最终 rename。
## Android 版本 ## Android 版本
@@ -139,6 +141,18 @@ APK:
`android/app/build/outputs/apk/debug/app-debug.apk` `android/app/build/outputs/apk/debug/app-debug.apk`
## 一键打包
脚本只打包 `windows/` 下的 Rust 桌面端:执行 `cargo test` 和 `cargo build --release`,随后把 Rust 二进制、协议说明和 SHA-256 校验文件收集到带时间戳的 `dist/QFT-Rust-*` 目录并生成压缩包。脚本不检查、下载或调用 Android SDK、Java、Gradle:
```bat
package.bat
```
```bash
./package.sh
```
## 字体 ## 字体
Windows UI 优先读取 `windows/assets/qft-ui.ttf`,也可用 `QFT_FONT_PATH` 指向你自己的中文 TTF。仓库不附带第三方字体文件。 Windows UI 优先读取 `windows/assets/qft-ui.ttf`,也可用 `QFT_FONT_PATH` 指向你自己的中文 TTF。仓库不附带第三方字体文件。
@@ -13,9 +13,13 @@ import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
import java.io.RandomAccessFile
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
import java.security.MessageDigest import java.security.MessageDigest
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
data class DownloadedFile(val name: String, val size: Long, val uri: Uri?) data class DownloadedFile(val name: String, val size: Long, val uri: Uri?)
@@ -24,7 +28,11 @@ data class DownloadedFile(val name: String, val size: Long, val uri: Uri?)
object DownloadClient { object DownloadClient {
private const val TAG = "QftDownload" private const val TAG = "QftDownload"
private const val BUFFER_SIZE = 1024 * 1024 private const val BUFFER_SIZE = 1024 * 1024
private const val ATTEMPTS = 5 private const val SERIAL_ATTEMPTS = 3
private const val RANGE_ATTEMPTS = 10
private const val PARALLEL_MIN_FILE_SIZE = 32L * 1024L * 1024L
private const val PARALLEL_CHUNK_SIZE = 16L * 1024L * 1024L
private const val MAX_WORKERS = 8
/** 整份重下的轮数。仅在 SHA-256 不匹配(缓存已损坏)时才会用到第二轮。 */ /** 整份重下的轮数。仅在 SHA-256 不匹配(缓存已损坏)时才会用到第二轮。 */
private const val VERIFY_ROUNDS = 2 private const val VERIFY_ROUNDS = 2
@@ -34,7 +42,7 @@ object DownloadClient {
fun download( fun download(
context: Context, context: Context,
resolver: ContentResolver, resolver: ContentResolver,
network: Network?, networkProvider: () -> Network?,
baseUrl: String, baseUrl: String,
pairing: PairingPayload, pairing: PairingPayload,
deviceName: String, deviceName: String,
@@ -51,14 +59,17 @@ object DownloadClient {
val expectedSha = requireNotNull(pairing.sha256) val expectedSha = requireNotNull(pairing.sha256)
val partial = partialFile(context, pairing.session) val partial = partialFile(context, pairing.session)
partial.parentFile?.let { if (!it.exists() && !it.mkdirs()) error("无法创建接收缓存目录") } partial.parentFile?.let { if (!it.exists() && !it.mkdirs()) error("无法创建接收缓存目录") }
if (partial.length() > expectedSize) partial.delete() if (partial.length() > expectedSize) {
partial.delete()
completionMapFile(partial).delete()
}
// 落盘时缓存文件与目标文件同时存在,需要约 2 倍空间。 // 落盘时缓存文件与目标文件同时存在,需要约 2 倍空间。
// 不预检的话,这里会在下载 100% 之后才失败,用户看到的是「快结束时失败」。 // 不预检的话,这里会在下载 100% 之后才失败,用户看到的是「快结束时失败」。
ensureSpaceForTransfer(partial, expectedSize) ensureSpaceForTransfer(partial, expectedSize)
repeat(VERIFY_ROUNDS) { round -> repeat(VERIFY_ROUNDS) { round ->
receiveUntilComplete(network, baseUrl, pairing, deviceName, partial, receiveUntilComplete(networkProvider, baseUrl, pairing, deviceName, partial,
expectedSize, abortReason, onProgress) expectedSize, abortReason, onProgress)
onPhase("正在校验文件完整性…") onPhase("正在校验文件完整性…")
@@ -73,12 +84,14 @@ object DownloadClient {
throw SaveFailure("文件已完整收到,但保存到 Downloads/QFT 失败:${failureDetail(e)}", e) throw SaveFailure("文件已完整收到,但保存到 Downloads/QFT 失败:${failureDetail(e)}", e)
} }
partial.delete() partial.delete()
completionMapFile(partial).delete()
Log.i(TAG, "completed size=$expectedSize") Log.i(TAG, "completed size=$expectedSize")
return result return result
} }
Log.w(TAG, "sha mismatch round=${round + 1}/$VERIFY_ROUNDS expected=$expectedSha actual=$actual") Log.w(TAG, "sha mismatch round=${round + 1}/$VERIFY_ROUNDS expected=$expectedSha actual=$actual")
partial.delete() partial.delete()
completionMapFile(partial).delete()
if (round + 1 >= VERIFY_ROUNDS) { if (round + 1 >= VERIFY_ROUNDS) {
error("文件校验失败:重新完整下载后 SHA-256 仍不匹配,请让电脑重新生成二维码") error("文件校验失败:重新完整下载后 SHA-256 仍不匹配,请让电脑重新生成二维码")
} }
@@ -89,7 +102,7 @@ object DownloadClient {
/** 传输阶段:断点续传 + 有限重试。只有这一段的失败才算「下载失败」。 */ /** 传输阶段:断点续传 + 有限重试。只有这一段的失败才算「下载失败」。 */
private fun receiveUntilComplete( private fun receiveUntilComplete(
network: Network?, networkProvider: () -> Network?,
baseUrl: String, baseUrl: String,
pairing: PairingPayload, pairing: PairingPayload,
deviceName: String, deviceName: String,
@@ -98,13 +111,19 @@ object DownloadClient {
abortReason: () -> String?, abortReason: () -> String?,
onProgress: (received: Long, total: Long) -> Unit, onProgress: (received: Long, total: Long) -> Unit,
) { ) {
if (expectedSize >= PARALLEL_MIN_FILE_SIZE && expectedSize > 0L) {
receiveParallel(networkProvider, baseUrl, pairing, deviceName, partial,
expectedSize, abortReason, onProgress)
return
}
var last: Exception? = null var last: Exception? = null
repeat(ATTEMPTS) { attempt -> repeat(SERIAL_ATTEMPTS) { attempt ->
abortReason()?.let { throw IllegalStateException(it, last) } abortReason()?.let { throw IllegalStateException(it, last) }
try { try {
Log.i(TAG, "attempt=${attempt + 1}/$ATTEMPTS partial=${partial.length()}/$expectedSize endpoint=$baseUrl") Log.i(TAG, "serial attempt=${attempt + 1}/$SERIAL_ATTEMPTS partial=${partial.length()}/$expectedSize endpoint=$baseUrl")
if (partial.length() < expectedSize) { if (partial.length() < expectedSize) {
receiveAttempt(network, baseUrl, pairing, deviceName, partial, receiveAttempt(networkProvider(), baseUrl, pairing, deviceName, partial,
expectedSize, abortReason, onProgress) expectedSize, abortReason, onProgress)
} }
if (partial.length() != expectedSize) { if (partial.length() != expectedSize) {
@@ -113,9 +132,9 @@ object DownloadClient {
return return
} catch (e: Exception) { } catch (e: Exception) {
last = e last = e
Log.w(TAG, "attempt=${attempt + 1}/$ATTEMPTS failed partial=${partial.length()}/$expectedSize: ${failureDetail(e)}", e) Log.w(TAG, "serial attempt=${attempt + 1}/$SERIAL_ATTEMPTS failed partial=${partial.length()}/$expectedSize: ${failureDetail(e)}", e)
abortReason()?.let { throw IllegalStateException(it, e) } abortReason()?.let { throw IllegalStateException(it, e) }
if (attempt + 1 < ATTEMPTS) { if (attempt + 1 < SERIAL_ATTEMPTS) {
try { try {
Thread.sleep(250L * (attempt + 1)) Thread.sleep(250L * (attempt + 1))
} catch (interrupted: InterruptedException) { } catch (interrupted: InterruptedException) {
@@ -127,11 +146,209 @@ object DownloadClient {
} }
val progressed = partial.length() val progressed = partial.length()
throw IllegalStateException( throw IllegalStateException(
"从电脑下载失败,已重试 $ATTEMPTS 次(已收到 ${progressed}/$expectedSize 字节):${failureDetail(last)}", "从电脑下载失败(断点 ${progressed}/$expectedSize 字节):${failureDetail(last)}",
last, last,
) )
} }
/**
* 大文件并行直传。每个 worker 请求一个有界 HTTP Range,并写入预分配文件的
* 固定 offset。完成位图单独落盘,所以应用进程或某条 TCP 流中断后只补缺块,
* 不会把 1 GB 文件从头再传一遍。
*/
private fun receiveParallel(
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
partial: File,
expectedSize: Long,
abortReason: () -> String?,
onProgress: (received: Long, total: Long) -> Unit,
) {
val totalChunks = ((expectedSize + PARALLEL_CHUNK_SIZE - 1L) / PARALLEL_CHUNK_SIZE).toInt()
val mapFile = completionMapFile(partial)
val completed = loadCompletionMap(mapFile, totalChunks)
// A partial from the old sequential downloader has no bitmap. Preserve
// only complete chunks; an incomplete tail is safely downloaded again.
val oldLength = partial.length()
if (!mapFile.exists() && oldLength in 1 until expectedSize) {
for (index in completed.indices) {
val endExclusive = minOf(expectedSize, (index + 1L) * PARALLEL_CHUNK_SIZE)
if (endExclusive <= oldLength) completed[index] = true
}
}
RandomAccessFile(partial, "rw").use { it.setLength(expectedSize) }
persistCompletionMap(mapFile, completed)
val initialBytes = completed.indices.sumOf { index ->
if (completed[index]) chunkLength(index, expectedSize) else 0L
}
val received = AtomicLong(initialBytes)
val lastReport = AtomicLong(0L)
val next = AtomicInteger(0)
onProgress(initialBytes, expectedSize)
val workers = totalChunks.coerceAtMost(
Runtime.getRuntime().availableProcessors().coerceIn(2, MAX_WORKERS)
).coerceAtLeast(1)
Log.i(TAG, "parallel download chunks=$totalChunks workers=$workers completed=$initialBytes/$expectedSize")
val pool = Executors.newFixedThreadPool(workers)
val futures = (0 until workers).map {
pool.submit {
RandomAccessFile(partial, "rw").use { output ->
while (!Thread.currentThread().isInterrupted) {
abortReason()?.let { error(it) }
val index = next.getAndIncrement()
if (index >= totalChunks) break
if (completed[index]) continue
val start = index.toLong() * PARALLEL_CHUNK_SIZE
val length = chunkLength(index, expectedSize)
receiveRangeWithRetry(
networkProvider, baseUrl, pairing, deviceName,
output, index, start, length, expectedSize, abortReason,
)
// Data reaches stable storage before its completion bit.
// A crash can therefore only cause a harmless re-download.
output.fd.sync()
synchronized(completed) {
completed[index] = true
persistCompletionMap(mapFile, completed)
}
val done = received.addAndGet(length)
val now = android.os.SystemClock.elapsedRealtime()
val previous = lastReport.get()
if (now - previous >= 80L && lastReport.compareAndSet(previous, now)) {
onProgress(done.coerceAtMost(expectedSize), expectedSize)
}
}
}
}
}
val failures = mutableListOf<Throwable>()
try {
futures.forEach { future ->
try { future.get() } catch (e: Exception) { failures += (e.cause ?: e) }
}
} finally {
pool.shutdownNow()
}
if (failures.isNotEmpty()) throw IllegalStateException(
"并行直传中断,已完成 ${received.get()}/$expectedSize 字节;下次将只补传缺块:${failureDetail(failures.first())}",
failures.first(),
)
if (completed.any { !it }) error("并行直传结束但仍有缺块")
onProgress(expectedSize, expectedSize)
}
private fun receiveRangeWithRetry(
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
output: RandomAccessFile,
index: Int,
start: Long,
length: Long,
expectedSize: Long,
abortReason: () -> String?,
) {
var last: Exception? = null
repeat(RANGE_ATTEMPTS) { attempt ->
abortReason()?.let { error(it) }
try {
receiveRange(
networkProvider(), baseUrl, pairing, deviceName, output,
start, length, expectedSize, abortReason,
)
return
} catch (e: Exception) {
last = e
Log.w(TAG, "range=$index attempt=${attempt + 1}/$RANGE_ATTEMPTS failed: ${failureDetail(e)}")
if (attempt + 1 < RANGE_ATTEMPTS) {
try { Thread.sleep((200L shl attempt).coerceAtMost(3_000L)) }
catch (_: InterruptedException) {
Thread.currentThread().interrupt()
throw e
}
}
}
}
throw IllegalStateException("区段 $index 传输失败:${failureDetail(last)}", last)
}
private fun receiveRange(
network: Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
output: RandomAccessFile,
start: Long,
length: Long,
expectedSize: Long,
abortReason: () -> String?,
) {
val end = start + length - 1L
val conn = open(network, URL("$baseUrl/qft/download"))
try {
conn.requestMethod = "GET"
conn.connectTimeout = 8_000
conn.readTimeout = 90_000
conn.useCaches = false
conn.setRequestProperty("Connection", "keep-alive")
conn.setRequestProperty("X-QFT-Token", pairing.session)
conn.setRequestProperty("X-QFT-Device", Base64Url.encode(deviceName.toByteArray(Charsets.UTF_8)))
conn.setRequestProperty("Range", "bytes=$start-$end")
val code = conn.responseCode
if (code != HttpURLConnection.HTTP_PARTIAL) {
val message = try { conn.errorStream?.bufferedReader()?.use { it.readText().take(300) } } catch (_: Exception) { null }
error("电脑端不支持并行 Range(HTTP $code)${message?.let { ": $it" } ?: ""}")
}
val contentRange = conn.getHeaderField("Content-Range").orEmpty()
if (contentRange != "bytes $start-$end/$expectedSize") {
error("下载服务返回了无效区段:$contentRange")
}
if (conn.contentLengthLong >= 0L && conn.contentLengthLong != length) {
error("区段长度不匹配:${conn.contentLengthLong} != $length")
}
output.seek(start)
var remaining = length
conn.inputStream.use { input ->
val buffer = ByteArray(BUFFER_SIZE)
while (remaining > 0L) {
abortReason()?.let { error(it) }
val n = input.read(buffer, 0, minOf(buffer.size.toLong(), remaining).toInt())
if (n < 0) error("区段提前结束:还缺 $remaining 字节")
if (n == 0) continue
output.write(buffer, 0, n)
remaining -= n.toLong()
}
}
} finally {
conn.disconnect()
}
}
private fun chunkLength(index: Int, total: Long): Long =
minOf(PARALLEL_CHUNK_SIZE, total - index.toLong() * PARALLEL_CHUNK_SIZE)
private fun completionMapFile(partial: File): File = File(partial.parentFile, partial.name + ".map")
private fun loadCompletionMap(file: File, count: Int): BooleanArray {
val result = BooleanArray(count)
val bytes = try { if (file.exists()) file.readBytes() else ByteArray(0) } catch (_: Exception) { ByteArray(0) }
if (bytes.size == count) bytes.indices.forEach { result[it] = bytes[it].toInt() == 1 }
return result
}
private fun persistCompletionMap(file: File, completed: BooleanArray) {
FileOutputStream(file, false).use { output ->
output.write(ByteArray(completed.size) { if (completed[it]) 1 else 0 })
output.fd.sync()
}
}
/** 传输 + 落盘阶段合计需要的可用空间。 */ /** 传输 + 落盘阶段合计需要的可用空间。 */
private fun ensureSpaceForTransfer(partial: File, expectedSize: Long) { private fun ensureSpaceForTransfer(partial: File, expectedSize: Long) {
val root = partial.parentFile ?: return val root = partial.parentFile ?: return
@@ -164,13 +381,9 @@ object DownloadClient {
try { try {
conn.requestMethod = "GET" conn.requestMethod = "GET"
conn.connectTimeout = 5_000 conn.connectTimeout = 5_000
// A QFT LAN transfer should start immediately. The old 120-second conn.readTimeout = 60_000
// read timeout let Huawei tear down a local-only Wi-Fi request
// before a retry could occur. A short timeout safely resumes from
// the persisted Range offset and finishes all retries sooner.
conn.readTimeout = 12_000
conn.useCaches = false conn.useCaches = false
conn.setRequestProperty("Connection", "close") conn.setRequestProperty("Connection", "keep-alive")
conn.setRequestProperty("X-QFT-Token", pairing.session) conn.setRequestProperty("X-QFT-Token", pairing.session)
conn.setRequestProperty("X-QFT-Device", Base64Url.encode(deviceName.toByteArray(Charsets.UTF_8))) conn.setRequestProperty("X-QFT-Device", Base64Url.encode(deviceName.toByteArray(Charsets.UTF_8)))
if (requestedOffset > 0L) conn.setRequestProperty("Range", "bytes=$requestedOffset-") if (requestedOffset > 0L) conn.setRequestProperty("Range", "bytes=$requestedOffset-")
@@ -208,8 +421,7 @@ object DownloadClient {
FileOutputStream(partial, offset > 0L).use { output -> FileOutputStream(partial, offset > 0L).use { output ->
val buffer = ByteArray(BUFFER_SIZE) val buffer = ByteArray(BUFFER_SIZE)
while (true) { while (true) {
// 网络已断开时立刻收手:否则会在 readTimeout 上白等 12 秒, // 用户明确返回首页时才中止;普通 TCP 波动由 Range 续传恢复。
// 再乘以 5 次重试,用户等一分钟才看到错误。
abortReason()?.let { error(it) } abortReason()?.let { error(it) }
val n = input.read(buffer) val n = input.read(buffer)
if (n < 0) break if (n < 0) break
@@ -76,7 +76,7 @@ class MainActivity : ComponentActivity() {
private lateinit var progress: ProgressBar private lateinit var progress: ProgressBar
private var pendingPair: PairingPayload? = null private var pendingPair: PairingPayload? = null
private var connectedNetwork: Network? = null @Volatile private var connectedNetwork: Network? = null
private var connectedBaseUrl: String? = null private var connectedBaseUrl: String? = null
private val selectedFiles = mutableListOf<SelectedFile>() private val selectedFiles = mutableListOf<SelectedFile>()
private var managingFilesForPair: PairingPayload? = null private var managingFilesForPair: PairingPayload? = null
@@ -146,6 +146,7 @@ class MainActivity : ComponentActivity() {
} }
private fun showHome(message: String? = null) { private fun showHome(message: String? = null) {
transferAbortReason = "已返回首页,传输已停止"
pairConnectionAttempt++ pairConnectionAttempt++
stopCamera() stopCamera()
scannerMode = ScannerMode.NONE scannerMode = ScannerMode.NONE
@@ -602,6 +603,7 @@ class MainActivity : ComponentActivity() {
private fun connectPair(pair: PairingPayload) { private fun connectPair(pair: PairingPayload) {
val attempt = ++pairConnectionAttempt val attempt = ++pairConnectionAttempt
transferAbortReason = null
showConnecting(pair, pair.wifi?.let { "正在请求连接 ${it.ssid}…" } ?: "正在使用当前局域网探测电脑…") showConnecting(pair, pair.wifi?.let { "正在请求连接 ${it.ssid}…" } ?: "正在使用当前局域网探测电脑…")
wifiConnector.connect( wifiConnector.connect(
pairing = pair, pairing = pair,
@@ -611,6 +613,10 @@ class MainActivity : ComponentActivity() {
onAvailable = available@{ network -> onAvailable = available@{ network ->
if (attempt != pairConnectionAttempt) return@available if (attempt != pairConnectionAttempt) return@available
connectedNetwork = network connectedNetwork = network
// A retained WifiNetworkSpecifier request may deliver a fresh
// Network after a brief radio fluctuation. Do not restart the
// page or transfer; chunk retries will use this new handle.
if (connectedBaseUrl != null) return@available
transferExecutor.execute { transferExecutor.execute {
if (attempt != pairConnectionAttempt) return@execute if (attempt != pairConnectionAttempt) return@execute
runOnUiThread { updateConnectingStatus("网络路由已就绪 · 正在 UDP 快速发现电脑…") } runOnUiThread { updateConnectingStatus("网络路由已就绪 · 正在 UDP 快速发现电脑…") }
@@ -657,14 +663,10 @@ class MainActivity : ComponentActivity() {
}, },
onUnavailable = { message -> runOnUiThread { onUnavailable = { message -> runOnUiThread {
if (attempt != pairConnectionAttempt) return@runOnUiThread if (attempt != pairConnectionAttempt) return@runOnUiThread
// Huawei can report onLost while a bound local-only HTTP // onLost is recovered inside WifiConnector. onUnavailable is
// socket is unwinding. Let DownloadClient surface its precise // terminal (system/user rejected the retained request), so it
// HTTP/timeout cause instead of replacing it with this generic // is the only network callback that aborts an active transfer.
// message mid-retry.
if (pair.direction == TransferDirection.DOWNLOAD_TO_PHONE && downloadRunning) { if (pair.direction == TransferDirection.DOWNLOAD_TO_PHONE && downloadRunning) {
// 让下载线程立刻收手并如实报告断网,而不是继续跑完
// 5 次「重试 → 12 秒超时」,最后给出一个与真因无关的
// 「下载失败,已重试 N 次」。
transferAbortReason = message transferAbortReason = message
downloadStatusText?.apply { downloadStatusText?.apply {
text = "QFT 本地网络已断开,正在结束当前下载请求…" text = "QFT 本地网络已断开,正在结束当前下载请求…"
@@ -705,8 +707,8 @@ class MainActivity : ComponentActivity() {
private fun showPairError(pair: PairingPayload?, message: String) { private fun showPairError(pair: PairingPayload?, message: String) {
// Do not unregister here: Android may still be displaying its // Do not unregister here: Android may still be displaying its
// Wi-Fi-selection sheet, and unregistering at that moment produces // Wi-Fi-selection sheet, and unregistering at that moment produces
// "the app cancelled the device selection request". The next attempt // "the app cancelled the device selection request". A retry reuses
// (or returning home) releases the previous request safely. // this retained request; returning home is what explicitly releases it.
pairConnectionAttempt++ pairConnectionAttempt++
connectedNetwork = null connectedNetwork = null
connectedBaseUrl = null connectedBaseUrl = null
@@ -718,7 +720,7 @@ class MainActivity : ComponentActivity() {
root.addView(button("重试连接").apply { setOnClickListener { ensureWifiPermissionThenConnect(pair) } }) root.addView(button("重试连接").apply { setOnClickListener { ensureWifiPermissionThenConnect(pair) } })
root.addView(space(10)) root.addView(space(10))
} }
root.addView(button("重新扫码").apply { setOnClickListener { requestScanner(ScannerMode.PAIR) } }) root.addView(button("返回首页重新扫码").apply { setOnClickListener { showHome() } })
root.addView(space(10)) root.addView(space(10))
root.addView(button("返回首页").apply { setOnClickListener { showHome() } }) root.addView(button("返回首页").apply { setOnClickListener { showHome() } })
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) }) setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
@@ -776,7 +778,13 @@ class MainActivity : ComponentActivity() {
root.addView(space(18)) root.addView(space(18))
root.addView(card().apply { root.addView(card().apply {
addView(text("扫码配置已完成", 17f, SUCCESS, true)) addView(text("扫码配置已完成", 17f, SUCCESS, true))
addView(text("电脑已自动确认会话,正在通过本地网络直接传输。", 14f, MUTED), matchWrap(top = 8)) addView(text(
if (total >= 32L * 1024L * 1024L)
"连接将保持到返回首页;正在通过本地网络进行最高 8 路并行直传。"
else
"连接将保持到返回首页;正在通过本地网络直接传输。",
14f, MUTED,
), matchWrap(top = 8))
addView(text(fileName, 15f, Color.WHITE), matchWrap(top = 12)) addView(text(fileName, 15f, Color.WHITE), matchWrap(top = 12))
addView(text(humanBytes(total), 13f, MUTED), matchWrap(top = 4)) addView(text(humanBytes(total), 13f, MUTED), matchWrap(top = 4))
}) })
@@ -801,7 +809,7 @@ class MainActivity : ComponentActivity() {
val saved = DownloadClient.download( val saved = DownloadClient.download(
context = this@MainActivity, context = this@MainActivity,
resolver = contentResolver, resolver = contentResolver,
network = connectedNetwork, networkProvider = { connectedNetwork },
baseUrl = baseUrl, baseUrl = baseUrl,
pairing = pair, pairing = pair,
deviceName = receiverDeviceLabel, deviceName = receiverDeviceLabel,
@@ -980,11 +988,12 @@ class MainActivity : ComponentActivity() {
} }
UploadClient.upload( UploadClient.upload(
resolver = contentResolver, resolver = contentResolver,
network = connectedNetwork, networkProvider = { connectedNetwork },
baseUrl = baseUrl, baseUrl = baseUrl,
pairing = pair, pairing = pair,
deviceName = receiverDeviceLabel, deviceName = receiverDeviceLabel,
file = file, file = file,
abortReason = { transferAbortReason },
) { sent, total -> ) { sent, total ->
runOnUiThread { runOnUiThread {
val p = if (total == 0L) 1000 else ((sent.toDouble() / total) * 1000).toInt().coerceIn(0, 1000) val p = if (total == 0L) 1000 else ((sent.toDouble() / total) * 1000).toInt().coerceIn(0, 1000)
@@ -20,12 +20,14 @@ import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicLong
import java.util.UUID
data class SelectedFile( data class SelectedFile(
val uri: Uri, val uri: Uri,
val name: String, val name: String,
val size: Long, val size: Long,
val uploadKey: String = UUID.randomUUID().toString(),
) )
object UploadClient { object UploadClient {
@@ -42,7 +44,7 @@ object UploadClient {
// QFT Direct can briefly reset a TCP flow while the Android network route // QFT Direct can briefly reset a TCP flow while the Android network route
// settles. Chunk PUTs and the init/finish control calls are safe to retry; // settles. Chunk PUTs and the init/finish control calls are safe to retry;
// the Windows receiver treats a repeated chunk as an idempotent success. // the Windows receiver treats a repeated chunk as an idempotent success.
private const val LOCAL_REQUEST_ATTEMPTS = 3 private const val LOCAL_REQUEST_ATTEMPTS = 8
fun describeFile(resolver: ContentResolver, uri: Uri): SelectedFile? { fun describeFile(resolver: ContentResolver, uri: Uri): SelectedFile? {
var name = "qft-upload.bin" var name = "qft-upload.bin"
@@ -220,16 +222,20 @@ object UploadClient {
fun upload( fun upload(
resolver: ContentResolver, resolver: ContentResolver,
network: Network?, networkProvider: () -> Network?,
baseUrl: String, baseUrl: String,
pairing: PairingPayload, pairing: PairingPayload,
deviceName: String, deviceName: String,
file: SelectedFile, file: SelectedFile,
abortReason: () -> String? = { null },
onProgress: (sent: Long, total: Long) -> Unit, onProgress: (sent: Long, total: Long) -> Unit,
) { ) {
if (pairing.version >= 2 && file.size >= PARALLEL_MIN_FILE_SIZE && isSeekable(resolver, file)) { if (pairing.version >= 2 && file.size >= PARALLEL_MIN_FILE_SIZE) {
require(isSeekable(resolver, file)) {
"大文件必须使用可随机读取的本地文件才能并行直传;请先把云盘文件下载到手机本地后再选择"
}
try { try {
uploadParallel(resolver, network, baseUrl, pairing, deviceName, file, onProgress) uploadParallel(resolver, networkProvider, baseUrl, pairing, deviceName, file, abortReason, onProgress)
return return
} catch (e: Exception) { } catch (e: Exception) {
// Never mask a transient v2 failure by immediately switching to a // Never mask a transient v2 failure by immediately switching to a
@@ -242,14 +248,20 @@ object UploadClient {
} }
} }
Log.i(TAG, "using single-stream upload: size=${file.size}, endpoint=$baseUrl") Log.i(TAG, "using single-stream upload: size=${file.size}, endpoint=$baseUrl")
uploadLegacy(resolver, network, baseUrl, pairing, deviceName, file, onProgress) uploadLegacy(resolver, networkProvider(), baseUrl, pairing, deviceName, file, abortReason, onProgress)
} }
private data class InitReply(val uploadId: String, val chunkSize: Long, val totalChunks: Int) private data class InitReply(
val uploadId: String,
val chunkSize: Long,
val totalChunks: Int,
val pendingChunks: IntArray,
)
private fun <T> retryLocalRequest(label: String, block: () -> T): T { private fun <T> retryLocalRequest(label: String, abortReason: () -> String?, block: () -> T): T {
var last: Throwable? = null var last: Throwable? = null
repeat(LOCAL_REQUEST_ATTEMPTS) { attempt -> repeat(LOCAL_REQUEST_ATTEMPTS) { attempt ->
abortReason()?.let { throw IllegalStateException(it, last) }
try { try {
return block() return block()
} catch (e: Throwable) { } catch (e: Throwable) {
@@ -260,7 +272,7 @@ object UploadClient {
e, e,
) )
if (attempt + 1 < LOCAL_REQUEST_ATTEMPTS) { if (attempt + 1 < LOCAL_REQUEST_ATTEMPTS) {
try { Thread.sleep(120L * (attempt + 1)) } catch (_: InterruptedException) { try { Thread.sleep((250L shl attempt).coerceAtMost(3_000L)) } catch (_: InterruptedException) {
Thread.currentThread().interrupt() Thread.currentThread().interrupt()
throw e throw e
} }
@@ -288,27 +300,37 @@ object UploadClient {
private fun uploadParallel( private fun uploadParallel(
resolver: ContentResolver, resolver: ContentResolver,
network: Network?, networkProvider: () -> Network?,
baseUrl: String, baseUrl: String,
pairing: PairingPayload, pairing: PairingPayload,
deviceName: String, deviceName: String,
file: SelectedFile, file: SelectedFile,
abortReason: () -> String?,
onProgress: (sent: Long, total: Long) -> Unit, onProgress: (sent: Long, total: Long) -> Unit,
) { ) {
Log.i(TAG, "parallel init: size=${file.size}, endpoint=$baseUrl") Log.i(TAG, "parallel init: size=${file.size}, endpoint=$baseUrl")
val init = initUpload(network, baseUrl, pairing, deviceName, file) val init = initUpload(networkProvider, baseUrl, pairing, deviceName, file, file.uploadKey, abortReason)
Log.i(TAG, "parallel init complete: chunks=${init.totalChunks}, chunkSize=${init.chunkSize}") Log.i(TAG, "parallel init complete: chunks=${init.totalChunks}, pending=${init.pendingChunks.size}, chunkSize=${init.chunkSize}")
if (init.totalChunks == 0) { if (init.totalChunks == 0) {
finishUpload(network, baseUrl, pairing, deviceName, init.uploadId) finishUpload(networkProvider, baseUrl, pairing, deviceName, init.uploadId, abortReason)
onProgress(0, 0) onProgress(0, 0)
return return
} }
val cpuWorkers = Runtime.getRuntime().availableProcessors().coerceIn(2, MAX_WORKERS) // Upload workers are network/disk bound, not CPU bound. Large files
val workerCount = init.totalChunks.coerceAtMost(cpuWorkers).coerceAtLeast(1) // therefore use all eight lanes even on a 4-core phone.
if (init.pendingChunks.isEmpty()) {
finishUpload(networkProvider, baseUrl, pairing, deviceName, init.uploadId, abortReason)
onProgress(file.size, file.size)
return
}
val workerCount = init.pendingChunks.size.coerceAtMost(MAX_WORKERS).coerceAtLeast(1)
val pool = Executors.newFixedThreadPool(workerCount) val pool = Executors.newFixedThreadPool(workerCount)
val next = AtomicInteger(0) val next = AtomicInteger(0)
val sent = AtomicLong(0) val pendingBytes = init.pendingChunks.sumOf { index ->
minOf(init.chunkSize, file.size - index.toLong() * init.chunkSize)
}
val sent = AtomicLong((file.size - pendingBytes).coerceAtLeast(0L))
val lastReport = AtomicLong(0) val lastReport = AtomicLong(0)
val failures = mutableListOf<Throwable>() val failures = mutableListOf<Throwable>()
val futures = (0 until workerCount).map { val futures = (0 until workerCount).map {
@@ -318,13 +340,15 @@ object UploadClient {
val channel = input.channel val channel = input.channel
val buffer = ByteBuffer.allocate(STREAM_BUFFER) val buffer = ByteBuffer.allocate(STREAM_BUFFER)
while (!Thread.currentThread().isInterrupted) { while (!Thread.currentThread().isInterrupted) {
val index = next.getAndIncrement() abortReason()?.let { error(it) }
if (index >= init.totalChunks) break val pendingIndex = next.getAndIncrement()
if (pendingIndex >= init.pendingChunks.size) break
val index = init.pendingChunks[pendingIndex]
val offset = index.toLong() * init.chunkSize val offset = index.toLong() * init.chunkSize
val length = minOf(init.chunkSize, file.size - offset) val length = minOf(init.chunkSize, file.size - offset)
sendChunk( sendChunk(
network, baseUrl, pairing, deviceName, networkProvider, baseUrl, pairing, deviceName,
init.uploadId, index, channel, offset, length, buffer, init.uploadId, index, channel, offset, length, buffer, abortReason,
) )
val totalSent = sent.addAndGet(length) val totalSent = sent.addAndGet(length)
val now = android.os.SystemClock.elapsedRealtime() val now = android.os.SystemClock.elapsedRealtime()
@@ -345,7 +369,7 @@ object UploadClient {
pool.shutdownNow() pool.shutdownNow()
} }
if (failures.isNotEmpty()) throw failures.first() if (failures.isNotEmpty()) throw failures.first()
finishUpload(network, baseUrl, pairing, deviceName, init.uploadId) finishUpload(networkProvider, baseUrl, pairing, deviceName, init.uploadId, abortReason)
onProgress(file.size, file.size) onProgress(file.size, file.size)
} }
@@ -357,10 +381,10 @@ object UploadClient {
} }
private fun initUpload( private fun initUpload(
network: Network?, baseUrl: String, pairing: PairingPayload, networkProvider: () -> Network?, baseUrl: String, pairing: PairingPayload,
deviceName: String, file: SelectedFile, deviceName: String, file: SelectedFile, requestKey: String, abortReason: () -> String?,
): InitReply = retryLocalRequest("本地上传初始化失败") { ): InitReply = retryLocalRequest("本地上传初始化失败", abortReason) {
val conn = open(network, URL("$baseUrl/qft/upload/init")) val conn = open(networkProvider(), URL("$baseUrl/qft/upload/init"))
try { try {
conn.requestMethod = "POST" conn.requestMethod = "POST"
conn.doOutput = true conn.doOutput = true
@@ -371,15 +395,27 @@ object UploadClient {
conn.setRequestProperty("X-QFT-Filename", b64(file.name)) conn.setRequestProperty("X-QFT-Filename", b64(file.name))
conn.setRequestProperty("X-QFT-Size", file.size.toString()) conn.setRequestProperty("X-QFT-Size", file.size.toString())
conn.setRequestProperty("X-QFT-Chunk-Size", desiredChunkSize(file.size).toString()) conn.setRequestProperty("X-QFT-Chunk-Size", desiredChunkSize(file.size).toString())
conn.setRequestProperty("X-QFT-Upload-Key", requestKey)
conn.outputStream.close() conn.outputStream.close()
val code = conn.responseCode val code = conn.responseCode
if (code != 200) error("初始化并行上传失败 HTTP $code") if (code != 200) error("初始化并行上传失败 HTTP $code")
val body = conn.inputStream.bufferedReader().use { it.readText() } val body = conn.inputStream.bufferedReader().use { it.readText() }
val obj = JSONObject(body) val obj = JSONObject(body)
val totalChunks = obj.getInt("total_chunks")
val missing = obj.optJSONArray("missing")
val pending = if (missing == null) {
IntArray(totalChunks) { it }
} else {
IntArray(missing.length()) { missing.getInt(it) }
}
if (pending.any { it !in 0 until totalChunks } || pending.distinct().size != pending.size) {
error("电脑返回了无效的缺块列表")
}
InitReply( InitReply(
uploadId = obj.getString("upload_id"), uploadId = obj.getString("upload_id"),
chunkSize = obj.getLong("chunk_size"), chunkSize = obj.getLong("chunk_size"),
totalChunks = obj.getInt("total_chunks"), totalChunks = totalChunks,
pendingChunks = pending,
) )
} finally { } finally {
conn.disconnect() conn.disconnect()
@@ -387,17 +423,17 @@ object UploadClient {
} }
private fun sendChunk( private fun sendChunk(
network: Network?, baseUrl: String, pairing: PairingPayload, deviceName: String, networkProvider: () -> Network?, baseUrl: String, pairing: PairingPayload, deviceName: String,
uploadId: String, index: Int, channel: FileChannel, offset: Long, length: Long, uploadId: String, index: Int, channel: FileChannel, offset: Long, length: Long,
buffer: ByteBuffer, buffer: ByteBuffer, abortReason: () -> String?,
) = retryLocalRequest("分块 $index 上传失败") { ) = retryLocalRequest("分块 $index 上传失败", abortReason) {
Log.d(TAG, "chunk start: index=$index, bytes=$length, endpoint=$baseUrl") Log.d(TAG, "chunk start: index=$index, bytes=$length, endpoint=$baseUrl")
val conn = open(network, URL("$baseUrl/qft/upload/chunk/$uploadId/$index")) val conn = open(networkProvider(), URL("$baseUrl/qft/upload/chunk/$uploadId/$index"))
try { try {
conn.requestMethod = "POST" conn.requestMethod = "POST"
conn.doOutput = true conn.doOutput = true
conn.connectTimeout = 4_000 conn.connectTimeout = 8_000
conn.readTimeout = 15_000 conn.readTimeout = 90_000
conn.setFixedLengthStreamingMode(length) conn.setFixedLengthStreamingMode(length)
conn.setRequestProperty("Content-Type", "application/octet-stream") conn.setRequestProperty("Content-Type", "application/octet-stream")
commonHeaders(conn, pairing, deviceName) commonHeaders(conn, pairing, deviceName)
@@ -406,6 +442,7 @@ object UploadClient {
var remaining = length var remaining = length
val array = buffer.array() val array = buffer.array()
while (remaining > 0) { while (remaining > 0) {
abortReason()?.let { error(it) }
buffer.clear() buffer.clear()
buffer.limit(minOf(array.size.toLong(), remaining).toInt()) buffer.limit(minOf(array.size.toLong(), remaining).toInt())
val n = channel.read(buffer) val n = channel.read(buffer)
@@ -428,10 +465,10 @@ object UploadClient {
} }
private fun finishUpload( private fun finishUpload(
network: Network?, baseUrl: String, pairing: PairingPayload, networkProvider: () -> Network?, baseUrl: String, pairing: PairingPayload,
deviceName: String, uploadId: String, deviceName: String, uploadId: String, abortReason: () -> String?,
) = retryLocalRequest("本地上传完成确认失败") { ) = retryLocalRequest("本地上传完成确认失败", abortReason) {
val conn = open(network, URL("$baseUrl/qft/upload/finish/$uploadId")) val conn = open(networkProvider(), URL("$baseUrl/qft/upload/finish/$uploadId"))
try { try {
conn.requestMethod = "POST" conn.requestMethod = "POST"
conn.doOutput = true conn.doOutput = true
@@ -458,6 +495,7 @@ object UploadClient {
pairing: PairingPayload, pairing: PairingPayload,
deviceName: String, deviceName: String,
file: SelectedFile, file: SelectedFile,
abortReason: () -> String?,
onProgress: (sent: Long, total: Long) -> Unit, onProgress: (sent: Long, total: Long) -> Unit,
) { ) {
val conn = open(network, URL("$baseUrl/qft/upload")) val conn = open(network, URL("$baseUrl/qft/upload"))
@@ -478,6 +516,7 @@ object UploadClient {
var sent = 0L var sent = 0L
var lastReport = 0L var lastReport = 0L
while (true) { while (true) {
abortReason()?.let { error(it) }
val n = input.read(buf) val n = input.read(buf)
if (n <= 0) break if (n <= 0) break
output.write(buf, 0, n) output.write(buf, 0, n)
@@ -520,7 +559,7 @@ object UploadClient {
private fun commonHeaders(conn: HttpURLConnection, pairing: PairingPayload, deviceName: String) { private fun commonHeaders(conn: HttpURLConnection, pairing: PairingPayload, deviceName: String) {
conn.useCaches = false conn.useCaches = false
conn.setRequestProperty("Connection", "close") conn.setRequestProperty("Connection", "keep-alive")
conn.setRequestProperty("X-QFT-Token", pairing.session) conn.setRequestProperty("X-QFT-Token", pairing.session)
conn.setRequestProperty("X-QFT-Device", b64(deviceName)) conn.setRequestProperty("X-QFT-Device", b64(deviceName))
} }
@@ -26,6 +26,7 @@ class WifiConnector(context: Context) {
private val wifiManager = appContext.getSystemService(Context.WIFI_SERVICE) as WifiManager private val wifiManager = appContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
private val handler = Handler(Looper.getMainLooper()) private val handler = Handler(Looper.getMainLooper())
private var callback: ConnectivityManager.NetworkCallback? = null private var callback: ConnectivityManager.NetworkCallback? = null
@Volatile private var currentNetwork: Network? = null
private var generation = 0 private var generation = 0
private var legacyPreviousNetworkId: Int? = null private var legacyPreviousNetworkId: Int? = null
private var legacyTargetNetworkId: Int? = null private var legacyTargetNetworkId: Int? = null
@@ -65,11 +66,17 @@ class WifiConnector(context: Context) {
// 同一个 SSID 的请求还挂在系统那里且没有终结:换掉 UI 回调即可, // 同一个 SSID 的请求还挂在系统那里且没有终结:换掉 UI 回调即可,
// 千万不要 unregister 再重新 requestNetwork。 // 千万不要 unregister 再重新 requestNetwork。
if (wifi != null && callback != null && pendingSsid == wifi.ssid && if (wifi != null && callback != null && pendingSsid == wifi.ssid &&
pendingTerminal?.get() == false && pendingDelivered?.get() == false) { pendingTerminal?.get() == false) {
Log.i(TAG, "reuse outstanding request SSID=${wifi.ssid}") Log.i(TAG, "reuse outstanding request SSID=${wifi.ssid}")
listener = Listener(onStatus, onAvailable, onUnavailable) listener = Listener(onStatus, onAvailable, onUnavailable)
notifyStatus("系统配网请求仍在进行中,正在继续等待…") val active = currentNetwork
scheduleWaitHints(generation, wifi.ssid) if (active != null) {
notifyStatus("QFT 本地网络仍保持连接")
notifyAvailable(active)
} else {
notifyStatus("系统配网请求仍在进行中,正在自动恢复连接…")
scheduleWaitHints(generation, wifi.ssid)
}
return return
} }
@@ -80,7 +87,8 @@ class WifiConnector(context: Context) {
if (wifi == null) { if (wifi == null) {
notifyStatus("正在使用当前局域网发现电脑…") notifyStatus("正在使用当前局域网发现电脑…")
val wifiNetwork = bestWifiNetwork() val wifiNetwork = bestWifiNetwork()
notifyAvailable(wifiNetwork ?: connectivity.activeNetwork) currentNetwork = wifiNetwork ?: connectivity.activeNetwork
notifyAvailable(currentNetwork)
return return
} }
@@ -125,10 +133,12 @@ class WifiConnector(context: Context) {
val cb = object : ConnectivityManager.NetworkCallback() { val cb = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) { override fun onAvailable(network: Network) {
if (run != generation || terminal.get()) return if (run != generation || terminal.get()) return
currentNetwork = network
Log.i(TAG, "attempt=$run framework onAvailable network=$network") Log.i(TAG, "attempt=$run framework onAvailable network=$network")
notifyStatus("Wi-Fi 已关联,正在等待本地 IP/路由…") notifyStatus("Wi-Fi 已关联,正在等待本地 IP/路由…")
waitUntilUsable(run, network, ::notifyStatus) { waitUntilUsable(run, network, ::notifyStatus) {
if (run == generation && !terminal.get() && delivered.compareAndSet(false, true)) { if (run == generation && !terminal.get() && currentNetwork == network &&
delivered.compareAndSet(false, true)) {
Log.i(TAG, "attempt=$run route ready network=$network") Log.i(TAG, "attempt=$run route ready network=$network")
notifyStatus("QFT 本地网络已就绪,正在快速发现电脑…") notifyStatus("QFT 本地网络已就绪,正在快速发现电脑…")
notifyAvailable(network) notifyAvailable(network)
@@ -137,8 +147,9 @@ class WifiConnector(context: Context) {
} }
override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) { override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
if (run != generation || terminal.get() || delivered.get()) return if (run != generation || terminal.get() || delivered.get() || currentNetwork != network) return
if (hasIpv4(linkProperties) && delivered.compareAndSet(false, true)) { if (hasIpv4(linkProperties) && delivered.compareAndSet(false, true)) {
currentNetwork = network
Log.i(TAG, "attempt=$run IPv4 ready network=$network") Log.i(TAG, "attempt=$run IPv4 ready network=$network")
notifyStatus("QFT 本地网络已就绪,正在快速发现电脑…") notifyStatus("QFT 本地网络已就绪,正在快速发现电脑…")
notifyAvailable(network) notifyAvailable(network)
@@ -147,6 +158,7 @@ class WifiConnector(context: Context) {
override fun onUnavailable() { override fun onUnavailable() {
if (run != generation || !terminal.compareAndSet(false, true)) return if (run != generation || !terminal.compareAndSet(false, true)) return
currentNetwork = null
val waited = SystemClock.elapsedRealtime() - startedAt val waited = SystemClock.elapsedRealtime() - startedAt
Log.w(TAG, "attempt=$run framework onUnavailable after ${waited}ms") Log.w(TAG, "attempt=$run framework onUnavailable after ${waited}ms")
// Android 不区分「用户点了取消」和「始终没扫到这个热点」, // Android 不区分「用户点了取消」和「始终没扫到这个热点」,
@@ -164,9 +176,14 @@ class WifiConnector(context: Context) {
} }
override fun onLost(network: Network) { override fun onLost(network: Network) {
if (run != generation || !delivered.get() || !terminal.compareAndSet(false, true)) return if (run != generation || terminal.get() || currentNetwork != network) return
Log.w(TAG, "attempt=$run network lost network=$network") Log.w(TAG, "attempt=$run network lost network=$network")
notifyUnavailable("QFT 临时网络已断开") currentNetwork = null
delivered.set(false)
// requestNetwork stays registered. Android can therefore
// deliver a replacement Network without another system picker;
// range/chunk retries wait for and then use that new handle.
notifyStatus("QFT 网络短暂波动,正在保持会话并自动恢复…")
} }
} }
callback = cb callback = cb
@@ -246,6 +263,7 @@ class WifiConnector(context: Context) {
val network = bestWifiNetwork() val network = bestWifiNetwork()
if (network != null && hasIpv4(connectivity.getLinkProperties(network))) { if (network != null && hasIpv4(connectivity.getLinkProperties(network))) {
notifyStatus("QFT Wi-Fi 已连接,正在快速发现电脑…") notifyStatus("QFT Wi-Fi 已连接,正在快速发现电脑…")
currentNetwork = network
notifyAvailable(network) notifyAvailable(network)
return return
} }
@@ -375,6 +393,7 @@ class WifiConnector(context: Context) {
try { connectivity.unregisterNetworkCallback(it) } catch (_: Exception) {} try { connectivity.unregisterNetworkCallback(it) } catch (_: Exception) {}
} }
callback = null callback = null
currentNetwork = null
listener = null listener = null
pendingSsid = null pendingSsid = null
pendingDelivered = null pendingDelivered = null
+15
View File
@@ -86,3 +86,18 @@ POST /qft/upload/finish
``` ```
Windows preallocates a `.qftpart`, writes chunks at explicit offsets, verifies SHA-256, then renames to the final file. Per-file worker count is capped at 8; chunk size is selected from 4/8/16 MiB by file size. Windows preallocates a `.qftpart`, writes chunks at explicit offsets, verifies SHA-256, then renames to the final file. Per-file worker count is capped at 8; chunk size is selected from 4/8/16 MiB by file size.
Repeated `init` requests carrying the same `X-QFT-Upload-Key` resume the matching active upload session. The response may include a `missing` chunk-index array; the client sends only those chunks. Chunk completion is recorded only after the positioned write reaches stable storage.
## 5. Windows → Android direct download
Files of at least 32 MiB use up to 8 parallel bounded range requests:
```text
GET /qft/download
Range: bytes=<start>-<end>
```
Android preallocates an app-private `.qftpart`, writes each range at its fixed offset and persists a separate completion bitmap. A reconnect or process restart downloads only missing ranges. After every range is present, Android verifies the QR-carried SHA-256 before publishing the file to `Downloads/QFT`.
The Android `WifiNetworkSpecifier` request remains registered for the entire transfer page. It is released when the user returns to the home page (or the Activity is destroyed); a replacement `Network` delivered after a brief radio loss is adopted by subsequent chunk/range retries.
+46
View File
@@ -0,0 +1,46 @@
@echo off
setlocal EnableExtensions
cd /d "%~dp0"
echo == QFT Rust one-click package ==
where cargo >nul 2>nul
if errorlevel 1 (
echo ERROR: Rust cargo was not found in PATH.
exit /b 1
)
echo [1/3] Testing and building the Rust Windows release...
pushd windows
cargo test
if errorlevel 1 (popd & exit /b 1)
cargo build --release
if errorlevel 1 (popd & exit /b 1)
popd
for /f "tokens=3" %%V in ('findstr /R /C:"^version =" "windows\Cargo.toml"') do set "QFT_VERSION=%%~V"
for /f "usebackq delims=" %%T in (`powershell -NoProfile -Command "Get-Date -Format yyyyMMdd-HHmmss"`) do set "QFT_STAMP=%%T"
if not defined QFT_VERSION set "QFT_VERSION=unknown"
set "QFT_PACKAGE=QFT-Rust-%QFT_VERSION%-%QFT_STAMP%-windows"
set "QFT_PACKAGE_DIR=%CD%\dist\%QFT_PACKAGE%"
echo [2/3] Collecting Rust artifacts...
mkdir "%QFT_PACKAGE_DIR%" || exit /b 1
copy /y "windows\target\release\qft-windows.exe" "%QFT_PACKAGE_DIR%\QFT-Windows.exe" >nul || exit /b 1
copy /y "README.md" "%QFT_PACKAGE_DIR%\README.md" >nul || exit /b 1
copy /y "TEST_REPORT.md" "%QFT_PACKAGE_DIR%\TEST_REPORT.md" >nul || exit /b 1
copy /y "docs\PROTOCOL.md" "%QFT_PACKAGE_DIR%\PROTOCOL.md" >nul || exit /b 1
echo [3/3] Writing SHA-256 checksums and ZIP archive...
powershell -NoProfile -Command ^
"$dir = [IO.Path]::GetFullPath('%QFT_PACKAGE_DIR%');" ^
"$lines = Get-ChildItem -LiteralPath $dir -File ^| Sort-Object Name ^| ForEach-Object { '{0} {1}' -f (Get-FileHash -Algorithm SHA256 -LiteralPath $_.FullName).Hash.ToLower(), $_.Name };" ^
"[IO.File]::WriteAllLines((Join-Path $dir 'SHA256SUMS.txt'), $lines, [Text.UTF8Encoding]::new($false));" ^
"Compress-Archive -LiteralPath $dir -DestinationPath ($dir + '.zip') -Force"
if errorlevel 1 exit /b 1
echo.
echo PACKAGE COMPLETE
echo Folder: %QFT_PACKAGE_DIR%
echo ZIP: %QFT_PACKAGE_DIR%.zip
exit /b 0
Executable
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env sh
set -eu
QFT_ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "$QFT_ROOT"
echo "== QFT Rust one-click package =="
command -v cargo >/dev/null 2>&1 || { echo "ERROR: Rust cargo was not found in PATH." >&2; exit 1; }
echo "[1/3] Testing and building the Rust desktop release..."
(
cd windows
cargo test
cargo build --release
)
QFT_VERSION=$(awk -F'"' '/^\[package\]/{package=1; next} package && /^version[[:space:]]*=/{print $2; exit} /^\[/{package=0}' windows/Cargo.toml)
[ -n "$QFT_VERSION" ] || QFT_VERSION=unknown
QFT_STAMP=$(date '+%Y%m%d-%H%M%S')
QFT_PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]')
QFT_PACKAGE="QFT-Rust-${QFT_VERSION}-${QFT_STAMP}-${QFT_PLATFORM}"
QFT_PACKAGE_DIR="$QFT_ROOT/dist/$QFT_PACKAGE"
echo "[2/3] Collecting Rust artifacts..."
mkdir -p "$QFT_PACKAGE_DIR"
if [ -f "windows/target/release/qft-windows.exe" ]; then
cp "windows/target/release/qft-windows.exe" "$QFT_PACKAGE_DIR/QFT-Windows.exe"
elif [ -f "windows/target/release/qft-windows" ]; then
cp "windows/target/release/qft-windows" "$QFT_PACKAGE_DIR/QFT-Desktop"
else
echo "ERROR: desktop release binary was not produced." >&2
exit 1
fi
cp README.md TEST_REPORT.md "$QFT_PACKAGE_DIR/"
cp docs/PROTOCOL.md "$QFT_PACKAGE_DIR/PROTOCOL.md"
echo "[3/3] Writing SHA-256 checksums and archive..."
(
cd "$QFT_PACKAGE_DIR"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum ./* > SHA256SUMS.txt
else
shasum -a 256 ./* > SHA256SUMS.txt
fi
)
tar -C "$QFT_ROOT/dist" -czf "$QFT_PACKAGE_DIR.tar.gz" "$QFT_PACKAGE"
echo
echo "PACKAGE COMPLETE"
echo "Folder: $QFT_PACKAGE_DIR"
echo "Archive: $QFT_PACKAGE_DIR.tar.gz"
+3
View File
@@ -4,6 +4,7 @@ import re
ROOT=Path(__file__).resolve().parents[1] ROOT=Path(__file__).resolve().parents[1]
gradle=(ROOT/'android/app/build.gradle.kts').read_text(); manifest=(ROOT/'android/app/src/main/AndroidManifest.xml').read_text() gradle=(ROOT/'android/app/build.gradle.kts').read_text(); manifest=(ROOT/'android/app/src/main/AndroidManifest.xml').read_text()
wifi=(ROOT/'android/app/src/main/java/com/qft/optical/WifiConnector.kt').read_text(); upload=(ROOT/'android/app/src/main/java/com/qft/optical/UploadClient.kt').read_text() wifi=(ROOT/'android/app/src/main/java/com/qft/optical/WifiConnector.kt').read_text(); upload=(ROOT/'android/app/src/main/java/com/qft/optical/UploadClient.kt').read_text()
download=(ROOT/'android/app/src/main/java/com/qft/optical/DownloadClient.kt').read_text()
main=(ROOT/'android/app/src/main/java/com/qft/optical/MainActivity.kt').read_text(); analyzer=(ROOT/'android/app/src/main/java/com/qft/optical/QrAnalyzer.kt').read_text() main=(ROOT/'android/app/src/main/java/com/qft/optical/MainActivity.kt').read_text(); analyzer=(ROOT/'android/app/src/main/java/com/qft/optical/QrAnalyzer.kt').read_text()
gray=(ROOT/'android/app/src/main/java/com/qft/optical/GrayFrame.kt').read_text(); protocol=(ROOT/'android/app/src/main/java/com/qft/optical/Protocol.kt').read_text(); base45=(ROOT/'android/app/src/main/java/com/qft/optical/Base45.kt').read_text(); overlay=(ROOT/'android/app/src/main/java/com/qft/optical/QrLockOverlay.kt').read_text(); security=(ROOT/'android/app/src/main/res/xml/network_security_config.xml').read_text() gray=(ROOT/'android/app/src/main/java/com/qft/optical/GrayFrame.kt').read_text(); protocol=(ROOT/'android/app/src/main/java/com/qft/optical/Protocol.kt').read_text(); base45=(ROOT/'android/app/src/main/java/com/qft/optical/Base45.kt').read_text(); overlay=(ROOT/'android/app/src/main/java/com/qft/optical/QrLockOverlay.kt').read_text(); security=(ROOT/'android/app/src/main/res/xml/network_security_config.xml').read_text()
assert re.search(r'minSdk\s*=\s*24',gradle) assert re.search(r'minSdk\s*=\s*24',gradle)
@@ -12,6 +13,8 @@ assert 'ACCESS_FINE_LOCATION" android:maxSdkVersion="32"' in manifest and 'NEARB
assert 'networkSecurityConfig="@xml/network_security_config"' in manifest and 'cleartextTrafficPermitted="true"' in security assert 'networkSecurityConfig="@xml/network_security_config"' in manifest and 'cleartextTrafficPermitted="true"' in security
assert 'Build.VERSION.SDK_INT >= 29' in wifi and 'connectLegacy' in wifi and 'subnetEndpoints' in wifi and 'directedBroadcast' in wifi assert 'Build.VERSION.SDK_INT >= 29' in wifi and 'connectLegacy' in wifi and 'subnetEndpoints' in wifi and 'directedBroadcast' in wifi
assert 'MAX_WORKERS = 8' in upload and 'QFTDISC2:' in upload assert 'MAX_WORKERS = 8' in upload and 'QFTDISC2:' in upload
assert 'MAX_WORKERS = 8' in download and 'bytes=$start-$end' in download and 'completionMapFile' in download
assert 'requestNetwork(request, cb)' in wifi and 'delivered.set(false)' in wifi
assert 'val targetSize = Size(640, 480)' in main and 'Size(960, 540)' not in main assert 'val targetSize = Size(640, 480)' in main and 'Size(960, 540)' not in main
assert 'Camera2Interop.Extender' in main and 'CONTROL_AE_TARGET_FPS_RANGE' in main and 'STRATEGY_KEEP_ONLY_LATEST' in main assert 'Camera2Interop.Extender' in main and 'CONTROL_AE_TARGET_FPS_RANGE' in main and 'STRATEGY_KEEP_ONLY_LATEST' in main
assert '设备拒绝显式高帧率请求' in main and 'fallbackAnalysis' in main assert '设备拒绝显式高帧率请求' in main and 'fallbackAnalysis' in main
+107
View File
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""Bounded Range + retry + out-of-order positioned-write download self-test."""
import hashlib
import http.client
import random
import tempfile
import threading
from concurrent.futures import ThreadPoolExecutor
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
SIZE = 64 * 1024 * 1024 + 12345
CHUNK = 16 * 1024 * 1024
WORKERS = 8
TOKEN = "parallel_download_test"
def digest(path):
value = hashlib.sha256()
with open(path, "rb", buffering=0) as stream:
while block := stream.read(4 * 1024 * 1024):
value.update(block)
return value.hexdigest()
def main():
assert (1024 * 1024 * 1024 + CHUNK - 1) // CHUNK == 64
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
source = root / "source.bin"
target = root / "target.qftpart"
pattern = bytes((index * 73 + 19) & 0xFF for index in range(1024 * 1024))
with source.open("wb", buffering=0) as stream:
remaining = SIZE
while remaining:
block = pattern[:min(len(pattern), remaining)]
stream.write(block)
remaining -= len(block)
with target.open("wb") as stream:
stream.truncate(SIZE)
class Handler(BaseHTTPRequestHandler):
def log_message(self, *_):
pass
def do_GET(self):
if self.path != "/qft/download" or self.headers.get("X-QFT-Token") != TOKEN:
self.send_error(403)
return
value = self.headers.get("Range", "")
assert value.startswith("bytes=") and "-" in value
start_text, end_text = value[6:].split("-", 1)
start, end = int(start_text), int(end_text)
assert 0 <= start <= end < SIZE
length = end - start + 1
self.send_response(206)
self.send_header("Content-Length", str(length))
self.send_header("Content-Range", f"bytes {start}-{end}/{SIZE}")
self.end_headers()
with source.open("rb", buffering=0) as stream:
stream.seek(start)
remaining = length
while remaining:
block = stream.read(min(1024 * 1024, remaining))
assert block
self.wfile.write(block)
remaining -= len(block)
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
host, port = server.server_address
indices = list(range((SIZE + CHUNK - 1) // CHUNK))
random.Random(42).shuffle(indices)
def receive(index):
start = index * CHUNK
end = min(SIZE, start + CHUNK) - 1
connection = http.client.HTTPConnection(host, port, timeout=10)
connection.request("GET", "/qft/download", headers={
"X-QFT-Token": TOKEN,
"Range": f"bytes={start}-{end}",
})
response = connection.getresponse()
assert response.status == 206
assert response.getheader("Content-Range") == f"bytes {start}-{end}/{SIZE}"
offset = start
with target.open("r+b", buffering=0) as output:
output.seek(start)
while block := response.read(1024 * 1024):
assert output.write(block) == len(block)
offset += len(block)
connection.close()
assert offset == end + 1
try:
with ThreadPoolExecutor(max_workers=WORKERS) as pool:
list(pool.map(receive, indices))
finally:
server.shutdown()
server.server_close()
assert digest(source) == digest(target)
print(f"PASS parallel download: {SIZE} bytes, 16 MiB bounded ranges, {WORKERS} workers, SHA-256 identical; 1 GiB maps to 64 resumable ranges")
if __name__ == "__main__":
main()
+1
View File
@@ -7,6 +7,7 @@ scripts = [
'optical_image_decode_selftest.py', 'optical_image_decode_selftest.py',
'pairing_selftest.py', 'pairing_selftest.py',
'parallel_upload_selftest.py', 'parallel_upload_selftest.py',
'parallel_download_selftest.py',
'udp_discovery_selftest.py', 'udp_discovery_selftest.py',
'local_transfer_integration_selftest.py', 'local_transfer_integration_selftest.py',
'memory_budget_selftest.py', 'memory_budget_selftest.py',
+29 -2
View File
@@ -34,6 +34,7 @@ pub enum ReceiverEvent {
#[derive(Debug)] #[derive(Debug)]
struct UploadSession { struct UploadSession {
upload_key: Option<String>,
device: String, device: String,
file_name: String, file_name: String,
size: u64, size: u64,
@@ -254,9 +255,34 @@ fn init_parallel_upload(
let total_chunks_u64 = if size == 0 { 0 } else { (size + chunk_size - 1) / 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!("文件分块数过大") } if total_chunks_u64 > u32::MAX as u64 { bail!("文件分块数过大") }
let total_chunks = total_chunks_u64 as u32; let total_chunks = total_chunks_u64 as u32;
let upload_key = headers.get("x-qft-upload-key")
.map(|value| value.trim().to_owned())
.filter(|value| !value.is_empty() && value.len() <= 128);
let mut map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
// init is idempotent: if the response packet was lost, the Android client
// may repeat init. Return the existing session so already received chunks
// and the preallocated file are preserved instead of leaking a second part.
for (existing_id, existing) in map.iter() {
if let Ok(session) = existing.lock() {
if upload_key.is_some() && session.upload_key == upload_key {
let missing: Vec<usize> = session.received.iter().enumerate()
.filter_map(|(index, received)| if *received { None } else { Some(index) })
.collect();
let body = json!({
"ok": true,
"upload_id": existing_id,
"chunk_size": session.chunk_size,
"total_chunks": session.total_chunks,
"resumed": true,
"missing": missing,
}).to_string();
return write_response(stream, 200, "application/json", body.as_bytes());
}
}
}
let upload_id = random_id(); let upload_id = random_id();
let mut map = sessions.lock().map_err(|_| anyhow::anyhow!("上传会话锁损坏"))?;
let final_path = unique_path_reserved(save_dir, &file_name, map.values()); let final_path = unique_path_reserved(save_dir, &file_name, map.values());
let final_name = final_path.file_name().and_then(|v| v.to_str()).unwrap_or("qft-upload.bin"); let final_name = final_path.file_name().and_then(|v| v.to_str()).unwrap_or("qft-upload.bin");
let part_path = final_path.with_file_name(format!("{final_name}.{upload_id}.qftpart")); let part_path = final_path.with_file_name(format!("{final_name}.{upload_id}.qftpart"));
@@ -266,6 +292,7 @@ fn init_parallel_upload(
drop(part); drop(part);
let session = UploadSession { let session = UploadSession {
upload_key,
device: device.to_owned(), device: device.to_owned(),
file_name: file_name.clone(), file_name: file_name.clone(),
size, size,
@@ -338,7 +365,7 @@ fn receive_parallel_chunk(
let mut out = OpenOptions::new().write(true).open(&part_path)?; let mut out = OpenOptions::new().write(true).open(&part_path)?;
out.seek(SeekFrom::Start(offset))?; out.seek(SeekFrom::Start(offset))?;
copy_exact(reader, &mut out, content_len)?; copy_exact(reader, &mut out, content_len)?;
out.flush()?; out.sync_data()?;
let received = { let received = {
let mut session = session_arc.lock().map_err(|_| anyhow::anyhow!("上传会话损坏"))?; let mut session = session_arc.lock().map_err(|_| anyhow::anyhow!("上传会话损坏"))?;
+47 -21
View File
@@ -168,18 +168,19 @@ fn handle_client(
if input.metadata()?.len() != size { if input.metadata()?.len() != size {
bail!("发送文件在二维码生成后发生变化,请重新选择文件并生成二维码") bail!("发送文件在二维码生成后发生变化,请重新选择文件并生成二维码")
} }
let offset = parse_range_start(headers.get("range"), size)?; let range = parse_range(headers.get("range"), size)?;
let remaining = size.saturating_sub(offset); let offset = range.start;
let end = range.end;
let remaining = if size == 0 { 0 } else { end.saturating_sub(offset).saturating_add(1) };
let _ = events.send(SenderEvent::DownloadStarted { device: device.clone(), file_name: file_name.to_owned(), size }); let _ = events.send(SenderEvent::DownloadStarted { device: device.clone(), file_name: file_name.to_owned(), size });
if offset == 0 { if headers.get("range").is_none() {
write!(stream, write!(stream,
"HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: {remaining}\r\nAccept-Ranges: bytes\r\nConnection: close\r\nX-QFT-Filename: {}\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: {remaining}\r\nAccept-Ranges: bytes\r\nConnection: close\r\nX-QFT-Filename: {}\r\n\r\n",
URL_SAFE_NO_PAD.encode(file_name.as_bytes()), URL_SAFE_NO_PAD.encode(file_name.as_bytes()),
)?; )?;
} else { } else {
write!(stream, write!(stream,
"HTTP/1.1 206 Partial Content\r\nContent-Type: application/octet-stream\r\nContent-Length: {remaining}\r\nContent-Range: bytes {offset}-{}/{}\r\nAccept-Ranges: bytes\r\nConnection: close\r\nX-QFT-Filename: {}\r\n\r\n", "HTTP/1.1 206 Partial Content\r\nContent-Type: application/octet-stream\r\nContent-Length: {remaining}\r\nContent-Range: bytes {offset}-{end}/{}\r\nAccept-Ranges: bytes\r\nConnection: close\r\nX-QFT-Filename: {}\r\n\r\n",
size - 1,
size, size,
URL_SAFE_NO_PAD.encode(file_name.as_bytes()), URL_SAFE_NO_PAD.encode(file_name.as_bytes()),
)?; )?;
@@ -187,11 +188,15 @@ fn handle_client(
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
input.seek(SeekFrom::Start(offset))?; input.seek(SeekFrom::Start(offset))?;
let mut buf = vec![0u8; IO_BUFFER]; let mut buf = vec![0u8; IO_BUFFER];
loop { let mut left = remaining;
let n = input.read(&mut buf)?; 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 { break }
stream.write_all(&buf[..n])?; stream.write_all(&buf[..n])?;
left -= n as u64;
} }
if left != 0 { bail!("发送文件读取提前结束:还缺 {left} 字节") }
stream.flush()?; stream.flush()?;
let _ = events.send(SenderEvent::DownloadCompleted { device, file_name: file_name.to_owned() }); let _ = events.send(SenderEvent::DownloadCompleted { device, file_name: file_name.to_owned() });
Ok(()) Ok(())
@@ -210,18 +215,34 @@ fn decode_header(value: Option<&String>) -> Option<String> {
String::from_utf8(bytes).ok() String::from_utf8(bytes).ok()
} }
fn parse_range_start(value: Option<&String>, size: u64) -> Result<u64> { #[derive(Clone, Copy, Debug, PartialEq, Eq)]
let Some(value) = value else { return Ok(0) }; struct ByteRange {
start: u64,
end: u64,
}
fn parse_range(value: Option<&String>, size: u64) -> Result<ByteRange> {
if size == 0 {
if value.is_some() { bail!("空文件没有可下载的字节范围") }
return Ok(ByteRange { start: 0, end: 0 });
}
let Some(value) = value else { return Ok(ByteRange { start: 0, end: size - 1 }) };
let suffix = value.trim().strip_prefix("bytes=").context("Range 格式无效")?; let suffix = value.trim().strip_prefix("bytes=").context("Range 格式无效")?;
let start = suffix.strip_suffix('-').context("只支持单段 Range")? let (start_text, end_text) = suffix.split_once('-').context("Range 格式无效")?;
.parse::<u64>().context("Range 起点无效")?; if start_text.is_empty() { bail!("不支持后缀 Range") }
if start >= size { bail!("Range 超出文件范围") } let start = start_text.parse::<u64>().context("Range 起点无效")?;
Ok(start) let end = if end_text.is_empty() {
size - 1
} else {
end_text.parse::<u64>().context("Range 终点无效")?
};
if start >= size || end >= size || end < start { bail!("Range 超出文件范围") }
Ok(ByteRange { start, end })
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{handle_client, parse_range_start, SenderEvent}; use super::{handle_client, parse_range, ByteRange, SenderEvent};
use std::{ use std::{
fs, fs,
io::{Read, Write}, io::{Read, Write},
@@ -231,12 +252,15 @@ mod tests {
}; };
#[test] #[test]
fn accepts_only_a_valid_resumable_byte_range() { fn accepts_open_and_bounded_byte_ranges() {
assert_eq!(parse_range_start(None, 10).unwrap(), 0); assert_eq!(parse_range(None, 10).unwrap(), ByteRange { start: 0, end: 9 });
assert_eq!(parse_range_start(Some(&"bytes=4-".to_owned()), 10).unwrap(), 4); assert_eq!(parse_range(None, 0).unwrap(), ByteRange { start: 0, end: 0 });
assert!(parse_range_start(Some(&"bytes=10-".to_owned()), 10).is_err()); assert_eq!(parse_range(Some(&"bytes=4-".to_owned()), 10).unwrap(), ByteRange { start: 4, end: 9 });
assert!(parse_range_start(Some(&"bytes=-4".to_owned()), 10).is_err()); assert_eq!(parse_range(Some(&"bytes=2-6".to_owned()), 10).unwrap(), ByteRange { start: 2, end: 6 });
assert!(parse_range_start(Some(&"items=4-".to_owned()), 10).is_err()); assert!(parse_range(Some(&"bytes=10-".to_owned()), 10).is_err());
assert!(parse_range(Some(&"bytes=6-2".to_owned()), 10).is_err());
assert!(parse_range(Some(&"bytes=-4".to_owned()), 10).is_err());
assert!(parse_range(Some(&"items=4-".to_owned()), 10).is_err());
} }
#[test] #[test]
@@ -248,7 +272,7 @@ mod tests {
let (events, _receiver) = mpsc::channel::<SenderEvent>(); let (events, _receiver) = mpsc::channel::<SenderEvent>();
let path_for_server = path.clone(); let path_for_server = path.clone();
let server = thread::spawn(move || { let server = thread::spawn(move || {
for _ in 0..2 { for _ in 0..3 {
let (mut stream, _) = listener.accept().unwrap(); let (mut stream, _) = listener.accept().unwrap();
handle_client(&mut stream, &path_for_server, "range.bin", 6, "session", &events).unwrap(); handle_client(&mut stream, &path_for_server, "range.bin", 6, "session", &events).unwrap();
} }
@@ -260,6 +284,8 @@ mod tests {
assert_response(&resumed, "HTTP/1.1 206 Partial Content", "cdef"); assert_response(&resumed, "HTTP/1.1 206 Partial Content", "cdef");
let header = String::from_utf8_lossy(&resumed[..header_end(&resumed)]); let header = String::from_utf8_lossy(&resumed[..header_end(&resumed)]);
assert!(header.contains("Content-Range: bytes 2-5/6")); 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");
server.join().unwrap(); server.join().unwrap();
fs::remove_file(path).unwrap(); fs::remove_file(path).unwrap();