OkHttp and OkHttp
After the introduction of OkHttp in the previous sections, we have learned about the general process of OkHttp, we know that in addition to those key classes, OkHttp also has many key classes that provide important functions throughout the network request process. Then we will analyze these classes.
OkHttp highlights:
RouteSelector: Route Selector
In OKhttp, the function is to return an available Route object.
RouteSelector initialization:
RouteSelector class is initialized in StreamAllocation (Stream configuration:
The code above shows that the RouteSelector object needs the Address and RouteDatabase objects for initialization.
Address represents a specification for connecting to the source server. For simple connections, This is the host name and port of the server. If there is a proxy, this also includes proxy information, mainly including host name, port number, Dns, proxy server proxy and other elements.
The RouteSelector class maintains a proxy server set proxies:
When the client configures the OkttCilent object, it can create its own proxy server through the Builder object of Okhttp. the user-configured proxy server object proxy will be handed over to the Address object mentioned above, and then handed over to RouteSelector
Of course, if the user does not configure the Proxy Server Object, Okhttp uses ProxySelector. the select method of the ProxySelector object getDefault () returns the default Proxy server set. Of course, if no Proxy server is found, the Proxy is used. NO_PROXY, indicating that the current Address requests to the network completely bypass the proxy server and connect directly to the remote host
In fact, if you have read the Http authoritative guide, this class is actually a bit similar to the DNS redirection function, simply decide which ip address is selected in the ip address list of a host as the target host address.
Connection Pool ConnectionPool of Okhttp
Based on the previous analysis of Okhttp, we know that each http request will create a StreamAllocation object in the RetryAndFollowUpInterceptor interceptor, or simply each http request will generate a StreamAllocation object. In this case, if several requests obtain the same RealConnection object from the link pool, their relationship can be simply as shown in:
In fact, we can also find that when each request generates a StreamAllocation Object Request link, the first thing we need to do is not create a new RealConnection object, but retrieve existing and reusable RealConnection from the link pool, if no available link is found, a new RealConnection will be created. (Of course, you must put the newly created RealConnection in the connection pool for reuse of other requests)
RealConnection is the object used by Okhttp to formally initiate a network request. How does one determine whether a RealConnection can enable StreamAllocation?How about it?
In fact, we can think of conditions for reusing a link, such as the same address, the same port, and the load of a link line that does not exceed the specified number of loads. Okhttp imposes more restrictions on reuse conditions. For details, refer to the isEligible code.
The iseligble method can basically be divided into two parts:
1. The Address object carried by StreamAllocation matches the Address of RealConnection;
2. If 1 is not met, the Route carried by StreamAllocation (selected by RouteSelecor) matches the Route of RealConnection (specifically the information of the SocketAddress table ). StreamAllocation:
In the previous source code analysis, we know that RealConnection is the object used by Okhttp to formally initiate a network request. Next, we can analyze the specific functions of RealConnection:
RealConnection: the object used by Okhttp to formally initiate a network request
In short, the connect () method mainly implements the following tasks:
1. If the current route request is https and the Proxy. Type. HTTP Proxy is used, a tunnel link is enabled.
2. If 1 is not true, call the connectSocket method to create a Socket link.
3. Call establishProtocol to build the Protocol
The function of the connectSocket method is to open a Socket link. The steps are as follows:
1. If no proxy is used or an HTTP proxy is used, use socketFactory of the Address object to create a Socket
2. If the SOCKET proxy is used, a Socket is initialized.
3. Calling Platform. get (). connectSocket is actually to call the connect Method of socket to open a connection.
4. Then, the inputStream of the socket input stream is handed over to Okio. buffer (Okio. source (rawSocket), that is, the Source object, and the output stream outputStream object is handed over to Okio. sink object. The number of read/write operations by Okio on the connection. Here, you only need to simply understand that the source object is to read service data, and the sink is to send data to the server.
At this point, we have already introduced the key classes in OkHttp network requests.
However, we know that the network framework commonly used for android is OkHttp + javasfit + RxJava. Next, we will look forward to my explanation of RxJava source code.