Android开发神器:OkHttp框架源码解析

2.EventListener.callStart()也是在RealCall.execute()嵌入到Request调用过程, EventListener.callEnd()位于StreamAllocation中调用

3.Request.Builder

url (String/URL/HttpUrl)

header

CacheControl

Tag (Use this API to attach timing, debugging, or other application data to a request so that you may read it in interceptors, event listeners, or callbacks.)

BridgeInterceptor

Bridges from application code to network code. First it builds a network request from a user request. Then it proceeds to call the network. Finally it builds a user response from the network response.

此拦截器是应用码到网络码的桥接。它会将用户请求封装成一个网络请求并且执行请求,同时它还完成从网络响应到用户响应的转化. 最后Chain.proceed() 方法启动拦截器责任链, RealInterceptorChain中通过递归调用将网络请求以及响应的任务分别分配到各个拦截器中, 然后通过ResponseBuilder.build()方法将网络响应封装, 然后递归调用责任链模式使得调用以及Response处理的过程可以一并写入BridgeInterceptor中

public final class RealInterceptorChain implements Interceptor.Chain { public Response proceed(Request request, StreamAllocation streamAllocation,

HttpCodec httpCodec, RealConnection connection) throws IOException { if (index >= interceptors.size()) throw new AssertionError();

calls++;

… // Call the next interceptor in the chain.

RealInterceptorChain next = new RealInterceptorChain(interceptors,

streamAllocation, httpCodec,connection, index + 1, request, call,

eventListener, connectTimeout, readTimeout,writeTimeout);

Interceptor interceptor = interceptors.get(index);

Response response = interceptor.intercept(next);

… return response;

}

}

CallServiceInterceptor;

Interceptor的逻辑均在intercept()方法中实现, 在通过Chain实体类获取到请求主题之后,通过BufferedSink接口将请求转发到Okio接口,在拦截过程中通过EventListener接口将拦截器处理状态(主要是RequestBodyStart和RequestBodyEnd两个状态)发送出去

public final class CallServiceInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException {

Response.Builder responseBuilder = null; if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) { // If there’s a “Expect: 100-continue” header on the request, wait for a "HTTP/1.1 100

// Continue" response before transmitting the request body. If we don’t get that, return

// what we did get (such as a 4xx response) without ever transmitting the request body.

if (“100-continue”.equalsIgnoreCase(request.header(“Expect”))) {

httpCodec.flushRequest();

realChain.eventListener().responseHeadersStart(realChain.call());

responseBuilder = httpCodec.readResponseHeaders(true);

} if (responseBuilder == null) { // Write the request body if the “Expect: 100-continue” expectation was met.

realChain.eventListener().requestBodyStart(realChain.call()); long contentLength = request.body().contentLength();

CountingSink requestBodyOut = new CountingSink(httpCodec.createRequestBody(request, contentLength));

BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);

request.body().writeTo(bufferedRequestBody);

bufferedRequestBody.close();

realChain.eventListener()

.requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);

} else if (!connection.isMultiplexed()) { // If the “Expect: 100-continue” expectation wasn’t met, prevent the HTTP/1 connection

// from being reused. Otherwise we’re still obligated to transmit the request body to

// leave the connection in a consistent state.

streamAllocation.noNewStreams();

}

}

}

}

CacheInterceptor;

public final class CacheInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException {

CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();

Request networkRequest = strategy.networkRequest;

Response cacheResponse = strategy.cacheResponse; if (cache != null) { /**

  • Track an HTTP response being satisfied with {@code cacheStrategy}.

  • 主要是跟踪networkRequest次数以及对应Cache的hitcount

*/

cache.trackResponse(strategy);

} if (cacheCandidate != null && cacheResponse == null) {

closeQuietly(cacheCandidate.body()); // The cache candidate wasn’t applicable. Close it.

} // If we’re forbidden from using the network and the cache is insufficient, fail.

if (networkRequest == null && cacheResponse == null) { return new Response.Builder()

.request(chain.request())

.protocol(Protocol.HTTP_1_1)

.code(504)

.message(“Unsatisfiable Request (only-if-cached)”)

.body(Util.EMPTY_RESPONSE)

.sentRequestAtMillis(-1L)

.receivedResponseAtMillis(System.currentTimeMillis())

.build();

} // If we don’t need the network, we’re done.

if (networkRequest == null) { return cacheResponse.newBuilder()

.cacheResponse(stripBody(cacheResponse))

.build();

} //在chain.proceed()调用下一个拦截器

Response networkResponse = null; try {

networkResponse = chain.proceed(networkRequest);

} finally { // If we’re crashing on I/O or otherwise, don’t leak the cache body.

if (networkResponse == null && cacheCandidate != null) {

closeQuietly(cacheCandidate.body());

}

} //处理response并返回

… return response;

}

}

OkHttpClient

OkHttpClient托管着所有HTTP调用, 每个Client均拥有自己的连接池和线程池

实现抽象类Internal的方法,这是Internel抽象类唯一的实现,方法与CacheInterceptor控制Http的Header.Lenient区域和StreamAlloction从连接池中获取连接有关

