SslHandler.handshake()的ChannelFutureListener.operationComplete未在Android上调用

时间:2013-09-06 17:10:28

标签: android ssl netty handshake

我在Android应用中使用netty-3.6.6 SSL。握手()实际上已经完成(Android应用程序能够向/从SSL服务器发送/接收数据)但是从不调用operationComplete。我需要调用它来执行某些任务。

我错过了什么或做错了什么?谢谢。

以下是设置和代码片段。

@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 个答案:

答案 0 :(得分:0)

netty可能无法处理返回到正确调用SslHandler.handshake()的应用程序的ChannelFuture状态。我将hsFuture.setSuccess()添加到SslHandler.handshake()并调用我的operationComplete。

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) {