1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| class ExchangeFinder( private val transmitter: Transmitter, private val connectionPool: RealConnectionPool, private val address: Address, private val call: Call, private val eventListener: EventListener ) { * Returns a connection to host a new stream. This prefers the existing connection if it exists, * then the pool, finally building a new connection. */ @Throws(IOException::class) private fun findConnection( connectTimeout: Int, readTimeout: Int, writeTimeout: Int, pingIntervalMillis: Int, connectionRetryEnabled: Boolean ): RealConnection { var foundPooledConnection = false var result: RealConnection? = null var selectedRoute: Route? = null var releasedConnection: RealConnection? val toClose: Socket? synchronized(connectionPool) { if (transmitter.isCanceled) throw IOException("Canceled") hasStreamFailure = false
releasedConnection = transmitter.connection toClose = if (transmitter.connection != null && transmitter.connection!!.noNewExchanges) { transmitter.releaseConnectionNoEvents() } else { null }
if (transmitter.connection != null) { result = transmitter.connection releasedConnection = null }
if (result == null) { if (connectionPool.transmitterAcquirePooledConnection(address, transmitter, null, false)) { foundPooledConnection = true result = transmitter.connection } else if (nextRouteToTry != null) { selectedRoute = nextRouteToTry nextRouteToTry = null } else if (retryCurrentRoute()) { selectedRoute = transmitter.connection!!.route() } } } toClose?.closeQuietly()
if (releasedConnection != null) { eventListener.connectionReleased(call, releasedConnection!!) } if (foundPooledConnection) { eventListener.connectionAcquired(call, result!!) } if (result != null) { return result!! }
var newRouteSelection = false if (selectedRoute == null && (routeSelection == null || !routeSelection!!.hasNext())) { newRouteSelection = true routeSelection = routeSelector.next() }
var routes: List<Route>? = null synchronized(connectionPool) { if (transmitter.isCanceled) throw IOException("Canceled")
if (newRouteSelection) { routes = routeSelection!!.routes if (connectionPool.transmitterAcquirePooledConnection( address, transmitter, routes, false)) { foundPooledConnection = true result = transmitter.connection } }
if (!foundPooledConnection) { if (selectedRoute == null) { selectedRoute = routeSelection!!.next() }
result = RealConnection(connectionPool, selectedRoute!!) connectingConnection = result } }
if (foundPooledConnection) { eventListener.connectionAcquired(call, result!!) return result!! }
result!!.connect( connectTimeout, readTimeout, writeTimeout, pingIntervalMillis, connectionRetryEnabled, call, eventListener ) connectionPool.routeDatabase.connected(result!!.route())
var socket: Socket? = null synchronized(connectionPool) { connectingConnection = null if (connectionPool.transmitterAcquirePooledConnection(address, transmitter, routes, true)) { result!!.noNewExchanges = true socket = result!!.socket() result = transmitter.connection } else { connectionPool.put(result!!) transmitter.acquireConnectionNoEvents(result!!) } } socket?.closeQuietly()
eventListener.connectionAcquired(call, result!!) return result!! } }
|