From b9697068328bfc6748d5b6f32cfce7c9bf8ec456 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 2 Aug 2011 15:38:58 +0900 Subject: [PATCH] NETTY-428 SslHandler does not trigger an exceptionCaught event for some handshake failure cases. * Made sure SslHandler.handshake() and SslHandler.close() trigger an exceptionCaught event when failed --- .../SecureChatClientPipelineFactory.java | 2 +- .../jboss/netty/handler/ssl/SslHandler.java | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jboss/netty/example/securechat/SecureChatClientPipelineFactory.java b/src/main/java/org/jboss/netty/example/securechat/SecureChatClientPipelineFactory.java index 1bf5a15a61..596d64f95c 100644 --- a/src/main/java/org/jboss/netty/example/securechat/SecureChatClientPipelineFactory.java +++ b/src/main/java/org/jboss/netty/example/securechat/SecureChatClientPipelineFactory.java @@ -50,7 +50,7 @@ public class SecureChatClientPipelineFactory implements SSLEngine engine = SecureChatSslContextFactory.getClientContext().createSSLEngine(); - engine.setUseClientMode(true); + //engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); diff --git a/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java b/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java index fef89bbaec..58153c3429 100644 --- a/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java +++ b/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java @@ -329,6 +329,8 @@ public class SslHandler extends FrameDecoder ChannelHandlerContext ctx = this.ctx; Channel channel = ctx.getChannel(); ChannelFuture handshakeFuture; + Exception exception = null; + synchronized (handshakeLock) { if (handshaking) { return this.handshakeFuture; @@ -340,15 +342,22 @@ public class SslHandler extends FrameDecoder handshakeFuture = this.handshakeFuture = future(channel); } catch (Exception e) { handshakeFuture = this.handshakeFuture = failedFuture(channel, e); + exception = e; } } } - try { - wrapNonAppData(ctx, channel); - } catch (SSLException e) { - handshakeFuture.setFailure(e); + if (exception == null) { // Began handshake successfully. + try { + wrapNonAppData(ctx, channel); + } catch (SSLException e) { + fireExceptionCaught(ctx, e); + handshakeFuture.setFailure(e); + } + } else { // Failed to initiate handshake. + fireExceptionCaught(ctx, exception); } + return handshakeFuture; } @@ -371,6 +380,7 @@ public class SslHandler extends FrameDecoder engine.closeOutbound(); return wrapNonAppData(ctx, channel); } catch (SSLException e) { + fireExceptionCaught(ctx, e); return failedFuture(channel, e); } }