1175 |
1175 |
1176 // Optimization for reading SSL encrypted data |
1176 // Optimization for reading SSL encrypted data |
1177 // -------------------------------------------- |
1177 // -------------------------------------------- |
1178 |
1178 |
1179 // Returns a BufferSupplier that can be used for reading |
1179 // Returns a BufferSupplier that can be used for reading |
1180 // encrypted bytes of the socket. These buffers can then |
1180 // encrypted bytes of the channel. These buffers can then |
1181 // be recycled by the SSLFlowDelegate::Reader after their |
1181 // be recycled by the SSLFlowDelegate::Reader after their |
1182 // content has been copied in the SSLFlowDelegate::Reader |
1182 // content has been copied in the SSLFlowDelegate::Reader |
1183 // readBuf. |
1183 // readBuf. |
1184 // Because allocating, reading, copying, and recycling |
1184 // Because allocating, reading, copying, and recycling |
1185 // all happen in the SelectorManager thread, |
1185 // all happen in the SelectorManager thread, |
1187 // the SSL connections managed by this client. |
1187 // the SSL connections managed by this client. |
1188 BufferSupplier getSSLBufferSupplier() { |
1188 BufferSupplier getSSLBufferSupplier() { |
1189 return sslBufferSupplier; |
1189 return sslBufferSupplier; |
1190 } |
1190 } |
1191 |
1191 |
1192 // An implementation of BufferSupplier that manage a pool of |
1192 // An implementation of BufferSupplier that manages a pool of |
1193 // maximum 3 direct byte buffers (SocketTube.MAX_BUFFERS) that |
1193 // maximum 3 direct byte buffers (SocketTube.MAX_BUFFERS) that |
1194 // are used for reading encrypted bytes off the socket before |
1194 // are used for reading encrypted bytes off the channel before |
1195 // copying before unwrapping. |
1195 // copying and subsequent unwrapping. |
1196 private static final class SSLDirectBufferSupplier implements BufferSupplier { |
1196 private static final class SSLDirectBufferSupplier implements BufferSupplier { |
1197 private static final int POOL_SIZE = SocketTube.MAX_BUFFERS; |
1197 private static final int POOL_SIZE = SocketTube.MAX_BUFFERS; |
1198 private final ByteBuffer[] pool = new ByteBuffer[POOL_SIZE]; |
1198 private final ByteBuffer[] pool = new ByteBuffer[POOL_SIZE]; |
1199 private final HttpClientImpl client; |
1199 private final HttpClientImpl client; |
1200 private final Logger debug; |
1200 private final Logger debug; |
1201 private int tail, count; // no need for volatile: only accessed in SM thread. |
1201 private int tail, count; // no need for volatile: only accessed in SM thread. |
1202 public SSLDirectBufferSupplier(HttpClientImpl client) { |
1202 |
|
1203 SSLDirectBufferSupplier(HttpClientImpl client) { |
1203 this.client = Objects.requireNonNull(client); |
1204 this.client = Objects.requireNonNull(client); |
1204 this.debug = client.debug; |
1205 this.debug = client.debug; |
1205 } |
1206 } |
1206 |
1207 |
1207 // get a buffer from the pool, or allocate a new one if needed. |
1208 // Gets a buffer from the pool, or allocates a new one if needed. |
1208 @Override |
1209 @Override |
1209 public ByteBuffer get() { |
1210 public ByteBuffer get() { |
1210 assert client.isSelectorThread(); |
1211 assert client.isSelectorThread(); |
1211 assert tail <= POOL_SIZE : "allocate tail is " + tail; |
1212 assert tail <= POOL_SIZE : "allocate tail is " + tail; |
1212 ByteBuffer buf; |
1213 ByteBuffer buf; |
1231 assert tail < POOL_SIZE; |
1232 assert tail < POOL_SIZE; |
1232 assert tail >= 0; |
1233 assert tail >= 0; |
1233 return buf; |
1234 return buf; |
1234 } |
1235 } |
1235 |
1236 |
1236 // return the buffer to the pool |
1237 // Returns the given buffer to the pool. |
1237 @Override |
1238 @Override |
1238 public void recycle(ByteBuffer buffer) { |
1239 public void recycle(ByteBuffer buffer) { |
1239 assert client.isSelectorThread(); |
1240 assert client.isSelectorThread(); |
1240 assert buffer.isDirect(); |
1241 assert buffer.isDirect(); |
1241 assert !buffer.hasRemaining(); |
1242 assert !buffer.hasRemaining(); |