Java tutorial
/* * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package reactor.ipc.netty.tcp; import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.ssl.SslHandshakeCompletionEvent; import reactor.core.publisher.MonoSink; /** * @author Stephane Maldini */ final class SslReadHandler extends ChannelDuplexHandler { final MonoSink<?> sink; boolean handshakeDone; SslReadHandler(MonoSink<?> sink) { this.sink = sink; } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.read(); //consume handshake //super.channelActive(ctx); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { if (!handshakeDone) { ctx.read(); /* continue consuming. */ } super.channelReadComplete(ctx); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof SslHandshakeCompletionEvent) { handshakeDone = true; SslHandshakeCompletionEvent handshake = (SslHandshakeCompletionEvent) evt; if (handshake.isSuccess()) { ctx.fireChannelActive(); } else { sink.error(handshake.cause()); } } super.userEventTriggered(ctx, evt); } }