OkHttpClient 解剖
源碼地址:https://github.com/square/okhttp
上一篇對(duì)整體的流程禾锤,有了窺探包个,這次就對(duì)另外一個(gè)重要的類進(jìn)行學(xué)習(xí)分析——OkHttpCilent刷允。
OkHttpClient結(jié)構(gòu)上比較簡(jiǎn)單,使用了Bulider模式來進(jìn)行構(gòu)建碧囊。
這個(gè)類大體分了三部分:
- 提供內(nèi)部使用的靜態(tài)塊树灶;
- 提供給外部調(diào)用讀取信息的方法,以及實(shí)現(xiàn)了某些接口糯而,如之前的說到的newCall;
- Builder,構(gòu)建相應(yīng)的后續(xù)需要的變量等熄驼。(重點(diǎn))
靜態(tài)塊
通過代碼注析的方式瓜贾,對(duì)每個(gè)方法進(jìn)行了解學(xué)習(xí)祭芦。參考中文注析。
提供的方法都是給內(nèi)部使用胃夏。
static {
Internal.instance = new Internal() {
//提供靜態(tài)方法對(duì)頭部信息進(jìn)行處理
@Override public void addLenient(Headers.Builder builder, String line) {
builder.addLenient(line);
}
@Override public void addLenient(Headers.Builder builder, String name, String value) {
builder.addLenient(name, value);
}
//寫入緩存(響應(yīng)數(shù)據(jù)緩存)
@Override public void setCache(OkHttpClient.Builder builder, InternalCache internalCache) {
builder.setInternalCache(internalCache);
}
//把連接變成空閑狀態(tài)
@Override public boolean connectionBecameIdle(
ConnectionPool pool, RealConnection connection) {
return pool.connectionBecameIdle(connection);
}
//從緩存中獲取有效的連接, 從內(nèi)存的ConnectiongPool中的Deque讀取
@Override public RealConnection get(ConnectionPool pool, Address address,
StreamAllocation streamAllocation, Route route) {
return pool.get(address, streamAllocation, route);
}
//對(duì)地址進(jìn)行校驗(yàn)
@Override public boolean equalsNonHost(Address a, Address b) {
return a.equalsNonHost(b);
}
//多路復(fù)用?(待研究 StreamAllocation 類)
@Override public Socket deduplicate(
ConnectionPool pool, Address address, StreamAllocation streamAllocation) {
return pool.deduplicate(address, streamAllocation);
}
//把連接添加到連接池當(dāng)中答恶,緩存起來
@Override public void put(ConnectionPool pool, RealConnection connection) {
pool.put(connection);
}
//獲取連接池里面的連接亥宿,可以有效剔除黑名單的線路砂沛,用于優(yōu)化連接效果碍庵。
@Override public RouteDatabase routeDatabase(ConnectionPool connectionPool) {
return connectionPool.routeDatabase;
}
//獲取response 的 code
@Override public int code(Response.Builder responseBuilder) {
return responseBuilder.code;
}
//sslSocket 的連接協(xié)議静浴,規(guī)范
@Override
public void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean isFallback) {
tlsConfiguration.apply(sslSocket, isFallback);
}
//檢查 Url 正確性苹享,可用性
@Override public HttpUrl getHttpUrlChecked(String url)
throws MalformedURLException, UnknownHostException {
return HttpUrl.getChecked(url);
}
//獲取到的 StreamAllocation(待研究) ,主要處理 Connections得问,Streams, Calls之間的關(guān)系
@Override public StreamAllocation streamAllocation(Call call) {
return ((RealCall) call).streamAllocation();
}
//新建web 相關(guān)的 Call
@Override public Call newWebSocketCall(OkHttpClient client, Request originalRequest) {
return new RealCall(client, originalRequest, true);
}
};
}
提供給外部調(diào)用的方法
略(提供的方法宫纬,大多都是獲取或者設(shè)置后面Bulider中變量。此處不做過多的解析)
Builder
在Builder里面蝌衔,你會(huì)發(fā)現(xiàn)很多的變量噩斟,而這些變量都是可以提供給你設(shè)置的亩冬,提供給后面邏輯調(diào)用的硅急。
下面是變量:
Dispatcher dispatcher;
Proxy proxy;
List<Protocol> protocols;
List<ConnectionSpec> connectionSpecs;
final List<Interceptor> interceptors = new ArrayList<>();
final List<Interceptor> networkInterceptors = new ArrayList<>();
ProxySelector proxySelector;
CookieJar cookieJar;
Cache cache;
InternalCache internalCache;
SocketFactory socketFactory;
SSLSocketFactory sslSocketFactory;
CertificateChainCleaner certificateChainCleaner;
HostnameVerifier hostnameVerifier;
CertificatePinner certificatePinner;
Authenticator proxyAuthenticator;
Authenticator authenticator;
ConnectionPool connectionPool;
Dns dns;
boolean followSslRedirects;
boolean followRedirects;
boolean retryOnConnectionFailure;
int connectTimeout;
int readTimeout;
int writeTimeout;
int pingInterval;
設(shè)置的默認(rèn)數(shù)據(jù):
public Builder() {
dispatcher = new Dispatcher();
protocols = DEFAULT_PROTOCOLS;
connectionSpecs = DEFAULT_CONNECTION_SPECS;
proxySelector = ProxySelector.getDefault();
cookieJar = CookieJar.NO_COOKIES;
socketFactory = SocketFactory.getDefault();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
certificatePinner = CertificatePinner.DEFAULT;
proxyAuthenticator = Authenticator.NONE;
authenticator = Authenticator.NONE;
connectionPool = new ConnectionPool();
dns = Dns.SYSTEM;
followSslRedirects = true;
followRedirects = true;
retryOnConnectionFailure = true;
connectTimeout = 10_000;
readTimeout = 10_000;
writeTimeout = 10_000;
pingInterval = 0;
}
Dispatcher, 分發(fā)請(qǐng)求荚板,內(nèi)部是有一個(gè)ThreadPoolExecutor
Proxy跪另, 代理連接,分三種類型直接(DIRECT)唧席、Http(http)嘲驾、SOCKS辽故。
ProxySelector誊垢,線路選擇器彤枢,對(duì)應(yīng)Okhttp的一大特點(diǎn),自行線路選擇壁晒,找到合適的連接
Cache秒咐, 真正的緩存實(shí)現(xiàn)
SSLSocketFactory携取, Https的支持
ConnectionPool帮孔, 連接池
Dns文兢,dns解析(Java實(shí)現(xiàn))
其他姆坚,如超時(shí)時(shí)間等
總結(jié):
OkhttpClient 類以Bulider的模式兼呵,構(gòu)建了相關(guān)的數(shù)據(jù)變量,提供后面使用碰辅。內(nèi)部結(jié)構(gòu)比較簡(jiǎn)單乎赴,主要還是初始化每個(gè)變量提供給后面的使用。在后面的學(xué)習(xí)分析中勉失,你會(huì)發(fā)現(xiàn)這個(gè)okHttpClient會(huì)無處不在乱凿,所以這里提前對(duì)其進(jìn)行學(xué)習(xí)分析徒蟆。
系列:
OKhttp源碼學(xué)習(xí)(一)—— 基本請(qǐng)求流程
OKhttp源碼學(xué)習(xí)(三)—— Request, RealCall
OKhttp源碼學(xué)習(xí)(四)——RetryAndFollowUpInterceptor攔截器分析
OKhttp源碼學(xué)習(xí)(五)—— BridgeInterceptor
OKhttp源碼學(xué)習(xí)(六)—— CacheInterceptor攔截器
OKhttp源碼學(xué)習(xí)(七)—— ConnectInterceptor攔截器
OKhttp源碼學(xué)習(xí)(八)——CallServerInterceptor攔截器
OKhttp源碼學(xué)習(xí)(九)—— 任務(wù)管理(Dispatcher)