private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException {

… synchronized (connectionPool) {

… if (result == null) { // Attempt to get a connection from the pool.

Internal.instance.get(connectionPool, address, this, null); if (connection != null) {

foundPooledConnection = true;

result = connection;

} else {

selectedRoute = route;

}

}

} return result;

RouteDatabase && RouteSeletor

RouteDatabase是记录连接失败的连接路径的黑名单,从而OkHttp可以从失败中学习并且倾向于选择其他可用的路径,RouteSeletor通过RouteDatabase.shouldPostpone(route)方法可获知此路径是否近期曾连接失败,RouteSelector部分源码如下:

public final class RouteSelector { /**

  • Clients should invoke this method when they encounter a connectivity failure on a connection

  • returned by this route selector.

  • 在StreamAllocation.streamFailed()中添加了routeSelector.connectFailed()逻辑

*/

public void connectFailed(Route failedRoute, IOException failure) { if (failedRoute.proxy().type() != Proxy.Type.DIRECT && address.proxySelector() != null) { // Tell the proxy selector when we fail to connect on a fresh connection.

address.proxySelector().connectFailed(

address.url().uri(), failedRoute.proxy().address(), failure);

}

routeDatabase.failed(failedRoute);

}

}

synchronized void enqueue(AsyncCall call) { if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {

runningAsyncCalls.add(call);

executorService().execute(call);

} else {

readyAsyncCalls.add(call);

}

}

… /** Used by {@code Call#execute} to signal it is in-flight. */

synchronized void executed(RealCall call) {

runningSyncCalls.add(call);

}

ExecutorSevice.execute(AsyncCall)执行代码位于AsyncCall内部复写的execute()方法, 方法内定义一些Callback回调节点运行逻辑,包括用户主动取消执行(使用retryAndFollowUpInterceptor)以及执行请求成功或者失败时的回调方法

final class AsyncCall extends NamedRunnable {

… @Override protected void execute() { boolean signalledCallback = false; try {

Response response = getResponseWithInterceptorChain(); if (retryAndFollowUpInterceptor.isCanceled()) {

signalledCallback = true;

responseCallback.onFailure(RealCall.this, new IOException(“Canceled”));

} else {

signalledCallback = true;

responseCallback.onResponse(RealCall.this, response);

}

} catch (IOException e) { if (signalledCallback) { // Do not signal the callback twice!

Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);

} else {

eventListener.callFailed(RealCall.this, e);

responseCallback.onFailure(RealCall.this, e);

}

} finally {

client.dispatcher().finished(this);

}

}

}

惰性初始模式(Created Lazily)成员

ExecutorService()

CacheControl

WebSocket

WebSocket 异步非堵塞的web socket接口 (通过Enqueue方法来实现)

OkHttpClient 通过实现 WebSocket.Factory.newWebSocket 接口实现工厂构造, 通常是由 OkHttpClient来构造

WebSocket生命周期:

Connecting状态: 每个websocket的初始状态, 此时Message可能位于入队状态但是还没有被Dispatcher处理

Open状态: WebSocket已经被服务器端接受并且Socket位于完全开放状态, 所有Message入队之后会即刻被处理

Closing状态: WebSocket进入优雅的关闭状态,WebSocket继续处理已入队的Message但拒绝新的Message入队

Closed状态: WebSocket已完成收发Message的过程, 进入完全关闭状态

WebSocket受到网络等各种因素影响, 可能会断路而提前进入关闭流程

Canceled状态: 被动WebSocket失败连接为非优雅的过程, 而主动则是优雅短路过程

RealWebSocket

RealWebSocket管理着Request队列内容所占的空间大小以及关闭Socket之后留给优雅关闭的时间,默认为16M和60秒,在RealWebSocket.connect()方法中RealWebSocket对OkHttpClient以及Request封装成Call的形式,然后通过Call.enqueue()方法定义调用成功和失败时的Callback代码

public void connect(OkHttpClient client) {

client = client.newBuilder()

.eventListener(EventListener.NONE)

.protocols(ONLY_HTTP1)

.build(); final Request request = originalRequest.newBuilder()

.header(“Upgrade”, “websocket”)

.header(“Connection”, “Upgrade”)

.header(“Sec-WebSocket-Key”, key)

.header(“Sec-WebSocket-Version”, “13”)

.build();

call = Internal.instance.newWebSocketCall(client, request);

call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { try {

checkResponse(response);

} catch (ProtocolException e) {

failWebSocket(e, response);

closeQuietly(response); return;

} // Promote the HTTP streams into web socket streams.

StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);

streamAllocation.noNewStreams(); // Prevent connection pooling!

Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation); // Process all web socket messages.

try {

listener.onOpen(RealWebSocket.this, response);

String name = "OkHttp WebSocket " + request.url().redact();

initReaderAndWriter(name, streams);

streamAllocation.connection().socket().setSoTimeout(0);

loopReader();

} catch (Exception e) {

failWebSocket(e, null);

}

} @Override public void onFailure(Call call, IOException e) {

failWebSocket(e, null);

}

});

}

当Call请求被服务端响应的时候就将HTTP流导入到Web Socket流中,并且调用WebSocketListener相对应的状态方法, WebSocketListener状态如下:

onOpen()onMessage()onClosing()onClosed()onFailure()

WebSocket -> RealWebSocket

Connection -> RealConnection

Interceptor -> RealInterceptorChain

Call -> RealCall

ResponseBody -> RealResponseBody

Gzip压缩机制

处理Gzip压缩的代码在BridgeInterceptor中,默认情况下为gzip压缩状态,可以从下面的源码片段中获知。如果header中没有Accept-Encoding,默认自动添加 ,且标记变量transparentGzip为true

最后

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

欢迎大家一起交流讨论啊~
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

WebSocket -> RealWebSocket

Connection -> RealConnection

Interceptor -> RealInterceptorChain

Call -> RealCall

ResponseBody -> RealResponseBody

Gzip压缩机制

处理Gzip压缩的代码在BridgeInterceptor中,默认情况下为gzip压缩状态,可以从下面的源码片段中获知。如果header中没有Accept-Encoding,默认自动添加 ,且标记变量transparentGzip为true

最后

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

[外链图片转存中…(img-AwY3UMuQ-1714734932501)]

欢迎大家一起交流讨论啊~
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 23
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值