V0.3.0版本优化传输速度和提示
This commit is contained in:
@@ -0,0 +1,473 @@
|
||||
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
|
||||
|
||||
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<InetAddress>,
|
||||
): String? {
|
||||
if (hosts.isEmpty()) return null
|
||||
val socket = DatagramSocket()
|
||||
return try {
|
||||
network?.bindSocket(socket)
|
||||
socket.broadcast = true
|
||||
socket.soTimeout = 220
|
||||
val request = "QFTDISC2:${pairing.session}".toByteArray(Charsets.US_ASCII)
|
||||
repeat(3) {
|
||||
for (host in hosts) {
|
||||
try { socket.send(DatagramPacket(request, request.size, host, DISCOVERY_PORT)) } catch (_: Exception) {}
|
||||
}
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + 320
|
||||
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<String> = emptyList(),
|
||||
discoveredEndpoint: String? = null,
|
||||
subnetEndpoints: List<String> = 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: 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
|
||||
while (fast.isNotEmpty() && android.os.SystemClock.elapsedRealtime() < fastDeadline) {
|
||||
probeCandidatesParallel(network, pairing, deviceName, fast, workers = 8, connectMs = 320, readMs = 380)
|
||||
?.let { return it }
|
||||
try { Thread.sleep(90) } 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 = 24, connectMs = 240, readMs = 300)
|
||||
?.let { return it }
|
||||
}
|
||||
|
||||
// Final short retry for the expected addresses after the subnet scan.
|
||||
val deadline = android.os.SystemClock.elapsedRealtime() + 2_000
|
||||
while (fast.isNotEmpty() && android.os.SystemClock.elapsedRealtime() < deadline) {
|
||||
probeCandidatesParallel(network, pairing, deviceName, fast, workers = 8, connectMs = 450, readMs = 500)
|
||||
?.let { return it }
|
||||
try { Thread.sleep(120) } catch (_: InterruptedException) { return null }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun probeCandidatesParallel(
|
||||
network: Network?,
|
||||
pairing: PairingPayload,
|
||||
deviceName: String,
|
||||
candidates: List<String>,
|
||||
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<String?>(pool)
|
||||
candidates.forEach { base ->
|
||||
completion.submit(java.util.concurrent.Callable<String?> {
|
||||
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(4_500L)
|
||||
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) {
|
||||
// If the provider cannot sustain random access or the v2 endpoint
|
||||
// is unavailable, preserve compatibility with the v1 stream path.
|
||||
if (e is SecurityException) 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 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<Throwable>()
|
||||
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 {
|
||||
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)
|
||||
return 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,
|
||||
) {
|
||||
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,
|
||||
) {
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user