mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
4b658ad46c
commit
8b5df3d54d
@ -91,7 +91,7 @@ abstract class AbsCallBack(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (mCall.isCanceled()) {
|
if (mCall.isCanceled()) {
|
||||||
onError(IOException("Request Canceled"))
|
onError(IOException("Cronet Request Canceled"))
|
||||||
request.cancel()
|
request.cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -122,13 +122,6 @@ abstract class AbsCallBack(
|
|||||||
override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) {
|
override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) {
|
||||||
this.request = request
|
this.request = request
|
||||||
|
|
||||||
cancelJob = Coroutine.async {
|
|
||||||
while (!mCall.isCanceled()) {
|
|
||||||
delay(1000)
|
|
||||||
}
|
|
||||||
request.cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
val response: Response
|
val response: Response
|
||||||
try {
|
try {
|
||||||
response = toResponse(originalRequest, info, urlResponseInfoChain, CronetBodySource())
|
response = toResponse(originalRequest, info, urlResponseInfoChain, CronetBodySource())
|
||||||
@ -184,6 +177,7 @@ abstract class AbsCallBack(
|
|||||||
//UrlResponseInfo可能为null
|
//UrlResponseInfo可能为null
|
||||||
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) {
|
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) {
|
||||||
callbackResults.add(CallbackResult(CallbackStep.ON_FAILED, null, error))
|
callbackResults.add(CallbackResult(CallbackStep.ON_FAILED, null, error))
|
||||||
|
cancelJob?.cancel()
|
||||||
DebugLog.e(javaClass.name, error.message.toString())
|
DebugLog.e(javaClass.name, error.message.toString())
|
||||||
onError(error.asIOException())
|
onError(error.asIOException())
|
||||||
eventListener?.callFailed(mCall, error)
|
eventListener?.callFailed(mCall, error)
|
||||||
@ -203,11 +197,20 @@ abstract class AbsCallBack(
|
|||||||
}
|
}
|
||||||
canceled.set(true)
|
canceled.set(true)
|
||||||
callbackResults.add(CallbackResult(CallbackStep.ON_CANCELED))
|
callbackResults.add(CallbackResult(CallbackStep.ON_CANCELED))
|
||||||
|
cancelJob?.cancel()
|
||||||
//DebugLog.i(javaClass.simpleName, "cancel[${info?.negotiatedProtocol}]${info?.url}")
|
//DebugLog.i(javaClass.simpleName, "cancel[${info?.negotiatedProtocol}]${info?.url}")
|
||||||
eventListener?.callEnd(mCall)
|
eventListener?.callEnd(mCall)
|
||||||
//onError(IOException("Cronet Request Canceled"))
|
onError(IOException("Cronet Request Canceled"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun startCheckCancelJob(request: UrlRequest) {
|
||||||
|
cancelJob = Coroutine.async {
|
||||||
|
while (!mCall.isCanceled()) {
|
||||||
|
delay(1000)
|
||||||
|
}
|
||||||
|
request.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
mResponse = Response.Builder()
|
mResponse = Response.Builder()
|
||||||
@ -431,7 +434,7 @@ abstract class AbsCallBack(
|
|||||||
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
|
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
|
||||||
override fun read(sink: Buffer, byteCount: Long): Long {
|
override fun read(sink: Buffer, byteCount: Long): Long {
|
||||||
if (canceled.get()) {
|
if (canceled.get()) {
|
||||||
throw IOException("Request Canceled")
|
throw IOException("Cronet Request Canceled")
|
||||||
}
|
}
|
||||||
|
|
||||||
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
|
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
|
||||||
@ -450,7 +453,7 @@ abstract class AbsCallBack(
|
|||||||
val result = callbackResults.poll(timeout, TimeUnit.MILLISECONDS)
|
val result = callbackResults.poll(timeout, TimeUnit.MILLISECONDS)
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
request?.cancel()
|
request?.cancel()
|
||||||
throw IOException("Body Read Timeout")
|
throw IOException("Cronet request body read timeout after wait $timeout ms")
|
||||||
}
|
}
|
||||||
|
|
||||||
return when (result.callbackStep) {
|
return when (result.callbackStep) {
|
||||||
|
@ -23,6 +23,7 @@ class NewCallBack(originalRequest: Request, mCall: Call, readTimeoutMillis: Int)
|
|||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun waitForDone(urlRequest: UrlRequest): Response {
|
override fun waitForDone(urlRequest: UrlRequest): Response {
|
||||||
urlRequest.start()
|
urlRequest.start()
|
||||||
|
startCheckCancelJob(urlRequest)
|
||||||
//DebugLog.i(javaClass.simpleName, "start ${originalRequest.method} ${originalRequest.url}")
|
//DebugLog.i(javaClass.simpleName, "start ${originalRequest.method} ${originalRequest.url}")
|
||||||
return if (mCall.timeout().timeoutNanos() > 0) {
|
return if (mCall.timeout().timeoutNanos() > 0) {
|
||||||
responseFuture.get(mCall.timeout().timeoutNanos(), TimeUnit.NANOSECONDS)
|
responseFuture.get(mCall.timeout().timeoutNanos(), TimeUnit.NANOSECONDS)
|
||||||
|
@ -20,6 +20,7 @@ class OldCallback(originalRequest: Request, mCall: Call, readTimeoutMillis: Int)
|
|||||||
//获取okhttp call的完整请求的超时时间
|
//获取okhttp call的完整请求的超时时间
|
||||||
val timeOutMs: Long = mCall.timeout().timeoutNanos() / 1000000
|
val timeOutMs: Long = mCall.timeout().timeoutNanos() / 1000000
|
||||||
urlRequest.start()
|
urlRequest.start()
|
||||||
|
startCheckCancelJob(urlRequest)
|
||||||
if (timeOutMs > 0) {
|
if (timeOutMs > 0) {
|
||||||
mResponseCondition.block(timeOutMs)
|
mResponseCondition.block(timeOutMs)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user