*
* @return a {@link ChannelPromise} which is notified when the handshake
* succeeds or fails.
*/
public ChannelFuture handshake(final ChannelPromise promise) {
final ChannelHandlerContext ctx = this.ctx;
final ScheduledFuture<?> timeoutFuture;
if (handshakeTimeoutMillis > 0) {
timeoutFuture = ctx.executor().schedule(new Runnable() {
@Override
public void run() {
if (promise.isDone()) {
return;
}
SSLException e = new SSLException("handshake timed out");
if (promise.tryFailure(e)) {
ctx.fireExceptionCaught(e);
ctx.close();
}
}
}, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
} else {
timeoutFuture = null;
}
ctx.executor().execute(new Runnable() {
@Override
public void run() {
try {
if (timeoutFuture != null) {
timeoutFuture.cancel(false);
}
engine.beginHandshake();
handshakePromises.add(promise);
flush0(ctx, ctx.newPromise(), true);
} catch (Exception e) {
if (promise.tryFailure(e)) {
ctx.fireExceptionCaught(e);
ctx.close();
}
}
}
});