connect方法分析
2017-09-20 20:34
316 查看
2,connect方法
HttpURLConnectionImpl的connect方法如下,public final void connect() throws IOException {
initHttpEngine();
boolean success;
do {
success = execute(false);
} while (!success);
}
首先调用initHttpEngine方法构造HttpEngine对象,然后调用execute方法进行连接,
2.1 构造HttpEngine对象
HttpURLConnectionImpl的initHttpEngine方法如下,httpEngine = newHttpEngine(method, null, null, null);
在newHttpEngine方法的最后会构造HttpEngine对象,
return new HttpEngine(engineClient, request, bufferRequestBody, true, false, connection, null, requestBody, priorResponse);
2.2 execute连接
调用流程图如下,HttpURLConnectionImpl的execute方法主要逻辑如下,
try { httpEngine.sendRequest();
调用HttpEngine对象的sendRequest发送请求,在该方法中会进行连接,
if (connection == null) { connect(); }
如果Connection对象connection为空,就调用connect方法进行连接, connect方法主要逻辑如下,
1,首先调用createAddress创建Address对象,
address = createAddress(client, networkRequest);
2,然后调用nextConnection方法创建Connection对象并进行连接,
connection = nextConnection();
nextConnection方法如下,
Connection connection = createNextConnection(); Internal.instance.connectAndSetOwner(client, connection, this, networkRequest);
首先调用createNextConnection方法构造Connection对象,然后调用Internal的connectAndSetOwner方法进行连接。
Internal是一个abstract类,定义如下,
public abstract class Internal {
有一个static变量instance,
public static Internal instance;
现在问题是哪个类继承了Internal并且实现了对应的方法呢?
在源码下面全局搜索,居然没有类继承Internal! 后来才发现,是在OkHttpClient中内部匿名实现的。
并且OkHttpClient是在HttpHandler的createHttpOkUrlFactory方法中构造的,
OkHttpClient client = new OkHttpClient();
Internal在OkHttpClient是一个static的代码块,
Internal.instance = new Internal() { @Override public Transport newTransport( Connection connection, HttpEngine httpEngine) throws IOException { return connection.newTransport(httpEngine); } •••
其connectAndSetOwner方法如下,
@Override public void connectAndSetOwner(OkHttpClient client, Connection connection, HttpEngine owner, Request request) throws RouteException { connection.connectAndSetOwner(client, owner, request); }
Connection的connectAndSetOwner逻辑如下,
if (!isConnected()) { List<ConnectionSpec> connectionSpecs = route.address.getConnectionSpecs(); connect(client.getConnectTimeout(), client.getReadTimeout(), client.getWriteTimeout(), request, connectionSpecs, client.getRetryOnConnectionFailure()); if (isSpdy()) { client.getConnectionPool().share(this); } client.routeDatabase().connected(getRoute()); }
如果还未连接,则调用connect方法进行连接。
2.3 socket连接
Connection的connect调用流程图如下,Connection的connect的主要逻辑如下,
1,构造SocketConnector对象,
SocketConnector socketConnector = new SocketConnector(this, pool);
2,构造SocketConnector的内部类ConnectedSocket对象,
if (route.address.getSslSocketFactory() != null) { // https:// communication connectedSocket = socketConnector.connectTls(connectTimeout, readTimeout, writeTimeout, request, route, connectionSpecs, connectionRetryEnabled); } else { // http:// communication. if (!connectionSpecs.contains(ConnectionSpec.CLEARTEXT)) { throw new RouteException( new UnknownServiceException( "CLEARTEXT communication not supported: " + connectionSpecs)); } connectedSocket = socketConnector.connectCleartext(connectTimeout, readTimeout, route); }
如果是https网址请求,就调用SocketConnector的connectTls方法创建ConnectedSocket对象,
否则就调用connectCleartext方法创建ConnectedSocket对象。
3,最后为Connection的变量赋值,
socket = connectedSocket.socket; handshake = connectedSocket.handshake; protocol = connectedSocket.alpnProtocol == null ? Protocol.HTTP_1_1 : connectedSocket.alpnProtocol;
SocketConnector的connectCleartext方法如下,
public ConnectedSocket connectCleartext(int connectTimeout, int readTimeout, Route route) throws RouteException { Socket socket = connectRawSocket(readTimeout, connectTimeout, route); return new ConnectedSocket(route, socket); }
首先调用connectRawSocket方法获取Socket对象,然后利用Socket对象构造并返回ConnectedSocket对象。
connectRawSocket方法的逻辑如下,
Platform platform = Platform.get();
try {
Proxy proxy = route.getProxy();
Address address = route.getAddress();
Socket socket;
if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP) {
socket = address.getSocketFactory().createSocket();
} else {
socket = new Socket(proxy);
}
socket.setSoTimeout(soTimeout);
platform.connectSocket(socket, route.getSocketAddress(), connectTimeout);
return socket;
} catch (IOException e) {
throw new RouteException(e);
}
首先获取Socket对象,然后调用Platform的connectSocket方法。
Platform这个类还是有点内涵,先来分析一下。
Platform的get方法如下,
public static Platform get() { return PLATFORM; }
PLATFORM是Platform对象,并且是调用findPlatform方法获取
private static final Platform PLATFORM = findPlatform();
findPlatform方法根据android 系统进行构造Platform对象,
1,如果是android 2.3以上版本,就构造Android对象,
••• return new Android(setUseSessionTickets, setHostname, trafficStatsTagSocket, trafficStatsUntagSocket, getAlpnSelectedProtocol, setAlpnProtocols);
当然Android是Platform的子类,并且是Platform的内部类。
2,否则就构造Platform对象,
return new Platform();
Android对象的connectSocket方法如下,
try { socket.connect(address, connectTimeout); } catch (SecurityException se) { // Before android 4.3, socket.connect could throw a SecurityException // if opening a socket resulted in an EACCES error. IOException ioException = new IOException("Exception in connect"); ioException.initCause(se); throw ioException; }
其实就是调用socket的connect方法进行连接,如果连接出错则抛出异常。
论述了这么多,本质就是构造Socket对象,并调用Socket对象的connect方法进行连接。
相关文章推荐
- greenplum全量恢复gprecoverseg -F出现Unable to connect to database时的相关分析及解决方法
- Android中的跨进程通信方法实例及特点分析(二):ContentProvider
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.24
- ios crash分析方法
- MySQL常用性能分析方法-profile,explain,索引
- Android 内存分析方法整理
- 防盗链的8种方法总结及分析
- OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析
- 定位服务应用的需求分析----微软分析方法
- 网络状态检测方法分析
- IE iframe的onload方法分析小结
- 在Activity的onCreate方法中显示PopupWindow导致异常的原因分析及解决方案
- Android中View绘制流程以及invalidate()等相关方法分析
- VxWorks系统异常分析方法
- Java之Collections工具类方法使用以及源码分析(三)
- (GPS移植三部曲)Linux下移植GPS应用程序之常见问题的分析与解决方法之一
- Pig_7. 常用的数据分析方法 -- 待完善
- 白盒测试覆盖方法举例分析
- nginx源码分析(19)-方法(3)
- js中匿名函数的创建与调用方法分析