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 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 data class SelectedFile( val uri: Uri, val name: String, val size: Long, ) object UploadClient { 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 // 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" 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, network: Network?, baseUrl: String, pairing: PairingPayload, deviceName: String, file: SelectedFile, onProgress: (sent: Long, total: Long) -> Unit, ) { if (pairing.version >= 2 && isSeekable(resolver, file)) { try { uploadParallel(resolver, network, baseUrl, pairing, deviceName, file, 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 } } uploadLegacy(resolver, network, baseUrl, pairing, deviceName, file, onProgress) } private data class InitReply(val uploadId: String, val chunkSize: Long, val totalChunks: Int) private fun 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?, baseUrl: String, pairing: PairingPayload, deviceName: String, file: SelectedFile, onProgress: (sent: Long, total: Long) -> Unit, ) { val init = initUpload(network, baseUrl, pairing, deviceName, file) if (init.totalChunks == 0) { finishUpload(network, baseUrl, pairing, deviceName, init.uploadId) onProgress(0, 0) return } val cpuWorkers = Runtime.getRuntime().availableProcessors().coerceIn(2, MAX_WORKERS) val workerCount = init.totalChunks.coerceAtMost(cpuWorkers).coerceAtLeast(1) val pool = Executors.newFixedThreadPool(workerCount) val next = AtomicInteger(0) val sent = AtomicLong(0) 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) { val index = next.getAndIncrement() if (index >= init.totalChunks) break 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, ) 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(network, baseUrl, pairing, deviceName, init.uploadId) 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( network: Network?, baseUrl: String, pairing: PairingPayload, deviceName: String, file: SelectedFile, ): InitReply = retryLocalRequest("本地上传初始化失败") { val conn = open(network, 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.outputStream.close() val code = conn.responseCode if (code != 200) error("初始化并行上传失败 HTTP $code") val body = conn.inputStream.bufferedReader().use { it.readText() } val obj = JSONObject(body) InitReply( uploadId = obj.getString("upload_id"), chunkSize = obj.getLong("chunk_size"), totalChunks = obj.getInt("total_chunks"), ) } finally { conn.disconnect() } } private fun sendChunk( 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" conn.doOutput = true conn.connectTimeout = 4_000 conn.readTimeout = 15_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) { 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) {} } finally { conn.disconnect() } } 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" 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, 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) { 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", "close") 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)) }