feat: 提交0.4加速版二维码直传与自动配网优化
This commit is contained in:
@@ -34,6 +34,10 @@ object UploadClient {
|
||||
private const val CHUNK_LARGE = 16L * 1024L * 1024L
|
||||
private const val MAX_WORKERS = 8
|
||||
private const val STREAM_BUFFER = 1024 * 1024
|
||||
// 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
|
||||
|
||||
fun describeFile(resolver: ContentResolver, uri: Uri): SelectedFile? {
|
||||
var name = "qft-upload.bin"
|
||||
@@ -68,13 +72,15 @@ object UploadClient {
|
||||
return try {
|
||||
network?.bindSocket(socket)
|
||||
socket.broadcast = true
|
||||
socket.soTimeout = 220
|
||||
socket.soTimeout = 160
|
||||
val request = "QFTDISC2:${pairing.session}".toByteArray(Charsets.US_ASCII)
|
||||
repeat(3) {
|
||||
// Two directed/broadcast bursts fit within the initial routing
|
||||
// window while keeping service discovery below 600 ms.
|
||||
repeat(2) {
|
||||
for (host in hosts) {
|
||||
try { socket.send(DatagramPacket(request, request.size, host, DISCOVERY_PORT)) } catch (_: Exception) {}
|
||||
}
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + 320
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + 240
|
||||
while (android.os.SystemClock.elapsedRealtime() < deadline) {
|
||||
val buf = ByteArray(512)
|
||||
val packet = DatagramPacket(buf, buf.size)
|
||||
@@ -117,13 +123,13 @@ object UploadClient {
|
||||
.filter { PairingProtocol.isAllowedLocalHttpEndpoint(it, pairing.port) }
|
||||
.distinct()
|
||||
|
||||
// Stage 1: expected gateway/QR addresses. Repeat quickly because a Wi-Fi
|
||||
// callback can precede ARP/firewall classification by a few hundred ms.
|
||||
val fastDeadline = android.os.SystemClock.elapsedRealtime() + 2_400
|
||||
// Stage 1: QR-carried endpoints and actual gateway. This is the normal
|
||||
// path and must complete well inside the 10-second pairing budget.
|
||||
val fastDeadline = android.os.SystemClock.elapsedRealtime() + 1_050
|
||||
while (fast.isNotEmpty() && android.os.SystemClock.elapsedRealtime() < fastDeadline) {
|
||||
probeCandidatesParallel(network, pairing, deviceName, fast, workers = 8, connectMs = 320, readMs = 380)
|
||||
probeCandidatesParallel(network, pairing, deviceName, fast, workers = 12, connectMs = 220, readMs = 260)
|
||||
?.let { return it }
|
||||
try { Thread.sleep(90) } catch (_: InterruptedException) { return null }
|
||||
try { Thread.sleep(60) } catch (_: InterruptedException) { return null }
|
||||
}
|
||||
|
||||
// Stage 2: actual connected subnet fallback. This specifically fixes the
|
||||
@@ -135,16 +141,17 @@ object UploadClient {
|
||||
.filterNot { it in fast }
|
||||
.distinct()
|
||||
if (scan.isNotEmpty()) {
|
||||
probeCandidatesParallel(network, pairing, deviceName, scan, workers = 24, connectMs = 240, readMs = 300)
|
||||
probeCandidatesParallel(network, pairing, deviceName, scan, workers = 48, connectMs = 180, readMs = 220)
|
||||
?.let { return it }
|
||||
}
|
||||
|
||||
// Final short retry for the expected addresses after the subnet scan.
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + 2_000
|
||||
// One short final pass covers a just-finished ARP/Windows firewall
|
||||
// transition without adding the former two-second tail latency.
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + 500
|
||||
while (fast.isNotEmpty() && android.os.SystemClock.elapsedRealtime() < deadline) {
|
||||
probeCandidatesParallel(network, pairing, deviceName, fast, workers = 8, connectMs = 450, readMs = 500)
|
||||
probeCandidatesParallel(network, pairing, deviceName, fast, workers = 12, connectMs = 240, readMs = 260)
|
||||
?.let { return it }
|
||||
try { Thread.sleep(120) } catch (_: InterruptedException) { return null }
|
||||
try { Thread.sleep(50) } catch (_: InterruptedException) { return null }
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -169,7 +176,7 @@ object UploadClient {
|
||||
return try {
|
||||
val overallMs = (connectMs + readMs + 150).toLong() *
|
||||
((candidates.size + workers - 1) / workers).coerceAtLeast(1)
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + overallMs.coerceAtMost(4_500L)
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + overallMs.coerceAtMost(2_400L)
|
||||
repeat(candidates.size) {
|
||||
val remaining = deadline - android.os.SystemClock.elapsedRealtime()
|
||||
if (remaining <= 0) return null
|
||||
@@ -220,9 +227,13 @@ object UploadClient {
|
||||
uploadParallel(resolver, network, baseUrl, pairing, deviceName, file, onProgress)
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
// If the provider cannot sustain random access or the v2 endpoint
|
||||
// is unavailable, preserve compatibility with the v1 stream path.
|
||||
// Never mask a transient v2 failure by immediately switching to a
|
||||
// second full-file stream (the old behavior produced the opaque
|
||||
// `unexpected end of stream` shown by Android). Only a confirmed
|
||||
// missing v2 endpoint uses the compatibility route.
|
||||
if (e is SecurityException) throw e
|
||||
val message = e.message.orEmpty()
|
||||
if (!message.contains("HTTP 404") && !message.contains("HTTP 501")) throw e
|
||||
}
|
||||
}
|
||||
uploadLegacy(resolver, network, baseUrl, pairing, deviceName, file, onProgress)
|
||||
@@ -230,6 +241,24 @@ object UploadClient {
|
||||
|
||||
private data class InitReply(val uploadId: String, val chunkSize: Long, val totalChunks: Int)
|
||||
|
||||
private fun <T> retryLocalRequest(label: String, block: () -> T): T {
|
||||
var last: Throwable? = null
|
||||
repeat(LOCAL_REQUEST_ATTEMPTS) { attempt ->
|
||||
try {
|
||||
return block()
|
||||
} catch (e: Throwable) {
|
||||
last = e
|
||||
if (attempt + 1 < LOCAL_REQUEST_ATTEMPTS) {
|
||||
try { Thread.sleep(120L * (attempt + 1)) } catch (_: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw IllegalStateException("$label after $LOCAL_REQUEST_ATTEMPTS attempts", last)
|
||||
}
|
||||
|
||||
private fun uploadParallel(
|
||||
resolver: ContentResolver,
|
||||
network: Network?,
|
||||
@@ -301,7 +330,7 @@ object UploadClient {
|
||||
private fun initUpload(
|
||||
network: Network?, baseUrl: String, pairing: PairingPayload,
|
||||
deviceName: String, file: SelectedFile,
|
||||
): InitReply {
|
||||
): InitReply = retryLocalRequest("本地上传初始化失败") {
|
||||
val conn = open(network, URL("$baseUrl/qft/upload/init"))
|
||||
try {
|
||||
conn.requestMethod = "POST"
|
||||
@@ -318,7 +347,7 @@ object UploadClient {
|
||||
if (code != 200) error("初始化并行上传失败 HTTP $code")
|
||||
val body = conn.inputStream.bufferedReader().use { it.readText() }
|
||||
val obj = JSONObject(body)
|
||||
return InitReply(
|
||||
InitReply(
|
||||
uploadId = obj.getString("upload_id"),
|
||||
chunkSize = obj.getLong("chunk_size"),
|
||||
totalChunks = obj.getInt("total_chunks"),
|
||||
@@ -332,7 +361,7 @@ object UploadClient {
|
||||
network: Network?, baseUrl: String, pairing: PairingPayload, deviceName: String,
|
||||
uploadId: String, index: Int, channel: FileChannel, offset: Long, length: Long,
|
||||
buffer: ByteBuffer,
|
||||
) {
|
||||
) = retryLocalRequest("分块 $index 上传失败") {
|
||||
val conn = open(network, URL("$baseUrl/qft/upload/chunk/$uploadId/$index"))
|
||||
try {
|
||||
conn.requestMethod = "POST"
|
||||
@@ -370,7 +399,7 @@ 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"))
|
||||
try {
|
||||
conn.requestMethod = "POST"
|
||||
|
||||
Reference in New Issue
Block a user