|
18 | 18 | import io.netty.buffer.Unpooled; |
19 | 19 | import io.netty.channel.Channel; |
20 | 20 | import io.netty.channel.ChannelFuture; |
21 | | -import io.netty.channel.ChannelFutureListener; |
22 | 21 | import io.netty.channel.ChannelHandler; |
23 | 22 | import io.netty.channel.ChannelHandlerContext; |
24 | 23 | import io.netty.channel.ChannelInboundHandlerAdapter; |
@@ -287,7 +286,7 @@ public ChannelFuture handshake(Channel channel) { |
287 | 286 | * the {@link ChannelPromise} to be notified when the opening handshake is sent |
288 | 287 | */ |
289 | 288 | public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) { |
290 | | - ChannelPipeline pipeline = channel.pipeline(); |
| 289 | + final ChannelPipeline pipeline = channel.pipeline(); |
291 | 290 | HttpResponseDecoder decoder = pipeline.get(HttpResponseDecoder.class); |
292 | 291 | if (decoder == null) { |
293 | 292 | HttpClientCodec codec = pipeline.get(HttpClientCodec.class); |
@@ -322,26 +321,22 @@ public final ChannelFuture handshake(Channel channel, final ChannelPromise promi |
322 | 321 |
|
323 | 322 | FullHttpRequest request = newHandshakeRequest(); |
324 | 323 |
|
325 | | - channel.writeAndFlush(request).addListener(new ChannelFutureListener() { |
326 | | - @Override |
327 | | - public void operationComplete(ChannelFuture future) { |
328 | | - if (future.isSuccess()) { |
329 | | - ChannelPipeline p = future.channel().pipeline(); |
330 | | - ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class); |
331 | | - if (ctx == null) { |
332 | | - ctx = p.context(HttpClientCodec.class); |
333 | | - } |
334 | | - if (ctx == null) { |
335 | | - promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + |
336 | | - "an HttpRequestEncoder or HttpClientCodec")); |
337 | | - return; |
338 | | - } |
339 | | - p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder()); |
340 | | - |
341 | | - promise.setSuccess(); |
342 | | - } else { |
343 | | - promise.setFailure(future.cause()); |
| 324 | + channel.writeAndFlush(request).addListener(future -> { |
| 325 | + if (future.isSuccess()) { |
| 326 | + ChannelHandlerContext ctx = pipeline.context(HttpRequestEncoder.class); |
| 327 | + if (ctx == null) { |
| 328 | + ctx = pipeline.context(HttpClientCodec.class); |
| 329 | + } |
| 330 | + if (ctx == null) { |
| 331 | + promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + |
| 332 | + "an HttpRequestEncoder or HttpClientCodec")); |
| 333 | + return; |
344 | 334 | } |
| 335 | + pipeline.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder()); |
| 336 | + |
| 337 | + promise.setSuccess(); |
| 338 | + } else { |
| 339 | + promise.setFailure(future.cause()); |
345 | 340 | } |
346 | 341 | }); |
347 | 342 | return promise; |
@@ -687,32 +682,23 @@ private ChannelFuture close0(final ChannelOutboundInvoker invoker, final Channel |
687 | 682 | return promise; |
688 | 683 | } |
689 | 684 |
|
690 | | - promise.addListener(new ChannelFutureListener() { |
691 | | - @Override |
692 | | - public void operationComplete(ChannelFuture future) { |
693 | | - // If flush operation failed, there is no reason to expect |
694 | | - // a server to receive CloseFrame. Thus this should be handled |
695 | | - // by the application separately. |
696 | | - // Also, close might be called twice from different threads. |
697 | | - if (future.isSuccess() && channel.isActive() && |
698 | | - FORCE_CLOSE_INIT_UPDATER.compareAndSet(handshaker, 0, 1)) { |
699 | | - final Future<?> forceCloseFuture = channel.eventLoop().schedule(new Runnable() { |
700 | | - @Override |
701 | | - public void run() { |
702 | | - if (channel.isActive()) { |
703 | | - invoker.close(); |
704 | | - forceCloseComplete = true; |
705 | | - } |
| 685 | + promise.addListener(future -> { |
| 686 | + // If flush operation failed, there is no reason to expect |
| 687 | + // a server to receive CloseFrame. Thus this should be handled |
| 688 | + // by the application separately. |
| 689 | + // Also, close might be called twice from different threads. |
| 690 | + if (future.isSuccess() && channel.isActive() && |
| 691 | + FORCE_CLOSE_INIT_UPDATER.compareAndSet(handshaker, 0, 1)) { |
| 692 | + final Future<?> forceCloseFuture = channel.eventLoop().schedule(new Runnable() { |
| 693 | + @Override |
| 694 | + public void run() { |
| 695 | + if (channel.isActive()) { |
| 696 | + invoker.close(); |
| 697 | + forceCloseComplete = true; |
706 | 698 | } |
707 | | - }, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS); |
708 | | - |
709 | | - channel.closeFuture().addListener(new ChannelFutureListener() { |
710 | | - @Override |
711 | | - public void operationComplete(ChannelFuture future) throws Exception { |
712 | | - forceCloseFuture.cancel(false); |
713 | | - } |
714 | | - }); |
715 | | - } |
| 699 | + } |
| 700 | + }, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS); |
| 701 | + channel.closeFuture().addListener(f -> forceCloseFuture.cancel(false)); |
716 | 702 | } |
717 | 703 | }); |
718 | 704 | return promise; |
|