commit
255d276add
@ -0,0 +1,66 @@ |
|||||||
|
package io.legado.app.help.http.cronet |
||||||
|
|
||||||
|
|
||||||
|
import android.os.Build |
||||||
|
import androidx.annotation.RequiresApi |
||||||
|
import okhttp3.Call |
||||||
|
import okhttp3.Request |
||||||
|
import okhttp3.Response |
||||||
|
import org.chromium.net.CronetException |
||||||
|
import org.chromium.net.UrlRequest |
||||||
|
import org.chromium.net.UrlResponseInfo |
||||||
|
import java.io.IOException |
||||||
|
import java.nio.ByteBuffer |
||||||
|
import java.util.concurrent.CompletableFuture |
||||||
|
import java.util.concurrent.TimeUnit |
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.N) |
||||||
|
class NewCallBack(originalRequest: Request, mCall: Call) : AbsCallBack(originalRequest, mCall) { |
||||||
|
private val responseFuture = CompletableFuture<Response>() |
||||||
|
|
||||||
|
|
||||||
|
@Throws(IOException::class) |
||||||
|
override fun waitForDone(urlRequest: UrlRequest): Response { |
||||||
|
return responseFuture.get(mCall.timeout().timeoutNanos(), TimeUnit.NANOSECONDS) |
||||||
|
} |
||||||
|
|
||||||
|
override fun onRedirectReceived( |
||||||
|
request: UrlRequest, |
||||||
|
info: UrlResponseInfo, |
||||||
|
newLocationUrl: String |
||||||
|
) { |
||||||
|
super.onRedirectReceived(request, info, newLocationUrl) |
||||||
|
if (mException != null) { |
||||||
|
responseFuture.completeExceptionally(mException) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
override fun onReadCompleted( |
||||||
|
request: UrlRequest, |
||||||
|
info: UrlResponseInfo, |
||||||
|
byteBuffer: ByteBuffer |
||||||
|
) { |
||||||
|
super.onReadCompleted(request, info, byteBuffer) |
||||||
|
if (mException != null) { |
||||||
|
responseFuture.completeExceptionally(mException) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) { |
||||||
|
super.onSucceeded(request, info) |
||||||
|
responseFuture.complete(mResponse) |
||||||
|
} |
||||||
|
|
||||||
|
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) { |
||||||
|
super.onFailed(request, info, error) |
||||||
|
responseFuture.completeExceptionally(mException) |
||||||
|
} |
||||||
|
|
||||||
|
override fun onCanceled(request: UrlRequest?, info: UrlResponseInfo?) { |
||||||
|
super.onCanceled(request, info) |
||||||
|
responseFuture.completeExceptionally(mException) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,66 @@ |
|||||||
|
package io.legado.app.help.http.cronet |
||||||
|
|
||||||
|
import android.os.ConditionVariable |
||||||
|
import okhttp3.Call |
||||||
|
import okhttp3.Request |
||||||
|
import okhttp3.Response |
||||||
|
import org.chromium.net.CronetException |
||||||
|
import org.chromium.net.UrlRequest |
||||||
|
import org.chromium.net.UrlResponseInfo |
||||||
|
import java.io.IOException |
||||||
|
import java.nio.ByteBuffer |
||||||
|
|
||||||
|
class OldCallback(originalRequest: Request, mCall: Call) : AbsCallBack(originalRequest, mCall) { |
||||||
|
|
||||||
|
private val mResponseCondition = ConditionVariable() |
||||||
|
|
||||||
|
@Throws(IOException::class) |
||||||
|
override fun waitForDone(urlRequest: UrlRequest): Response { |
||||||
|
//获取okhttp call的完整请求的超时时间 |
||||||
|
val timeOutMs: Long = mCall.timeout().timeoutNanos() / 1000000 |
||||||
|
if (timeOutMs > 0) { |
||||||
|
mResponseCondition.block(timeOutMs) |
||||||
|
} else { |
||||||
|
mResponseCondition.block() |
||||||
|
} |
||||||
|
//ConditionVariable 正常open或者超时open后,检查urlRequest是否完成 |
||||||
|
if (!urlRequest.isDone) { |
||||||
|
urlRequest.cancel() |
||||||
|
mException = IOException("Cronet timeout after wait " + timeOutMs + "ms") |
||||||
|
} |
||||||
|
|
||||||
|
if (mException != null) { |
||||||
|
throw mException as IOException |
||||||
|
} |
||||||
|
return this.mResponse |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
override fun onReadCompleted( |
||||||
|
request: UrlRequest, |
||||||
|
info: UrlResponseInfo, |
||||||
|
byteBuffer: ByteBuffer |
||||||
|
) { |
||||||
|
super.onReadCompleted(request, info, byteBuffer) |
||||||
|
if (mException != null) { |
||||||
|
mResponseCondition.open() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) { |
||||||
|
super.onSucceeded(request, info) |
||||||
|
mResponseCondition.open() |
||||||
|
} |
||||||
|
|
||||||
|
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) { |
||||||
|
super.onFailed(request, info, error) |
||||||
|
mResponseCondition.open() |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
override fun onCanceled(request: UrlRequest?, info: UrlResponseInfo?) { |
||||||
|
super.onCanceled(request, info) |
||||||
|
mResponseCondition.open() |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue