package com.qft.optical import android.content.ContentResolver import android.database.Cursor import android.net.Network import android.net.Uri import android.provider.OpenableColumns import android.util.Log import org.json.JSONObject import java.io.FileInputStream import java.net.DatagramPacket import java.net.DatagramSocket import java.net.HttpURLConnection import java.net.InetAddress import java.net.URL import java.nio.ByteBuffer import java.nio.channels.FileChannel import java.util.concurrent.ExecutorCompletionService 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 { private const val TAG = "QftUpload" 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 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 // 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 = 8 fun describeFile(resolver: ContentResolver, uri: Uri): SelectedFile? { var name = "qft-upload.bin" var size = -1L var cursor: Cursor? = null try { cursor = resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE), null, null, null) if (cursor?.moveToFirst() == true) { val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE) if (nameIndex >= 0) name = cursor.getString(nameIndex) ?: name if (sizeIndex >= 0 && !cursor.isNull(sizeIndex)) size = cursor.getLong(sizeIndex) } } finally { cursor?.close() } if (size < 0) { size = try { resolver.openAssetFileDescriptor(uri, "r")?.use { it.length } ?: -1L } catch (_: Exception) { -1L } } if (size < 0) return null return SelectedFile(uri, name, size) } /** Fast path: ask the PC directly over UDP on the newly connected QFT Wi-Fi. */ fun discoverUdp( network: Network?, pairing: PairingPayload, hosts: List, ): String? { if (hosts.isEmpty()) return null val socket = DatagramSocket() return try { network?.bindSocket(socket) socket.broadcast = true socket.soTimeout = 160 val request = "QFTDISC2:${pairing.session}".toByteArray(Charsets.US_ASCII) // 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() + 240 while (android.os.SystemClock.elapsedRealtime() < deadline) { val buf = ByteArray(512) val packet = DatagramPacket(buf, buf.size) try { socket.receive(packet) } catch (_: Exception) { break } val text = String(packet.data, packet.offset, packet.length, Charsets.US_ASCII) val prefix = "QFTHERE2:${pairing.session}:" if (!text.startsWith(prefix)) continue val port = text.substring(prefix.length).toIntOrNull() ?: continue if (port != pairing.port) continue val base = "http://${packet.address.hostAddress}:$port" if (PairingProtocol.isAllowedLocalHttpEndpoint(base, pairing.port)) return base } } null } catch (_: Exception) { null } finally { socket.close() } } fun probeRobust( network: Network?, pairing: PairingPayload, deviceName: String, preferredEndpoints: List = emptyList(), discoveredEndpoint: String? = null, subnetEndpoints: List = emptyList(), ): String? { val common = listOf( "http://192.168.137.1:${pairing.port}", "http://192.168.49.1:${pairing.port}", ) val fast = (listOfNotNull(discoveredEndpoint) + preferredEndpoints + pairing.endpoints + common) .map { it.trim().removeSuffix("/") } .filter { PairingProtocol.isAllowedLocalHttpEndpoint(it, pairing.port) } .distinct() // 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 = 12, connectMs = 220, readMs = 260) ?.let { return it } try { Thread.sleep(60) } catch (_: InterruptedException) { return null } } // Stage 2: actual connected subnet fallback. This specifically fixes the // case where Windows Wi-Fi Direct is up but uses an unexpected gateway // address. Up to /24 is scanned concurrently and still session-authenticated. val scan = subnetEndpoints .map { it.trim().removeSuffix("/") } .filter { PairingProtocol.isAllowedLocalHttpEndpoint(it, pairing.port) } .filterNot { it in fast } .distinct() if (scan.isNotEmpty()) { probeCandidatesParallel(network, pairing, deviceName, scan, workers = 48, connectMs = 180, readMs = 220) ?.let { return it } } // 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 = 12, connectMs = 240, readMs = 260) ?.let { return it } try { Thread.sleep(50) } catch (_: InterruptedException) { return null } } return null } private fun probeCandidatesParallel( network: Network?, pairing: PairingPayload, deviceName: String, candidates: List, workers: Int, connectMs: Int, readMs: Int, ): String? { if (candidates.isEmpty()) return null val pool = Executors.newFixedThreadPool(candidates.size.coerceAtMost(workers).coerceAtLeast(1)) val completion = ExecutorCompletionService(pool) candidates.forEach { base -> completion.submit(java.util.concurrent.Callable { if (probeOne(network, pairing, deviceName, base, connectMs, readMs)) base else null }) } return try { val overallMs = (connectMs + readMs + 150).toLong() * ((candidates.size + workers - 1) / workers).coerceAtLeast(1) 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 val future = completion.poll(remaining.coerceAtMost(350L), TimeUnit.MILLISECONDS) ?: return@repeat val result = future.get() if (result != null) return result } null } catch (_: Exception) { null } finally { pool.shutdownNow() } } private fun probeOne( network: Network?, pairing: PairingPayload, deviceName: String, base: String, connectMs: Int, readMs: Int, ): Boolean { var conn: HttpURLConnection? = null return try { conn = open(network, URL("$base/qft/ping")) conn.requestMethod = "GET" conn.connectTimeout = connectMs conn.readTimeout = readMs conn.useCaches = false conn.setRequestProperty("X-QFT-Token", pairing.session) conn.setRequestProperty("X-QFT-Device", b64(deviceName)) conn.responseCode == 200 } catch (_: Exception) { false } finally { conn?.disconnect() } } fun upload( resolver: ContentResolver, 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) { require(isSeekable(resolver, file)) { "大文件必须使用可随机读取的本地文件才能并行直传;请先把云盘文件下载到手机本地后再选择" } try { uploadParallel(resolver, networkProvider, baseUrl, pairing, deviceName, file, abortReason, onProgress) return } catch (e: Exception) { // 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 } } Log.i(TAG, "using single-stream upload: size=${file.size}, endpoint=$baseUrl") uploadLegacy(resolver, networkProvider(), baseUrl, pairing, deviceName, file, abortReason, onProgress) } private data class InitReply( val uploadId: String, val chunkSize: Long, val totalChunks: Int, val pendingChunks: IntArray, ) private fun 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) { last = e Log.w( TAG, "$label: attempt=${attempt + 1}/$LOCAL_REQUEST_ATTEMPTS, ${failureSummary(e)}", e, ) if (attempt + 1 < LOCAL_REQUEST_ATTEMPTS) { try { Thread.sleep((250L shl attempt).coerceAtMost(3_000L)) } catch (_: InterruptedException) { Thread.currentThread().interrupt() throw e } } } } val failure = last ?: IllegalStateException("unknown local request failure") throw IllegalStateException( "$label,已重试 $LOCAL_REQUEST_ATTEMPTS 次:${failureSummary(failure)}", failure, ) } private fun failureSummary(error: Throwable): String { val chain = ArrayList(3) var current: Throwable? = error while (current != null && chain.size < 3) { val type = current.javaClass.simpleName.ifBlank { current.javaClass.name } val detail = current.message?.replace(Regex("\\s+"), " ")?.take(240) chain += if (detail.isNullOrBlank()) type else "$type: $detail" current = current.cause } return chain.joinToString(" <- ") } private fun uploadParallel( resolver: ContentResolver, 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(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(networkProvider, baseUrl, pairing, deviceName, init.uploadId, abortReason) onProgress(0, 0) return } // 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 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() val futures = (0 until workerCount).map { pool.submit { resolver.openFileDescriptor(file.uri, "r")?.use { pfd -> FileInputStream(pfd.fileDescriptor).use { input -> val channel = input.channel val buffer = ByteBuffer.allocate(STREAM_BUFFER) while (!Thread.currentThread().isInterrupted) { 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( networkProvider, baseUrl, pairing, deviceName, init.uploadId, index, channel, offset, length, buffer, abortReason, ) val totalSent = sent.addAndGet(length) val now = android.os.SystemClock.elapsedRealtime() val previous = lastReport.get() if (now - previous >= 80 && lastReport.compareAndSet(previous, now)) { onProgress(totalSent.coerceAtMost(file.size), file.size) } } } } ?: error("无法打开 ${file.name}") } } try { futures.forEach { future -> try { future.get() } catch (e: Exception) { failures += (e.cause ?: e) } } } finally { pool.shutdownNow() } if (failures.isNotEmpty()) throw failures.first() finishUpload(networkProvider, baseUrl, pairing, deviceName, init.uploadId, abortReason) onProgress(file.size, file.size) } private fun desiredChunkSize(fileSize: Long): Long = when { fileSize < 32L * 1024L * 1024L -> CHUNK_SMALL fileSize >= 512L * 1024L * 1024L -> CHUNK_LARGE else -> CHUNK_DEFAULT } private fun initUpload( 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 conn.connectTimeout = 3_000 conn.readTimeout = 6_000 conn.setFixedLengthStreamingMode(0) commonHeaders(conn, pairing, deviceName) 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 = totalChunks, pendingChunks = pending, ) } finally { conn.disconnect() } } private fun sendChunk( networkProvider: () -> Network?, baseUrl: String, pairing: PairingPayload, deviceName: String, uploadId: String, index: Int, channel: FileChannel, offset: Long, length: Long, buffer: ByteBuffer, abortReason: () -> String?, ) = retryLocalRequest("分块 $index 上传失败", abortReason) { Log.d(TAG, "chunk start: index=$index, bytes=$length, endpoint=$baseUrl") val conn = open(networkProvider(), URL("$baseUrl/qft/upload/chunk/$uploadId/$index")) try { conn.requestMethod = "POST" conn.doOutput = true conn.connectTimeout = 8_000 conn.readTimeout = 90_000 conn.setFixedLengthStreamingMode(length) conn.setRequestProperty("Content-Type", "application/octet-stream") commonHeaders(conn, pairing, deviceName) channel.position(offset) conn.outputStream.use { output -> 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) if (n <= 0) error("读取文件分块提前结束") output.write(array, 0, n) remaining -= n.toLong() } output.flush() } val code = conn.responseCode if (code != 200) { val message = try { conn.errorStream?.bufferedReader()?.use { it.readText() } } catch (_: Exception) { null } error("分块 $index 上传失败 HTTP $code${message?.let { ": $it" } ?: ""}") } try { conn.inputStream.close() } catch (_: Exception) {} Log.d(TAG, "chunk complete: index=$index, bytes=$length") } finally { conn.disconnect() } } private fun finishUpload( 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 conn.connectTimeout = 4_000 conn.readTimeout = 120_000 conn.setFixedLengthStreamingMode(0) commonHeaders(conn, pairing, deviceName) conn.outputStream.close() val code = conn.responseCode if (code != 200) { val message = try { conn.errorStream?.bufferedReader()?.use { it.readText() } } catch (_: Exception) { null } error("完成上传失败 HTTP $code${message?.let { ": $it" } ?: ""}") } conn.inputStream.close() } finally { conn.disconnect() } } private fun uploadLegacy( resolver: ContentResolver, network: Network?, baseUrl: String, pairing: PairingPayload, deviceName: String, file: SelectedFile, abortReason: () -> String?, onProgress: (sent: Long, total: Long) -> Unit, ) { val conn = open(network, URL("$baseUrl/qft/upload")) conn.requestMethod = "POST" conn.doOutput = true conn.connectTimeout = 8_000 conn.readTimeout = 30_000 conn.setFixedLengthStreamingMode(file.size) conn.setRequestProperty("Content-Type", "application/octet-stream") commonHeaders(conn, pairing, deviceName) conn.setRequestProperty("X-QFT-Filename", b64(file.name)) conn.setRequestProperty("X-QFT-Size", file.size.toString()) resolver.openInputStream(file.uri).use { input -> requireNotNull(input) { "无法打开 ${file.name}" } conn.outputStream.use { output -> val buf = ByteArray(STREAM_BUFFER) 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) sent += n val now = android.os.SystemClock.elapsedRealtime() if (now - lastReport >= 100 || sent == file.size) { onProgress(sent, file.size) lastReport = now } } output.flush() } } val code = conn.responseCode if (code != 200) { val message = try { conn.errorStream?.bufferedReader()?.use { it.readText() } } catch (_: Exception) { null } conn.disconnect() error("电脑返回 HTTP $code${message?.let { ": $it" } ?: ""}") } conn.inputStream.close() conn.disconnect() } private fun isSeekable(resolver: ContentResolver, file: SelectedFile): Boolean { if (file.size <= 0) return true return try { resolver.openFileDescriptor(file.uri, "r")?.use { pfd -> FileInputStream(pfd.fileDescriptor).use { input -> val channel = input.channel val target = minOf(file.size - 1, 4096L).coerceAtLeast(0) channel.position(target) channel.position() == target } } ?: false } catch (_: Exception) { false } } private fun commonHeaders(conn: HttpURLConnection, pairing: PairingPayload, deviceName: String) { conn.useCaches = false conn.setRequestProperty("Connection", "keep-alive") conn.setRequestProperty("X-QFT-Token", pairing.session) conn.setRequestProperty("X-QFT-Device", b64(deviceName)) } private fun open(network: Network?, url: URL): HttpURLConnection { return (network?.openConnection(url) ?: url.openConnection()) as HttpURLConnection } private fun b64(value: String): String = Base64Url.encode(value.toByteArray(Charsets.UTF_8)) }