优化大文件稳定并发传输与打包流程

This commit is contained in:
zhy200308
2026-08-11 11:27:40 +08:00
parent e326611c82
commit a4edf734b6
13 changed files with 715 additions and 101 deletions
@@ -13,9 +13,13 @@ import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.OutputStream
import java.io.RandomAccessFile
import java.net.HttpURLConnection
import java.net.URL
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?)
@@ -24,7 +28,11 @@ data class DownloadedFile(val name: String, val size: Long, val uri: Uri?)
object DownloadClient {
private const val TAG = "QftDownload"
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 不匹配(缓存已损坏)时才会用到第二轮。 */
private const val VERIFY_ROUNDS = 2
@@ -34,7 +42,7 @@ object DownloadClient {
fun download(
context: Context,
resolver: ContentResolver,
network: Network?,
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
@@ -51,14 +59,17 @@ object DownloadClient {
val expectedSha = requireNotNull(pairing.sha256)
val partial = partialFile(context, pairing.session)
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 倍空间。
// 不预检的话,这里会在下载 100% 之后才失败,用户看到的是「快结束时失败」。
ensureSpaceForTransfer(partial, expectedSize)
repeat(VERIFY_ROUNDS) { round ->
receiveUntilComplete(network, baseUrl, pairing, deviceName, partial,
receiveUntilComplete(networkProvider, baseUrl, pairing, deviceName, partial,
expectedSize, abortReason, onProgress)
onPhase("正在校验文件完整性…")
@@ -73,12 +84,14 @@ object DownloadClient {
throw SaveFailure("文件已完整收到,但保存到 Downloads/QFT 失败:${failureDetail(e)}", e)
}
partial.delete()
completionMapFile(partial).delete()
Log.i(TAG, "completed size=$expectedSize")
return result
}
Log.w(TAG, "sha mismatch round=${round + 1}/$VERIFY_ROUNDS expected=$expectedSha actual=$actual")
partial.delete()
completionMapFile(partial).delete()
if (round + 1 >= VERIFY_ROUNDS) {
error("文件校验失败:重新完整下载后 SHA-256 仍不匹配,请让电脑重新生成二维码")
}
@@ -89,7 +102,7 @@ object DownloadClient {
/** 传输阶段:断点续传 + 有限重试。只有这一段的失败才算「下载失败」。 */
private fun receiveUntilComplete(
network: Network?,
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
@@ -98,13 +111,19 @@ object DownloadClient {
abortReason: () -> String?,
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
repeat(ATTEMPTS) { attempt ->
repeat(SERIAL_ATTEMPTS) { attempt ->
abortReason()?.let { throw IllegalStateException(it, last) }
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) {
receiveAttempt(network, baseUrl, pairing, deviceName, partial,
receiveAttempt(networkProvider(), baseUrl, pairing, deviceName, partial,
expectedSize, abortReason, onProgress)
}
if (partial.length() != expectedSize) {
@@ -113,9 +132,9 @@ object DownloadClient {
return
} catch (e: Exception) {
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) }
if (attempt + 1 < ATTEMPTS) {
if (attempt + 1 < SERIAL_ATTEMPTS) {
try {
Thread.sleep(250L * (attempt + 1))
} catch (interrupted: InterruptedException) {
@@ -127,11 +146,209 @@ object DownloadClient {
}
val progressed = partial.length()
throw IllegalStateException(
"从电脑下载失败,已重试 $ATTEMPTS 次(已收到 ${progressed}/$expectedSize 字节):${failureDetail(last)}",
"从电脑下载失败(断点 ${progressed}/$expectedSize 字节):${failureDetail(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) {
val root = partial.parentFile ?: return
@@ -164,13 +381,9 @@ object DownloadClient {
try {
conn.requestMethod = "GET"
conn.connectTimeout = 5_000
// A QFT LAN transfer should start immediately. The old 120-second
// 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.readTimeout = 60_000
conn.useCaches = false
conn.setRequestProperty("Connection", "close")
conn.setRequestProperty("Connection", "keep-alive")
conn.setRequestProperty("X-QFT-Token", pairing.session)
conn.setRequestProperty("X-QFT-Device", Base64Url.encode(deviceName.toByteArray(Charsets.UTF_8)))
if (requestedOffset > 0L) conn.setRequestProperty("Range", "bytes=$requestedOffset-")
@@ -208,8 +421,7 @@ object DownloadClient {
FileOutputStream(partial, offset > 0L).use { output ->
val buffer = ByteArray(BUFFER_SIZE)
while (true) {
// 网络已断开时立刻收手:否则会在 readTimeout 上白等 12 秒,
// 再乘以 5 次重试,用户等一分钟才看到错误。
// 用户明确返回首页时才中止;普通 TCP 波动由 Range 续传恢复。
abortReason()?.let { error(it) }
val n = input.read(buffer)
if (n < 0) break
@@ -76,7 +76,7 @@ class MainActivity : ComponentActivity() {
private lateinit var progress: ProgressBar
private var pendingPair: PairingPayload? = null
private var connectedNetwork: Network? = null
@Volatile private var connectedNetwork: Network? = null
private var connectedBaseUrl: String? = null
private val selectedFiles = mutableListOf<SelectedFile>()
private var managingFilesForPair: PairingPayload? = null
@@ -146,6 +146,7 @@ class MainActivity : ComponentActivity() {
}
private fun showHome(message: String? = null) {
transferAbortReason = "已返回首页,传输已停止"
pairConnectionAttempt++
stopCamera()
scannerMode = ScannerMode.NONE
@@ -602,6 +603,7 @@ class MainActivity : ComponentActivity() {
private fun connectPair(pair: PairingPayload) {
val attempt = ++pairConnectionAttempt
transferAbortReason = null
showConnecting(pair, pair.wifi?.let { "正在请求连接 ${it.ssid}…" } ?: "正在使用当前局域网探测电脑…")
wifiConnector.connect(
pairing = pair,
@@ -611,6 +613,10 @@ class MainActivity : ComponentActivity() {
onAvailable = available@{ network ->
if (attempt != pairConnectionAttempt) return@available
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 {
if (attempt != pairConnectionAttempt) return@execute
runOnUiThread { updateConnectingStatus("网络路由已就绪 · 正在 UDP 快速发现电脑…") }
@@ -657,14 +663,10 @@ class MainActivity : ComponentActivity() {
},
onUnavailable = { message -> runOnUiThread {
if (attempt != pairConnectionAttempt) return@runOnUiThread
// Huawei can report onLost while a bound local-only HTTP
// socket is unwinding. Let DownloadClient surface its precise
// HTTP/timeout cause instead of replacing it with this generic
// message mid-retry.
// onLost is recovered inside WifiConnector. onUnavailable is
// terminal (system/user rejected the retained request), so it
// is the only network callback that aborts an active transfer.
if (pair.direction == TransferDirection.DOWNLOAD_TO_PHONE && downloadRunning) {
// 让下载线程立刻收手并如实报告断网,而不是继续跑完
// 5 次「重试 → 12 秒超时」,最后给出一个与真因无关的
// 「下载失败,已重试 N 次」。
transferAbortReason = message
downloadStatusText?.apply {
text = "QFT 本地网络已断开,正在结束当前下载请求…"
@@ -705,8 +707,8 @@ class MainActivity : ComponentActivity() {
private fun showPairError(pair: PairingPayload?, message: String) {
// Do not unregister here: Android may still be displaying its
// Wi-Fi-selection sheet, and unregistering at that moment produces
// "the app cancelled the device selection request". The next attempt
// (or returning home) releases the previous request safely.
// "the app cancelled the device selection request". A retry reuses
// this retained request; returning home is what explicitly releases it.
pairConnectionAttempt++
connectedNetwork = null
connectedBaseUrl = null
@@ -718,7 +720,7 @@ class MainActivity : ComponentActivity() {
root.addView(button("重试连接").apply { setOnClickListener { ensureWifiPermissionThenConnect(pair) } })
root.addView(space(10))
}
root.addView(button("重新扫码").apply { setOnClickListener { requestScanner(ScannerMode.PAIR) } })
root.addView(button("返回首页重新扫码").apply { setOnClickListener { showHome() } })
root.addView(space(10))
root.addView(button("返回首页").apply { setOnClickListener { showHome() } })
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
@@ -776,7 +778,13 @@ class MainActivity : ComponentActivity() {
root.addView(space(18))
root.addView(card().apply {
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(humanBytes(total), 13f, MUTED), matchWrap(top = 4))
})
@@ -801,7 +809,7 @@ class MainActivity : ComponentActivity() {
val saved = DownloadClient.download(
context = this@MainActivity,
resolver = contentResolver,
network = connectedNetwork,
networkProvider = { connectedNetwork },
baseUrl = baseUrl,
pairing = pair,
deviceName = receiverDeviceLabel,
@@ -980,11 +988,12 @@ class MainActivity : ComponentActivity() {
}
UploadClient.upload(
resolver = contentResolver,
network = connectedNetwork,
networkProvider = { connectedNetwork },
baseUrl = baseUrl,
pairing = pair,
deviceName = receiverDeviceLabel,
file = file,
abortReason = { transferAbortReason },
) { sent, total ->
runOnUiThread {
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.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import java.util.UUID
data class SelectedFile(
val uri: Uri,
val name: String,
val size: Long,
val uploadKey: String = UUID.randomUUID().toString(),
)
object UploadClient {
@@ -42,7 +44,7 @@ object UploadClient {
// 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;
// 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? {
var name = "qft-upload.bin"
@@ -220,16 +222,20 @@ object UploadClient {
fun upload(
resolver: ContentResolver,
network: Network?,
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
file: SelectedFile,
abortReason: () -> String? = { null },
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 {
uploadParallel(resolver, network, baseUrl, pairing, deviceName, file, onProgress)
uploadParallel(resolver, networkProvider, baseUrl, pairing, deviceName, file, abortReason, onProgress)
return
} catch (e: Exception) {
// 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")
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
repeat(LOCAL_REQUEST_ATTEMPTS) { attempt ->
abortReason()?.let { throw IllegalStateException(it, last) }
try {
return block()
} catch (e: Throwable) {
@@ -260,7 +272,7 @@ object UploadClient {
e,
)
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()
throw e
}
@@ -288,27 +300,37 @@ object UploadClient {
private fun uploadParallel(
resolver: ContentResolver,
network: Network?,
networkProvider: () -> Network?,
baseUrl: String,
pairing: PairingPayload,
deviceName: String,
file: SelectedFile,
abortReason: () -> String?,
onProgress: (sent: Long, total: Long) -> Unit,
) {
Log.i(TAG, "parallel init: size=${file.size}, endpoint=$baseUrl")
val init = initUpload(network, baseUrl, pairing, deviceName, file)
Log.i(TAG, "parallel init complete: chunks=${init.totalChunks}, chunkSize=${init.chunkSize}")
val init = initUpload(networkProvider, baseUrl, pairing, deviceName, file, file.uploadKey, abortReason)
Log.i(TAG, "parallel init complete: chunks=${init.totalChunks}, pending=${init.pendingChunks.size}, chunkSize=${init.chunkSize}")
if (init.totalChunks == 0) {
finishUpload(network, baseUrl, pairing, deviceName, init.uploadId)
finishUpload(networkProvider, baseUrl, pairing, deviceName, init.uploadId, abortReason)
onProgress(0, 0)
return
}
val cpuWorkers = Runtime.getRuntime().availableProcessors().coerceIn(2, MAX_WORKERS)
val workerCount = init.totalChunks.coerceAtMost(cpuWorkers).coerceAtLeast(1)
// Upload workers are network/disk bound, not CPU bound. Large files
// 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 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 failures = mutableListOf<Throwable>()
val futures = (0 until workerCount).map {
@@ -318,13 +340,15 @@ object UploadClient {
val channel = input.channel
val buffer = ByteBuffer.allocate(STREAM_BUFFER)
while (!Thread.currentThread().isInterrupted) {
val index = next.getAndIncrement()
if (index >= init.totalChunks) break
abortReason()?.let { error(it) }
val pendingIndex = next.getAndIncrement()
if (pendingIndex >= init.pendingChunks.size) break
val index = init.pendingChunks[pendingIndex]
val offset = index.toLong() * init.chunkSize
val length = minOf(init.chunkSize, file.size - offset)
sendChunk(
network, baseUrl, pairing, deviceName,
init.uploadId, index, channel, offset, length, buffer,
networkProvider, baseUrl, pairing, deviceName,
init.uploadId, index, channel, offset, length, buffer, abortReason,
)
val totalSent = sent.addAndGet(length)
val now = android.os.SystemClock.elapsedRealtime()
@@ -345,7 +369,7 @@ object UploadClient {
pool.shutdownNow()
}
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)
}
@@ -357,10 +381,10 @@ object UploadClient {
}
private fun initUpload(
network: Network?, baseUrl: String, pairing: PairingPayload,
deviceName: String, file: SelectedFile,
): InitReply = retryLocalRequest("本地上传初始化失败") {
val conn = open(network, URL("$baseUrl/qft/upload/init"))
networkProvider: () -> Network?, baseUrl: String, pairing: PairingPayload,
deviceName: String, file: SelectedFile, requestKey: String, abortReason: () -> String?,
): InitReply = retryLocalRequest("本地上传初始化失败", abortReason) {
val conn = open(networkProvider(), URL("$baseUrl/qft/upload/init"))
try {
conn.requestMethod = "POST"
conn.doOutput = true
@@ -371,15 +395,27 @@ object UploadClient {
conn.setRequestProperty("X-QFT-Filename", b64(file.name))
conn.setRequestProperty("X-QFT-Size", file.size.toString())
conn.setRequestProperty("X-QFT-Chunk-Size", desiredChunkSize(file.size).toString())
conn.setRequestProperty("X-QFT-Upload-Key", requestKey)
conn.outputStream.close()
val code = conn.responseCode
if (code != 200) error("初始化并行上传失败 HTTP $code")
val body = conn.inputStream.bufferedReader().use { it.readText() }
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(
uploadId = obj.getString("upload_id"),
chunkSize = obj.getLong("chunk_size"),
totalChunks = obj.getInt("total_chunks"),
totalChunks = totalChunks,
pendingChunks = pending,
)
} finally {
conn.disconnect()
@@ -387,17 +423,17 @@ object UploadClient {
}
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,
buffer: ByteBuffer,
) = retryLocalRequest("分块 $index 上传失败") {
buffer: ByteBuffer, abortReason: () -> String?,
) = retryLocalRequest("分块 $index 上传失败", abortReason) {
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 {
conn.requestMethod = "POST"
conn.doOutput = true
conn.connectTimeout = 4_000
conn.readTimeout = 15_000
conn.connectTimeout = 8_000
conn.readTimeout = 90_000
conn.setFixedLengthStreamingMode(length)
conn.setRequestProperty("Content-Type", "application/octet-stream")
commonHeaders(conn, pairing, deviceName)
@@ -406,6 +442,7 @@ object UploadClient {
var remaining = length
val array = buffer.array()
while (remaining > 0) {
abortReason()?.let { error(it) }
buffer.clear()
buffer.limit(minOf(array.size.toLong(), remaining).toInt())
val n = channel.read(buffer)
@@ -428,10 +465,10 @@ object UploadClient {
}
private fun finishUpload(
network: Network?, baseUrl: String, pairing: PairingPayload,
deviceName: String, uploadId: String,
) = retryLocalRequest("本地上传完成确认失败") {
val conn = open(network, URL("$baseUrl/qft/upload/finish/$uploadId"))
networkProvider: () -> Network?, baseUrl: String, pairing: PairingPayload,
deviceName: String, uploadId: String, abortReason: () -> String?,
) = retryLocalRequest("本地上传完成确认失败", abortReason) {
val conn = open(networkProvider(), URL("$baseUrl/qft/upload/finish/$uploadId"))
try {
conn.requestMethod = "POST"
conn.doOutput = true
@@ -458,6 +495,7 @@ object UploadClient {
pairing: PairingPayload,
deviceName: String,
file: SelectedFile,
abortReason: () -> String?,
onProgress: (sent: Long, total: Long) -> Unit,
) {
val conn = open(network, URL("$baseUrl/qft/upload"))
@@ -478,6 +516,7 @@ object UploadClient {
var sent = 0L
var lastReport = 0L
while (true) {
abortReason()?.let { error(it) }
val n = input.read(buf)
if (n <= 0) break
output.write(buf, 0, n)
@@ -520,7 +559,7 @@ object UploadClient {
private fun commonHeaders(conn: HttpURLConnection, pairing: PairingPayload, deviceName: String) {
conn.useCaches = false
conn.setRequestProperty("Connection", "close")
conn.setRequestProperty("Connection", "keep-alive")
conn.setRequestProperty("X-QFT-Token", pairing.session)
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 handler = Handler(Looper.getMainLooper())
private var callback: ConnectivityManager.NetworkCallback? = null
@Volatile private var currentNetwork: Network? = null
private var generation = 0
private var legacyPreviousNetworkId: Int? = null
private var legacyTargetNetworkId: Int? = null
@@ -65,11 +66,17 @@ class WifiConnector(context: Context) {
// 同一个 SSID 的请求还挂在系统那里且没有终结:换掉 UI 回调即可,
// 千万不要 unregister 再重新 requestNetwork。
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}")
listener = Listener(onStatus, onAvailable, onUnavailable)
notifyStatus("系统配网请求仍在进行中,正在继续等待…")
scheduleWaitHints(generation, wifi.ssid)
val active = currentNetwork
if (active != null) {
notifyStatus("QFT 本地网络仍保持连接")
notifyAvailable(active)
} else {
notifyStatus("系统配网请求仍在进行中,正在自动恢复连接…")
scheduleWaitHints(generation, wifi.ssid)
}
return
}
@@ -80,7 +87,8 @@ class WifiConnector(context: Context) {
if (wifi == null) {
notifyStatus("正在使用当前局域网发现电脑…")
val wifiNetwork = bestWifiNetwork()
notifyAvailable(wifiNetwork ?: connectivity.activeNetwork)
currentNetwork = wifiNetwork ?: connectivity.activeNetwork
notifyAvailable(currentNetwork)
return
}
@@ -125,10 +133,12 @@ class WifiConnector(context: Context) {
val cb = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
if (run != generation || terminal.get()) return
currentNetwork = network
Log.i(TAG, "attempt=$run framework onAvailable network=$network")
notifyStatus("Wi-Fi 已关联,正在等待本地 IP/路由…")
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")
notifyStatus("QFT 本地网络已就绪,正在快速发现电脑…")
notifyAvailable(network)
@@ -137,8 +147,9 @@ class WifiConnector(context: Context) {
}
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)) {
currentNetwork = network
Log.i(TAG, "attempt=$run IPv4 ready network=$network")
notifyStatus("QFT 本地网络已就绪,正在快速发现电脑…")
notifyAvailable(network)
@@ -147,6 +158,7 @@ class WifiConnector(context: Context) {
override fun onUnavailable() {
if (run != generation || !terminal.compareAndSet(false, true)) return
currentNetwork = null
val waited = SystemClock.elapsedRealtime() - startedAt
Log.w(TAG, "attempt=$run framework onUnavailable after ${waited}ms")
// Android 不区分「用户点了取消」和「始终没扫到这个热点」,
@@ -164,9 +176,14 @@ class WifiConnector(context: Context) {
}
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")
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
@@ -246,6 +263,7 @@ class WifiConnector(context: Context) {
val network = bestWifiNetwork()
if (network != null && hasIpv4(connectivity.getLinkProperties(network))) {
notifyStatus("QFT Wi-Fi 已连接,正在快速发现电脑…")
currentNetwork = network
notifyAvailable(network)
return
}
@@ -375,6 +393,7 @@ class WifiConnector(context: Context) {
try { connectivity.unregisterNetworkCallback(it) } catch (_: Exception) {}
}
callback = null
currentNetwork = null
listener = null
pendingSsid = null
pendingDelivered = null