@Override public int drain(ByteBuffer dst) throws IOException { int cnt = 0; if (buffer != null) { ByteBuffer src = buffer.getReadable(); while (src.hasRemaining() && dst.hasRemaining()) { dst.put(src.get()); cnt++; } if (buffer.bytes() == 0) { buffer = null; } } return cnt; } }
private HandshakeResult writeKey() throws IOException { if (output.bytes() == 0) { output.getWritable(1).put(myKey); } if (channel.write(output.getReadable()) == 0) { return HandshakeResult.NEED_WRITE; } return HandshakeResult.DONE; } private HandshakeResult perform(OP op) throws IOException {
@Override public int read(ByteBuffer dst) throws IOException { if (input.bytes() == 0) { if (channel.read(input.getWritable(CHUNK_SIZE)) == -1) { return -1; // EOF } } return drain(dst); } @Override public int drain(ByteBuffer dst) throws IOException {
@Override public FlushResult flush() throws IOException { verifyHandshakeCompleted(); channelWrite(); return wrapBuffer.bytes() > 0 ? FlushResult.NEED_WRITE : FlushResult.DONE; }
public void injectReadData(Buffer data) { unwrapBuffer.getWritable(data.bytes()).put(data.getReadable()); }
@Override public int write(ByteBuffer src) throws IOException { verifyHandshakeCompleted(); if (flush() == FlushResult.NEED_WRITE) return 0; int totalBytesWrapped = 0; int bytesWrapped; do { bytesWrapped = applicationDataWrap(src); totalBytesWrapped += bytesWrapped; } while (bytesWrapped > 0 && wrapBuffer.bytes() < sessionPacketBufferSize); return totalBytesWrapped; }
switch (sslEngine.getHandshakeStatus()) { case NOT_HANDSHAKING: if (wrapBuffer.bytes() > 0) return HandshakeState.NEED_WRITE; sslEngine.setEnableSessionCreation(false); // disable renegotiation handshakeDummyBuffer = null; if (wrapBuffer.bytes() > 0) return HandshakeState.NEED_WRITE; if (!handshakeUnwrap()) return HandshakeState.NEED_READ; break;
@Override public HandshakeResult handshake() throws IOException { if (factory != null) { if (channel().read(buffer.getWritable(SNOOP_SIZE)) == -1) { throw new IOException("jrt: Connection closed by peer during tls detection"); } if (buffer.bytes() < SNOOP_SIZE) { return HandshakeResult.NEED_READ; } byte[] data = new byte[SNOOP_SIZE]; ByteBuffer src = buffer.getReadable(); for (int i = 0; i < SNOOP_SIZE; i++) { data[i] = src.get(i); } if (looksLikeTlsToMe(data)) { TlsCryptoSocket tlsSocket = factory.createCryptoSocket(channel(), true); tlsSocket.injectReadData(buffer); socket = tlsSocket; return socket.handshake(); } else { metrics.incrementServerUnencryptedConnectionsEstablished(); factory = null; } } return HandshakeResult.DONE; }