2 Commits

Author SHA1 Message Date
zhy a960ad91d8 合并 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
2026-08-11 14:27:30 +08:00
zhy 20ac4dc6c7 发布 0.4.7 双向并发传输封版 2026-08-11 13:49:27 +08:00
18 changed files with 407 additions and 74 deletions
+13 -2
View File
@@ -1,4 +1,4 @@
# QFT v0.4.6 implementation status # QFT v0.4.7 implementation status
## Windows → Android optical ## Windows → Android optical
@@ -44,13 +44,24 @@
- [x] local cleartext network security config - [x] local cleartext network security config
- [x] local/private IPv4 + session-token validation - [x] local/private IPv4 + session-token validation
- [x] Parallel v2 init/chunk/finish - [x] Parallel v2 init/chunk/finish
- [x] adaptive 4/8/16 MiB chunks - [x] adaptive 64 KiB–16 MiB chunks, targeting eight flows per file
- [x] up to 8 Android workers per file - [x] up to 8 Android workers per file
- [x] Windows positioned concurrent writes - [x] Windows positioned concurrent writes
- [x] temporary `.qftpart`, final SHA-256 and rename - [x] temporary `.qftpart`, final SHA-256 and rename
- [x] accepted sockets return to timeout-bound blocking I/O before file transfer
- [x] loopback UDP + HTTP + out-of-order parallel integration test - [x] loopback UDP + HTTP + out-of-order parallel integration test
- [ ] physical Windows Wi-Fi Direct adapter / driver / firewall matrix - [ ] physical Windows Wi-Fi Direct adapter / driver / firewall matrix
## Windows → Android local direct
- [x] direction-specific pairing QR starts automatic local download after scan
- [x] HTTP `Range` responses return the exact requested range (`206` + `Content-Range`)
- [x] Android parallel Range downloader targets eight workers per file
- [x] adaptive 64 KiB–16 MiB chunks
- [x] `.qftpart` completion ledger resumes only verified completed ranges
- [x] five retries per range, final SHA-256, then save to `Downloads/QFT`
- [x] regression test for delayed large download after nonblocking accept
## Android compatibility ## Android compatibility
- [x] minSdk 24 (Android 7.0) - [x] minSdk 24 (Android 7.0)
+17 -9
View File
@@ -1,13 +1,20 @@
# QFT · 超速二维码文件传输 v0.4.6 # QFT · 超速二维码文件传输 v0.4.7
Windows + Android 双向候选版。v0.4 的核心目标不是继续堆“4 码理论吞吐”,而是解决 Android 端实际识别慢的问题:**删除 2×2/4 通道,改成一个大二维码 + 动态 ROI 锁定 + 高频局部解码**。 Windows + Android 双向候选版。v0.4 的核心目标不是继续堆“4 码理论吞吐”,而是解决 Android 端实际识别慢的问题:**删除 2×2/4 通道,改成一个大二维码 + 动态 ROI 锁定 + 高频局部解码**。
两个方向仍然采用不同链路: v0.4.7 提供两套互不依赖的传输方式:
- **Windows → Android:纯光学**。文件数据只经过 Windows 屏幕 → Android 摄像头;Wi‑Fi、蓝牙、局域网和互联网都不参与文件数据传输。 - **电脑 → 手机(纯光学)**:文件数据只经过 Windows 屏幕 → Android 摄像头;Wi‑Fi、蓝牙、局域网和互联网都不参与文件数据传输。
- **Android → Windows:扫码本地直连**。Windows 选择保存目录并启动 QFT Direct/LAN 接收服务;Android 扫码、配网、自动发现电脑,然后并行分块上传,不经过外网。 - **扫码本地直传(双向)**:Windows 为“手机上传”和“电脑下发”分别生成方向明确的二维码。Android 扫码后连接 QFT 本地网络、通过 UDP/网关/子网探测定位电脑,并按二维码指定方向自动进入选文件上传或接收下载;文件数据不经过外网。
> v0.4.6 已完成协议、图像合成解码、ROI、并行上传、UDP 发现、内存预算和关键 Kotlin 源码测试。当前生成环境没有真实 Windows 显示器、Android 相机或 Wi‑Fi Direct 适配器,因此物理 30/45/60 FPS 和真实扫码配网仍需在你的设备上跑 `VERIFY_WINDOWS_HOST.ps1` 后做最终验收;详见 `TEST_REPORT.md`。 ### v0.4.7 直传稳定性与并发策略
- **双向均使用并行分块**:每个可分块文件以 8 路为目标并发数;分块大小根据文件大小自适应,范围为 **64 KiB–16 MiB**。小于一个最小分块的文件会自然只有一个请求。
- **手机 → 电脑**:Android 使用 offset 定位的 Parallel v2 上传;Windows 对每块进行定位写入,重复块与完成请求可安全重试,最终以 SHA-256 校验后原子改名。
- **电脑 → 手机**:Windows 支持精确的 HTTP `Range`(`206 Partial Content`);Android 对未完成块创建多个独立 Range 请求,并用 `.qftpart` 与完成位图恢复中断传输。每块最多重试 5 次,完成后校验 SHA-256 再保存至 `Downloads/QFT`。
- **连接稳定性**:Windows 监听器保持非阻塞以便及时停止服务,但已连接的文件套接字会切换为带读写超时的阻塞模式,避免 Windows `WSAEWOULDBLOCK (10035)` 造成大文件中途失败。
> v0.4.7 已完成协议、图像合成解码、ROI、双向并发传输、UDP 发现、内存预算和关键 Kotlin 源码测试。当前生成环境没有真实 Windows 显示器、Android 相机或 Wi‑Fi Direct 适配器,因此物理 30/45/60 FPS 和真实扫码配网仍需在你的设备上跑 `VERIFY_WINDOWS_HOST.ps1` 后做最终验收;详见 `TEST_REPORT.md`。
## v0.4 光学链路改动 ## v0.4 光学链路改动
@@ -93,7 +100,7 @@ Windows 光传页面已经改成“二维码优先”:
推荐初次真机测试从 **30 FPS** 开始,看 Android 的“新帧/s”,再切 45、60。目标不是 Windows 数字最大,而是 Android **实际新帧/s 最大**。 推荐初次真机测试从 **30 FPS** 开始,看 Android 的“新帧/s”,再切 45、60。目标不是 Windows 数字最大,而是 Android **实际新帧/s 最大**。
## Android → Windows ## 扫码本地直传(Android ↔ Windows)
v0.3 的稳定性优化继续保留: v0.3 的稳定性优化继续保留:
@@ -104,9 +111,10 @@ v0.3 的稳定性优化继续保留:
- gateway / endpoint / 当前子网 fallback; - gateway / endpoint / 当前子网 fallback;
- HTTP 请求明确绑定扫码建立的 `Network`; - HTTP 请求明确绑定扫码建立的 `Network`;
- 配网请求在传输页持续持有,仅返回首页时释放;短暂网络波动会自动接入新的 `Network`; - 配网请求在传输页持续持有,仅返回首页时释放;短暂网络波动会自动接入新的 `Network`;
- 上传与下载单文件均最高 8 worker 并行; - 手机上传和电脑下发均以 8 路为目标并发数;
- 4/8/16 MiB 自适应上传块; - 64 KiB–16 MiB 自适应分块;
- 16 MiB 有界 Range 并行下载,完成位图支持掉线/重启后只补缺块; - 电脑下发使用精确 Range 响应,手机以 `.qftpart` + 位图断点续传;
- 手机上传使用定位写入和幂等完成确认;
- Windows positioned writes + `.qftpart` + SHA-256 + 最终 rename。 - Windows positioned writes + `.qftpart` + SHA-256 + 最终 rename。
## Android 版本 ## Android 版本
+5 -5
View File
@@ -1,4 +1,4 @@
# QFT v0.4.6 test report # QFT v0.4.7 test report
Date: 2026-08-10 Date: 2026-08-10
@@ -6,7 +6,7 @@ This report separates **tests actually executed in the current environment** fro
## What this RC changes ## What this RC changes
The v0.4.6 optical path is deliberately simpler than v0.3: The v0.4.7 optical path is deliberately simpler than v0.3:
- 2×2 / 4-channel QR removed; - 2×2 / 4-channel QR removed;
- one large QR only; - one large QR only;
@@ -152,7 +152,7 @@ The guard models the Windows single-QR 1024×1024 RGBA pre-render queue:
This is a design-budget check, not an OS-enforced RSS hard cap. GPU/runtime allocators can add platform-specific memory. This is a design-budget check, not an OS-enforced RSS hard cap. GPU/runtime allocators can add platform-specific memory.
## Important implementation decisions validated during v0.4.6 ## Important implementation decisions validated during v0.4.7
### QFT2 uses Result.text, not ZXing rawBytes ### QFT2 uses Result.text, not ZXing rawBytes
@@ -160,7 +160,7 @@ The active path is Base45 text. Android reconstructs the QFT binary frame from `
### Duplicate camera hits are not duplicate assembler work ### Duplicate camera hits are not duplicate assembler work
At 45/60 display FPS, a camera can observe the same displayed QR in multiple exposures. v0.4.6 still counts every successful decode in diagnostics, but only a changed QFT2 text fingerprint is delivered to `SessionAssembler`. At 45/60 display FPS, a camera can observe the same displayed QR in multiple exposures. v0.4.7 still counts every successful decode in diagnostics, but only a changed QFT2 text fingerprint is delivered to `SessionAssembler`.
### AF/AE does not run on a fixed periodic timer ### AF/AE does not run on a fixed periodic timer
@@ -170,7 +170,7 @@ The scanner performs initial focus/metering, then re-meters after first lock or
The current generation environment does not contain the Windows MSVC Rust toolchain, Android SDK, a physical Android camera, a Windows display or a Windows Wi-Fi Direct adapter. Therefore the following are **not claimed as passed here**: The current generation environment does not contain the Windows MSVC Rust toolchain, Android SDK, a physical Android camera, a Windows display or a Windows Wi-Fi Direct adapter. Therefore the following are **not claimed as passed here**:
1. Windows `cargo check/test/build --release` for v0.4.6. 1. Windows `cargo check/test/build --release` for v0.4.7.
2. Android Gradle `:app:testDebugUnitTest :app:assembleDebug` against the real Android SDK. 2. Android Gradle `:app:testDebugUnitTest :app:assembleDebug` against the real Android SDK.
3. Actual 30/45/60 displayed FPS → CameraX/ZXing effective new-frame throughput. 3. Actual 30/45/60 displayed FPS → CameraX/ZXing effective new-frame throughput.
4. Lock acquisition/reacquisition timing on Android 7 and different phone camera pipelines. 4. Lock acquisition/reacquisition timing on Android 7 and different phone camera pipelines.
+1 -1
View File
@@ -1,4 +1,4 @@
QFT v0.4.6 test results QFT v0.4.7 test results
UTC 2026-08-10T08:45:56Z UTC 2026-08-10T08:45:56Z
== protocol_selftest.py == == protocol_selftest.py ==
+1 -1
View File
@@ -1,7 +1,7 @@
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path $Root = Split-Path -Parent $MyInvocation.MyCommand.Path
Write-Host "== QFT v0.4.6 Windows host verification ==" -ForegroundColor Cyan Write-Host "== QFT v0.4.7 Windows host verification ==" -ForegroundColor Cyan
if (Get-Command python -ErrorAction SilentlyContinue) { if (Get-Command python -ErrorAction SilentlyContinue) {
Write-Host "[1/4] Core protocol/selftests" Write-Host "[1/4] Core protocol/selftests"
+2 -2
View File
@@ -12,8 +12,8 @@ android {
applicationId = "com.qft.optical" applicationId = "com.qft.optical"
minSdk = 24 minSdk = 24
targetSdk = 35 targetSdk = 35
versionCode = 18 versionCode = 19
versionName = "0.4.6" versionName = "0.4.7"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }
@@ -16,6 +16,7 @@ import java.io.OutputStream
import java.io.RandomAccessFile import java.io.RandomAccessFile
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
import java.nio.ByteBuffer
import java.security.MessageDigest import java.security.MessageDigest
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
@@ -29,16 +30,108 @@ 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 SERIAL_ATTEMPTS = 3 private const val SERIAL_ATTEMPTS = 3
private const val RANGE_ATTEMPTS = 10 // Kept only for the old sequential compatibility path below. New direct
// sessions always use the concurrent Range downloader.
private const val PARALLEL_MIN_FILE_SIZE = 32L * 1024L * 1024L private const val PARALLEL_MIN_FILE_SIZE = 32L * 1024L * 1024L
private const val PARALLEL_CHUNK_SIZE = 16L * 1024L * 1024L private const val PARALLEL_CHUNK_SIZE = 16L * 1024L * 1024L
private const val MAX_WORKERS = 8 private const val MAX_WORKERS = 8
private const val RANGE_WORKERS = 8
private const val RANGE_ATTEMPTS = 5
private const val MIN_RANGE_CHUNK = 64L * 1024L
private const val MAX_RANGE_CHUNK = 16L * 1024L * 1024L
/** 整份重下的轮数。仅在 SHA-256 不匹配(缓存已损坏)时才会用到第二轮。 */ /** 整份重下的轮数。仅在 SHA-256 不匹配(缓存已损坏)时才会用到第二轮。 */
private const val VERIFY_ROUNDS = 2 private const val VERIFY_ROUNDS = 2
/** 落盘阶段的失败与「下载失败」是两回事,用独立异常类型区分。 */ /** 落盘阶段的失败与「下载失败」是两回事,用独立异常类型区分。 */
class SaveFailure(message: String, cause: Throwable?) : Exception(message, cause) class SaveFailure(message: String, cause: Throwable?) : Exception(message, cause)
/** Persistent received-range map. The data file is preallocated, so its
* length is not used as a completion signal; only an atomically replaced
* map entry marks a range safe to resume. */
private class ChunkLedger private constructor(
private val partial: File,
private val map: File,
private val size: Long,
val chunkSize: Long,
val totalChunks: Int,
private val completed: BooleanArray,
) {
private val lock = Any()
fun isComplete(index: Int): Boolean = synchronized(lock) { completed[index] }
fun allComplete(): Boolean = synchronized(lock) { completed.all { it } }
fun bytesComplete(): Long = synchronized(lock) {
completed.indices.sumOf { index -> if (completed[index]) chunkLength(index) else 0L }
}
fun chunkOffset(index: Int): Long = index.toLong() * chunkSize
fun chunkLength(index: Int): Long = minOf(chunkSize, size - chunkOffset(index))
fun markComplete(index: Int) = synchronized(lock) {
if (!completed[index]) {
completed[index] = true
persistLocked()
}
}
fun discard() = synchronized(lock) {
partial.delete()
map.delete()
File("${map.path}.tmp").delete()
}
private fun persistLocked() {
val temp = File("${map.path}.tmp")
temp.bufferedWriter().use { writer ->
writer.appendLine("$size:$chunkSize:$totalChunks")
writer.write(buildString(totalChunks) {
completed.forEach { append(if (it) '1' else '0') }
})
}
if (map.exists()) map.delete()
if (!temp.renameTo(map)) {
temp.copyTo(map, overwrite = true)
temp.delete()
}
}
companion object {
fun open(partial: File, size: Long, chunkSize: Long): ChunkLedger {
val totalLong = if (size == 0L) 0L else (size + chunkSize - 1L) / chunkSize
require(totalLong <= Int.MAX_VALUE) { "文件分块数过大" }
val total = totalLong.toInt()
val map = File("${partial.path}.map")
val restored = runCatching {
val pieces = map.readText().split('\n', limit = 2)
val header = pieces.firstOrNull()?.split(':') ?: emptyList()
val bits = pieces.getOrNull(1).orEmpty()
if (
partial.length() == size && header.size == 3 &&
header[0].toLong() == size && header[1].toLong() == chunkSize &&
header[2].toInt() == total && bits.length >= total
) {
BooleanArray(total) { bits[it] == '1' }
} else {
null
}
}.getOrNull()
val ledger = ChunkLedger(partial, map, size, chunkSize, total, restored ?: BooleanArray(total))
if (restored == null) {
partial.parentFile?.let { if (!it.exists()) it.mkdirs() }
RandomAccessFile(partial, "rw").use { file ->
file.setLength(0L)
file.setLength(size)
}
ledger.persistLocked()
}
return ledger
}
}
}
fun download( fun download(
context: Context, context: Context,
resolver: ContentResolver, resolver: ContentResolver,
@@ -59,10 +152,7 @@ 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) { if (partial.length() > expectedSize) discardTransferState(partial)
partial.delete()
completionMapFile(partial).delete()
}
// 落盘时缓存文件与目标文件同时存在,需要约 2 倍空间。 // 落盘时缓存文件与目标文件同时存在,需要约 2 倍空间。
// 不预检的话,这里会在下载 100% 之后才失败,用户看到的是「快结束时失败」。 // 不预检的话,这里会在下载 100% 之后才失败,用户看到的是「快结束时失败」。
@@ -83,15 +173,13 @@ object DownloadClient {
// 这一步失败与网络无关,重下多少次都没用,直接如实报错 // 这一步失败与网络无关,重下多少次都没用,直接如实报错
throw SaveFailure("文件已完整收到,但保存到 Downloads/QFT 失败:${failureDetail(e)}", e) throw SaveFailure("文件已完整收到,但保存到 Downloads/QFT 失败:${failureDetail(e)}", e)
} }
partial.delete() discardTransferState(partial)
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() discardTransferState(partial)
completionMapFile(partial).delete()
if (round + 1 >= VERIFY_ROUNDS) { if (round + 1 >= VERIFY_ROUNDS) {
error("文件校验失败:重新完整下载后 SHA-256 仍不匹配,请让电脑重新生成二维码") error("文件校验失败:重新完整下载后 SHA-256 仍不匹配,请让电脑重新生成二维码")
} }
@@ -101,7 +189,8 @@ object DownloadClient {
} }
/** 传输阶段:断点续传 + 有限重试。只有这一段的失败才算「下载失败」。 */ /** 传输阶段:断点续传 + 有限重试。只有这一段的失败才算「下载失败」。 */
private fun receiveUntilComplete( @Suppress("unused")
private fun receiveSequentialCompatibility(
networkProvider: () -> Network?, networkProvider: () -> Network?,
baseUrl: String, baseUrl: String,
pairing: PairingPayload, pairing: PairingPayload,
@@ -446,6 +535,185 @@ object DownloadClient {
} }
/** 校验也要报进度:大文件读一整遍是好几秒,静默的话看起来就是卡在 100%。 */ /** 校验也要报进度:大文件读一整遍是好几秒,静默的话看起来就是卡在 100%。 */
private fun receiveUntilComplete(
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
partial: File,
expectedSize: Long,
abortReason: () -> String?,
onProgress: (received: Long, total: Long) -> Unit,
) {
val ledger = ChunkLedger.open(partial, expectedSize, desiredRangeChunkSize(expectedSize))
val initial = ledger.bytesComplete()
onProgress(initial, expectedSize)
if (ledger.allComplete()) return
val workers = ledger.totalChunks.coerceAtMost(RANGE_WORKERS).coerceAtLeast(1)
Log.i(TAG, "parallel start: chunks=${ledger.totalChunks}, workers=$workers, completed=$initial/$expectedSize")
val pool = Executors.newFixedThreadPool(workers)
val next = AtomicInteger(0)
val futures = (0 until workers).map {
pool.submit {
while (!Thread.currentThread().isInterrupted) {
abortReason()?.let { error(it) }
val index = next.getAndIncrement()
if (index >= ledger.totalChunks) break
if (ledger.isComplete(index)) continue
receiveRangeWithRetry(
networkProvider,
baseUrl,
pairing,
deviceName,
partial,
expectedSize,
ledger,
index,
abortReason,
)
onProgress(ledger.bytesComplete(), expectedSize)
}
}
}
try {
futures.forEach { future -> future.get() }
} catch (e: Exception) {
futures.forEach { it.cancel(true) }
throw (e.cause ?: e)
} finally {
pool.shutdownNow()
}
if (!ledger.allComplete()) {
error("并行下载未完成:${ledger.bytesComplete()}/$expectedSize")
}
Log.i(TAG, "parallel complete: chunks=${ledger.totalChunks}, bytes=$expectedSize")
}
private fun receiveRangeWithRetry(
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
partial: File,
expectedSize: Long,
ledger: ChunkLedger,
index: Int,
abortReason: () -> String?,
) {
var last: Exception? = null
repeat(RANGE_ATTEMPTS) { attempt ->
abortReason()?.let { throw IllegalStateException(it, last) }
try {
receiveRange(
networkProvider(),
baseUrl,
pairing,
deviceName,
partial,
expectedSize,
ledger,
index,
)
ledger.markComplete(index)
return
} catch (e: Exception) {
last = e
Log.w(
TAG,
"range=$index/${ledger.totalChunks} attempt=${attempt + 1}/$RANGE_ATTEMPTS failed: ${failureDetail(e)}",
e,
)
if (attempt + 1 < RANGE_ATTEMPTS) {
try {
Thread.sleep(180L * (attempt + 1))
} catch (interrupted: InterruptedException) {
Thread.currentThread().interrupt()
throw e
}
}
}
}
throw IllegalStateException(
"分块 $index 下载失败,已重试 $RANGE_ATTEMPTS 次:${failureDetail(last)}",
last,
)
}
private fun receiveRange(
network: Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
partial: File,
expectedSize: Long,
ledger: ChunkLedger,
index: Int,
) {
val offset = ledger.chunkOffset(index)
val length = ledger.chunkLength(index)
val end = offset + length - 1L
val conn = open(network, URL("$baseUrl/qft/download"))
try {
conn.requestMethod = "GET"
conn.connectTimeout = 5_000
conn.readTimeout = 20_000
conn.useCaches = false
conn.setRequestProperty("Connection", "close")
conn.setRequestProperty("X-QFT-Token", pairing.session)
conn.setRequestProperty("X-QFT-Device", Base64Url.encode(deviceName.toByteArray(Charsets.UTF_8)))
conn.setRequestProperty("Range", "bytes=$offset-$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 expectedRange = "bytes $offset-$end/$expectedSize"
if (conn.getHeaderField("Content-Range") != expectedRange) {
error("下载服务返回了无效范围:${conn.getHeaderField("Content-Range")}")
}
if (conn.contentLengthLong >= 0L && conn.contentLengthLong != length) {
error("分块 $index 长度不匹配:${conn.contentLengthLong} != $length")
}
var received = 0L
conn.inputStream.use { input ->
RandomAccessFile(partial, "rw").use { output ->
output.channel.use { channel ->
channel.position(offset)
val buffer = ByteArray(BUFFER_SIZE)
while (received < length) {
val n = input.read(buffer, 0, minOf(buffer.size.toLong(), length - received).toInt())
if (n < 0) break
if (n == 0) continue
val bytes = ByteBuffer.wrap(buffer, 0, n)
while (bytes.hasRemaining()) channel.write(bytes)
received += n.toLong()
}
channel.force(false)
}
}
}
if (received != length) error("分块 $index 提前结束:$received/$length")
Log.d(TAG, "range=$index/${ledger.totalChunks} complete bytes=$length")
} finally {
conn.disconnect()
}
}
private fun desiredRangeChunkSize(fileSize: Long): Long {
if (fileSize <= 0L) return MIN_RANGE_CHUNK
return ((fileSize + RANGE_WORKERS - 1L) / RANGE_WORKERS)
.coerceIn(MIN_RANGE_CHUNK, MAX_RANGE_CHUNK)
}
private fun discardTransferState(partial: File) {
partial.delete()
File("${partial.path}.map").delete()
File("${partial.path}.map.tmp").delete()
}
private fun sha256(file: File, onProgress: (Long, Long) -> Unit): String { private fun sha256(file: File, onProgress: (Long, Long) -> Unit): String {
val digest = MessageDigest.getInstance("SHA-256") val digest = MessageDigest.getInstance("SHA-256")
val total = file.length() val total = file.length()
@@ -194,7 +194,7 @@ class MainActivity : ComponentActivity() {
) { showReceivedFiles() }) ) { showReceivedFiles() })
root.addView(space(22)) root.addView(space(22))
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED)) root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
root.addView(text("QFT v0.4.6 · Android 7+ 到最新", 12f, MUTED)) root.addView(text("QFT v0.4.7 · Android 7+ 到最新", 12f, MUTED))
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) }) setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
} }
@@ -734,7 +734,7 @@ class MainActivity : ComponentActivity() {
root.addView(card().apply { root.addView(card().apply {
addView(text("连接状态", 14f, MUTED)) addView(text("连接状态", 14f, MUTED))
addView(text("● 本地直连已就绪", 17f, SUCCESS, true), matchWrap(top = 8)) addView(text("● 本地直连已就绪", 17f, SUCCESS, true), matchWrap(top = 8))
addView(text(if (pair.version >= 2) "Parallel v2 · 自适应最高 8 路并发 · 4/8/16 MiB 自适应分块" else "兼容单流模式", 13f, ACCENT), matchWrap(top = 5)) addView(text(if (pair.version >= 2) "Parallel v2 · 目标 8 路并发 · 64 KiB–16 MiB 自适应分块" else "兼容单流模式", 13f, ACCENT), matchWrap(top = 5))
addView(text("目标:$baseUrl", 12f, MUTED), matchWrap(top = 6)) addView(text("目标:$baseUrl", 12f, MUTED), matchWrap(top = 6))
pair.wifi?.let { addView(text("QFT 临时网络:${it.ssid}", 12f, MUTED), matchWrap(top = 4)) } pair.wifi?.let { addView(text("QFT 临时网络:${it.ssid}", 12f, MUTED), matchWrap(top = 4)) }
}) })
@@ -33,14 +33,10 @@ data class SelectedFile(
object UploadClient { object UploadClient {
private const val TAG = "QftUpload" private const val TAG = "QftUpload"
private const val DISCOVERY_PORT = 39392 private const val DISCOVERY_PORT = 39392
private const val CHUNK_SMALL = 4L * 1024L * 1024L
private const val CHUNK_DEFAULT = 8L * 1024L * 1024L
private const val CHUNK_LARGE = 16L * 1024L * 1024L
private const val MAX_WORKERS = 8 private const val MAX_WORKERS = 8
private const val MIN_PARALLEL_CHUNK = 64L * 1024L
private const val MAX_PARALLEL_CHUNK = 16L * 1024L * 1024L
private const val STREAM_BUFFER = 1024 * 1024 private const val STREAM_BUFFER = 1024 * 1024
// A single request is materially more reliable for small files on Wi-Fi
// Direct, and a 4 MiB parallel chunk gives no throughput benefit there.
private const val PARALLEL_MIN_FILE_SIZE = 32L * 1024L * 1024L
// 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.
@@ -230,10 +226,7 @@ object UploadClient {
abortReason: () -> String? = { null }, 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) { if (pairing.version >= 2 && isSeekable(resolver, file)) {
require(isSeekable(resolver, file)) {
"大文件必须使用可随机读取的本地文件才能并行直传;请先把云盘文件下载到手机本地后再选择"
}
try { try {
uploadParallel(resolver, networkProvider, baseUrl, pairing, deviceName, file, abortReason, onProgress) uploadParallel(resolver, networkProvider, baseUrl, pairing, deviceName, file, abortReason, onProgress)
return return
@@ -247,7 +240,7 @@ object UploadClient {
if (!message.contains("HTTP 404") && !message.contains("HTTP 501")) throw e if (!message.contains("HTTP 404") && !message.contains("HTTP 501")) throw e
} }
} }
Log.i(TAG, "using single-stream upload: size=${file.size}, endpoint=$baseUrl") Log.i(TAG, "using compatibility single-stream upload: size=${file.size}, endpoint=$baseUrl")
uploadLegacy(resolver, networkProvider(), baseUrl, pairing, deviceName, file, abortReason, onProgress) uploadLegacy(resolver, networkProvider(), baseUrl, pairing, deviceName, file, abortReason, onProgress)
} }
@@ -374,10 +367,13 @@ object UploadClient {
} }
private fun desiredChunkSize(fileSize: Long): Long = when { private fun desiredChunkSize(fileSize: Long): Long {
fileSize < 32L * 1024L * 1024L -> CHUNK_SMALL if (fileSize <= 0L) return MIN_PARALLEL_CHUNK
fileSize >= 512L * 1024L * 1024L -> CHUNK_LARGE // Aim for eight independent HTTP flows on every transferable file.
else -> CHUNK_DEFAULT // The floor keeps QR Direct overhead reasonable for tiny files, while
// the ceiling protects memory and TCP fairness on very large files.
return ((fileSize + MAX_WORKERS - 1) / MAX_WORKERS)
.coerceIn(MIN_PARALLEL_CHUNK, MAX_PARALLEL_CHUNK)
} }
private fun initUpload( private fun initUpload(
+8 -11
View File
@@ -1,4 +1,4 @@
# QFT protocol notes — v0.4.6 # QFT protocol notes — v0.4.7
## 1. Windows → Android optical: QFT2 ## 1. Windows → Android optical: QFT2
@@ -36,7 +36,7 @@ senderLen 2 u16 BE
senderDevice N UTF-8 senderDevice N UTF-8
``` ```
### Optical parameters in v0.4.6 ### Optical parameters in v0.4.7
- one QR per display frame; - one QR per display frame;
- DATA payload: **450 B**; - DATA payload: **450 B**;
@@ -75,7 +75,7 @@ The overlay is derived from the same ROI used by the decoder, so the green/yello
Discovery order includes QR hint, UDP `QFTDISC2`, gateway/direct candidates and bounded current-subnet fallback. Discovery order includes QR hint, UDP `QFTDISC2`, gateway/direct candidates and bounded current-subnet fallback.
## 4. Android → Windows upload ## 4. 扫码本地直传(Android ↔ Windows)
Parallel v2 flow: Parallel v2 flow:
@@ -85,19 +85,16 @@ POST /qft/upload/chunk (parallel, offset based)
POST /qft/upload/finish 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. Uploads target 8 workers per file (bounded by the usable chunks and device capacity); the requested chunk size is derived from the file size and clamped to **64 KiB–16 MiB**. Repeated `init` requests carrying the same `X-QFT-Upload-Key` resume the matching active upload session; repeated chunk and finish requests are idempotent so a lost response can be retried safely. Chunk completion is recorded only after the positioned write reaches stable storage.
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 download
## 5. Windows → Android direct download The same QFTPAIR2 local session can declare the download direction. Android discovers and verifies the Windows sender, then downloads `GET /qft/download` with exact single-range requests:
Files of at least 32 MiB use up to 8 parallel bounded range requests:
```text ```text
GET /qft/download
Range: bytes=<start>-<end> 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 sender returns `206 Partial Content`, the matching `Content-Range`, and the exact requested body length. Android splits the file toward 8 workers with **64 KiB–16 MiB** chunks, stores each completed chunk in a `.qftpart.map` ledger, and resumes only missing chunks after a reconnect. Each range retries with the currently available `Network`; the complete partial file must pass SHA-256 before it is published 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. For both direct-transfer directions, the Windows listener may accept connections non-blockingly for shutdown responsiveness, but each accepted file socket is switched to blocking I/O with explicit timeouts before reading or writing request bodies. This prevents `WSAEWOULDBLOCK (10035)` from turning an in-progress parallel transfer into an HTTP 500 failure.
+4 -4
View File
@@ -194,7 +194,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 46; CURRENT_PROJECT_VERSION = 47;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = NO; GENERATE_INFOPLIST_FILE = NO;
INFOPLIST_FILE = QFT/Info.plist; INFOPLIST_FILE = QFT/Info.plist;
@@ -203,7 +203,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
MARKETING_VERSION = 0.4.6; MARKETING_VERSION = 0.4.7;
PRODUCT_BUNDLE_IDENTIFIER = com.qft.transfer; PRODUCT_BUNDLE_IDENTIFIER = com.qft.transfer;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO; SWIFT_EMIT_LOC_STRINGS = NO;
@@ -272,7 +272,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 46; CURRENT_PROJECT_VERSION = 47;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = NO; GENERATE_INFOPLIST_FILE = NO;
INFOPLIST_FILE = QFT/Info.plist; INFOPLIST_FILE = QFT/Info.plist;
@@ -281,7 +281,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
MARKETING_VERSION = 0.4.6; MARKETING_VERSION = 0.4.7;
PRODUCT_BUNDLE_IDENTIFIER = com.qft.transfer; PRODUCT_BUNDLE_IDENTIFIER = com.qft.transfer;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO; SWIFT_EMIT_LOC_STRINGS = NO;
+1 -1
View File
@@ -1,4 +1,4 @@
# QFT iOS 客户端 v0.4.6 # QFT iOS 客户端 v0.4.7
三种传输方式: 三种传输方式:
+2 -2
View File
@@ -19,7 +19,7 @@ IOS_ROOT = os.path.dirname(HERE)
APP_NAME = "QFT" APP_NAME = "QFT"
BUNDLE_ID = "com.qft.transfer" BUNDLE_ID = "com.qft.transfer"
DEPLOYMENT_TARGET = "17.0" DEPLOYMENT_TARGET = "17.0"
MARKETING_VERSION = "0.4.6" MARKETING_VERSION = "0.4.7"
SOURCES = [ SOURCES = [
"QFT/App/QFTApp.swift", "QFT/App/QFTApp.swift",
@@ -280,7 +280,7 @@ def generate():
"ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon", "ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon",
"ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor", "ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor",
"CODE_SIGN_STYLE = Automatic", "CODE_SIGN_STYLE = Automatic",
"CURRENT_PROJECT_VERSION = 46", "CURRENT_PROJECT_VERSION = 47",
"ENABLE_PREVIEWS = YES", "ENABLE_PREVIEWS = YES",
"GENERATE_INFOPLIST_FILE = NO", "GENERATE_INFOPLIST_FILE = NO",
"INFOPLIST_FILE = QFT/Info.plist", "INFOPLIST_FILE = QFT/Info.plist",
+1 -1
View File
@@ -2587,7 +2587,7 @@ checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
[[package]] [[package]]
name = "qft-windows" name = "qft-windows"
version = "0.4.6" version = "0.4.7"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64", "base64",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "qft-windows" name = "qft-windows"
version = "0.4.6" version = "0.4.7"
edition = "2024" edition = "2024"
rust-version = "1.95" rust-version = "1.95"
+2 -2
View File
@@ -23,7 +23,7 @@ use std::{
// a nominally larger page that is only decoded 10~18 times per second. // a nominally larger page that is only decoded 10~18 times per second.
const DEFAULT_CHUNK_SIZE: u16 = 320; const DEFAULT_CHUNK_SIZE: u16 = 320;
const UI_FONT_NAME: &str = "qft-ui"; 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 { fn main() -> eframe::Result {
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
@@ -446,7 +446,7 @@ impl QftApp {
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {
ui.heading("Android → 电脑 · 扫码直连"); ui.heading("Android → 电脑 · 扫码直连");
ui.separator(); ui.separator();
ui.label("UDP 快速发现 · 4/8/16 MiB 自适应分块 · 最高 8 路并行 · 不经过外网"); ui.label("UDP 快速发现 · 64 KiB–16 MiB 自适应分块 · 目标 8 路并行 · 不经过外网");
}); });
ui.add_space(10.0); ui.add_space(10.0);
let running = self.receive.server.is_some(); 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 PREFERRED_PORT: u16 = 39393;
pub const DISCOVERY_PORT: u16 = 39392; pub const DISCOVERY_PORT: u16 = 39392;
pub const PARALLEL_CHUNK_SIZE: u64 = 8 * 1024 * 1024; 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; const IO_BUFFER: usize = 1024 * 1024;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@@ -250,7 +252,7 @@ fn init_parallel_upload(
.parse().context("X-QFT-Size 无效")?; .parse().context("X-QFT-Size 无效")?;
let chunk_size = headers.get("x-qft-chunk-size") let chunk_size = headers.get("x-qft-chunk-size")
.and_then(|v| v.parse::<u64>().ok()) .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); .unwrap_or(PARALLEL_CHUNK_SIZE);
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!("文件分块数过大") }
+55 -4
View File
@@ -128,6 +128,11 @@ fn handle_client(
token: &str, token: &str,
events: &Sender<SenderEvent>, events: &Sender<SenderEvent>,
) -> Result<()> { ) -> 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_nodelay(true).ok();
stream.set_read_timeout(Some(Duration::from_secs(20)))?; stream.set_read_timeout(Some(Duration::from_secs(20)))?;
stream.set_write_timeout(Some(Duration::from_secs(120)))?; stream.set_write_timeout(Some(Duration::from_secs(120)))?;
@@ -192,11 +197,10 @@ fn handle_client(
while left > 0 { while left > 0 {
let want = left.min(buf.len() as u64) as usize; let want = left.min(buf.len() as u64) as usize;
let n = input.read(&mut buf[..want])?; let n = input.read(&mut buf[..want])?;
if n == 0 { break } if n == 0 { bail!("发送文件读取提前结束:还缺 {left} 字节") }
stream.write_all(&buf[..n])?; stream.write_all(&buf[..n])?;
left -= n as u64; 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(())
@@ -249,6 +253,7 @@ mod tests {
net::{TcpListener, TcpStream}, net::{TcpListener, TcpStream},
sync::mpsc, sync::mpsc,
thread, thread,
time::Duration,
}; };
#[test] #[test]
@@ -284,13 +289,59 @@ 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")); let bounded = request(address, Some("bytes=2-4"));
assert_response(&bounded, "HTTP/1.1 206 Partial Content", "bcd"); 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(); server.join().unwrap();
fs::remove_file(path).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> { fn request(address: std::net::SocketAddr, range: Option<&str>) -> Vec<u8> {
let mut stream = TcpStream::connect(address).unwrap(); let mut stream = TcpStream::connect(address).unwrap();
write!( write!(