|
47 | 47 | import org.checkerframework.checker.nullness.qual.Nullable; |
48 | 48 | import org.kitteh.irc.client.library.Client; |
49 | 49 | import org.kitteh.irc.client.library.event.connection.ClientConnectionClosedEvent; |
| 50 | +import org.kitteh.irc.client.library.event.connection.ClientConnectionEndedEvent; |
50 | 51 | import org.kitteh.irc.client.library.event.connection.ClientConnectionEstablishedEvent; |
51 | 52 | import org.kitteh.irc.client.library.event.connection.ClientConnectionFailedEvent; |
52 | 53 | import org.kitteh.irc.client.library.exception.KittehConnectionException; |
@@ -87,6 +88,7 @@ public final class NettyConnection implements ClientConnection { |
87 | 88 | private @Nullable ScheduledFuture<?> ping; |
88 | 89 |
|
89 | 90 | private volatile String lastMessage; |
| 91 | + private volatile Throwable lastCause; |
90 | 92 |
|
91 | 93 | private boolean alive = true; |
92 | 94 |
|
@@ -187,12 +189,17 @@ protected void channelRead0(ChannelHandlerContext ctx, String msg) { |
187 | 189 | // The presence of the two latter arguments enables SNI. |
188 | 190 | final SslHandler sslHandler = sslContext.newHandler(this.channel.alloc(), addr.getHost(), addr.getPort()); |
189 | 191 | sslHandler.handshakeFuture().addListener(handshakeFuture -> { |
190 | | - if (!handshakeFuture.isSuccess() && NettyConnection.this.client.getStsMachine().isPresent()) { |
191 | | - StsMachine machine = NettyConnection.this.client.getStsMachine().get(); |
192 | | - if (machine.getCurrentState() == StsClientState.STS_PRESENT_RECONNECTING) { |
193 | | - NettyConnection.this.shutdown(DefaultMessageType.STS_FAILURE, false); |
194 | | - machine.setCurrentState(StsClientState.STS_PRESENT_CANNOT_CONNECT); |
195 | | - throw new KittehStsException("Handshake failure, aborting STS-protected connection attempt.", handshakeFuture.cause()); |
| 192 | + this.lastCause = null; |
| 193 | + if (!handshakeFuture.isSuccess()) { |
| 194 | + if (NettyConnection.this.client.getStsMachine().isPresent()) { |
| 195 | + StsMachine machine = NettyConnection.this.client.getStsMachine().get(); |
| 196 | + if (machine.getCurrentState() == StsClientState.STS_PRESENT_RECONNECTING) { |
| 197 | + NettyConnection.this.shutdown(DefaultMessageType.STS_FAILURE, false); |
| 198 | + machine.setCurrentState(StsClientState.STS_PRESENT_CANNOT_CONNECT); |
| 199 | + throw new KittehStsException("Handshake failure, aborting STS-protected connection attempt.", handshakeFuture.cause()); |
| 200 | + } |
| 201 | + } else { |
| 202 | + this.lastCause = handshakeFuture.cause(); |
196 | 203 | } |
197 | 204 | } |
198 | 205 | }); |
@@ -227,7 +234,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
227 | 234 | this.ping.cancel(true); |
228 | 235 | } |
229 | 236 | NettyConnection.this.alive = false; |
230 | | - ClientConnectionClosedEvent event = new ClientConnectionClosedEvent(NettyConnection.this.client, NettyConnection.this.reconnect, future.cause(), this.lastMessage); |
| 237 | + ClientConnectionEndedEvent event; |
| 238 | + if (this.lastCause == null ) { |
| 239 | + event = new ClientConnectionClosedEvent(this.client, this.reconnect, future.cause(), this.lastMessage); |
| 240 | + } else { |
| 241 | + event = new ClientConnectionFailedEvent(this.client, this.reconnect, this.lastCause); |
| 242 | + } |
231 | 243 | NettyConnection.this.client.getEventManager().callEvent(event); |
232 | 244 | if (event.willAttemptReconnect()) { |
233 | 245 | this.scheduleReconnect(event.getReconnectionDelay()); |
|
0 commit comments