while ((runnable = engine.getDelegatedTask()) != null)
runnable.run();
}
private Packet decode(Session session, Packet packet) throws Exception {
Buffer src = packet.getContent();
ByteBuffer srcBuffer = src.asByteBuffer();
int size = appBufferSize
* (int) Math.ceil((double) (src.remaining() + 1)
/ netBufferSize);
Buffer dest = BufferFactory.allocate(size);
ByteBuffer destBuffer = dest.asByteBuffer();
boolean hasAppData = false;
// decode
while (true) {
SSLEngineResult result = engine.unwrap(srcBuffer, destBuffer);
Status status = result.getStatus();
if (status == Status.OK) {
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING) {
hasAppData = true;
if (!srcBuffer.hasRemaining()) // decode completed
break;
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
runTask();
} else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
// unwrap again
} else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
// need send some handshake packet
session.flush(new HandshakePacket());
break;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
handshakeCompleted();
}
} else if (status == Status.BUFFER_UNDERFLOW) {
// need more content, wait next time
break;
} else if (status == Status.BUFFER_OVERFLOW) {
// increase dest capacity
destBuffer.flip();
Buffer newDest = BufferFactory.allocate(
appBufferSize + destBuffer.remaining()).put(destBuffer);
dest.release();
dest = newDest;
destBuffer = dest.asByteBuffer();
} else if (status == Status.CLOSED) {