ChannelFutureListener.operationComplete of SslHandler.handshake() Not Being Called on Android

北城余情 提交于 2019-12-06 13:36:25

问题


I am using netty-3.6.6 SSL in my Android app. The handshake() is actually done(Android app is able to send/receive data to/from SSL server) but operationComplete never gets called. I need it getting called to perform some tasks.

Anything I missed or did wrong? Thank you.

Follows are the settings and the code piece.

@Override
public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pip = Channels.pipeline();
        SSLEngine engine = SslContextFactory.getClientContext().createSSLEngine();
        engine.setUseClientMode(true);
        SslHandler sslHandler = new SslHandler(engine);
        sslHandler.setIssueHandshake(false);
        pip.addLast("ssl", sslHandler);
        ...
        return pip;
}

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("channelConnected");
        SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
        if (sslHandler != null) {
            // Begin handshake.
            ChannelFuture handshakeFuture = sslHandler.handshake();
            handshakeFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {

                    if (!future.isSuccess()) {
                        System.out.println("handshake failed(" + future.getCause() + ")");
                    } else {
                        System.out.println("handshake OK");
                    }
                }
            });
        }
    }

}

回答1:


netty might not handle the state of ChannelFuture returned to my application which calls SslHandler.handshake() correctly. I added hsFuture.setSuccess() to SslHandler.handshake() and my operationComplete gets called.

public ChannelFuture handshake() {
        ...
        if (exception == null) { // Began handshake successfully.
            try {
                final ChannelFuture hsFuture = handshakeFuture;
                wrapNonAppData(ctx, channel).addListener(new ChannelFutureListener() {
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            Throwable cause = future.getCause();
                            hsFuture.setFailure(cause);

                            fireExceptionCaught(ctx, cause);
                            if (closeOnSSLException) {
                                Channels.close(ctx, future(channel));
                            }
                        } else {
                            hsFuture.setSuccess();
                        }
                    }
                });
            } catch (SSLException e) {


来源:https://stackoverflow.com/questions/18663061/channelfuturelistener-operationcomplete-of-sslhandler-handshake-not-being-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!