Cleanup and simplify SslHandler
Motivation: SslHandler can be cleaned up a bit in terms of naming and duplicated code. Modifications: - Fix naming of arguments - Not schedule timeout event if not really needed - share some code and simplify Result: Cleaner code.
This commit is contained in:
parent
515f8964b4
commit
31cbb85073
@ -378,7 +378,7 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
/**
|
/**
|
||||||
* See {@link #close()}
|
* See {@link #close()}
|
||||||
*/
|
*/
|
||||||
public ChannelFuture close(final ChannelPromise future) {
|
public ChannelFuture close(final ChannelPromise promise) {
|
||||||
final ChannelHandlerContext ctx = this.ctx;
|
final ChannelHandlerContext ctx = this.ctx;
|
||||||
ctx.executor().execute(new Runnable() {
|
ctx.executor().execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -386,17 +386,16 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
outboundClosed = true;
|
outboundClosed = true;
|
||||||
engine.closeOutbound();
|
engine.closeOutbound();
|
||||||
try {
|
try {
|
||||||
write(ctx, Unpooled.EMPTY_BUFFER, future);
|
flush(ctx, promise);
|
||||||
flush(ctx);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (!future.tryFailure(e)) {
|
if (!promise.tryFailure(e)) {
|
||||||
logger.warn("{} flush() raised a masked exception.", ctx.channel(), e);
|
logger.warn("{} flush() raised a masked exception.", ctx.channel(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return future;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -883,8 +882,6 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (totalLength > 0) {
|
if (totalLength > 0) {
|
||||||
boolean decoded = false;
|
|
||||||
|
|
||||||
// The buffer contains one or more full SSL records.
|
// The buffer contains one or more full SSL records.
|
||||||
// Slice out the whole packet so unwrap will only be called with complete packets.
|
// Slice out the whole packet so unwrap will only be called with complete packets.
|
||||||
// Also directly reset the packetLength. This is needed as unwrap(..) may trigger
|
// Also directly reset the packetLength. This is needed as unwrap(..) may trigger
|
||||||
@ -897,13 +894,8 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
// See https://github.com/netty/netty/issues/1534
|
// See https://github.com/netty/netty/issues/1534
|
||||||
|
|
||||||
in.skipBytes(totalLength);
|
in.skipBytes(totalLength);
|
||||||
decoded = unwrap(ctx, in, startOffset, totalLength);
|
|
||||||
|
|
||||||
if (!firedChannelRead) {
|
firedChannelRead = unwrap(ctx, in, startOffset, totalLength) || firedChannelRead;
|
||||||
// Check first if firedChannelRead is not set yet as it may have been set in a
|
|
||||||
// previous decode(...) call.
|
|
||||||
firedChannelRead = decoded;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nonSslRecord) {
|
if (nonSslRecord) {
|
||||||
@ -1251,15 +1243,19 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
outboundClosed = true;
|
outboundClosed = true;
|
||||||
engine.closeOutbound();
|
engine.closeOutbound();
|
||||||
|
|
||||||
ChannelPromise closeNotifyFuture = ctx.newPromise();
|
ChannelPromise closeNotifyPromise = ctx.newPromise();
|
||||||
try {
|
try {
|
||||||
write(ctx, Unpooled.EMPTY_BUFFER, closeNotifyFuture);
|
flush(ctx, closeNotifyPromise);
|
||||||
flush(ctx);
|
|
||||||
} finally {
|
} finally {
|
||||||
safeClose(ctx, closeNotifyFuture, promise);
|
safeClose(ctx, closeNotifyPromise, promise);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
|
||||||
|
pendingUnencryptedWrites.add(Unpooled.EMPTY_BUFFER, promise);
|
||||||
|
flush(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
|
public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
@ -1410,6 +1406,7 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
}
|
}
|
||||||
|
|
||||||
final ScheduledFuture<?> timeoutFuture;
|
final ScheduledFuture<?> timeoutFuture;
|
||||||
|
if (!flushFuture.isDone()) {
|
||||||
if (closeNotifyTimeoutMillis > 0) {
|
if (closeNotifyTimeoutMillis > 0) {
|
||||||
// Force-close the connection if close_notify is not fully sent in time.
|
// Force-close the connection if close_notify is not fully sent in time.
|
||||||
timeoutFuture = ctx.executor().schedule(new Runnable() {
|
timeoutFuture = ctx.executor().schedule(new Runnable() {
|
||||||
@ -1423,6 +1420,9 @@ public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundH
|
|||||||
} else {
|
} else {
|
||||||
timeoutFuture = null;
|
timeoutFuture = null;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
timeoutFuture = null;
|
||||||
|
}
|
||||||
|
|
||||||
// Close the connection if close_notify is sent in time.
|
// Close the connection if close_notify is sent in time.
|
||||||
flushFuture.addListener(new ChannelFutureListener() {
|
flushFuture.addListener(new ChannelFutureListener() {
|
||||||
|
Loading…
Reference in New Issue
Block a user