Netty 5,多个客户端

Netty 5, multiple clients

我刚刚将我的 Netty 3 软件转换为 5,我遇到了一个问题。对于一个客户,一切都很好。我可以使用两个解码器(它们可以正确切换)并允许我登录。

当我尝试连接另一个客户端时出现问题,无论第一个客户端是否仍然连接。第一个客户端保持连接绝对正常,但第二个客户端根本无法连接(没有通过第一个解码器)。

Apr 26, 2015 4:51:16 AM io.netty.channel.ChannelHandlerInvokerUtil notifyOutboundHandlerException
WARNING: Failed to fail the promise because it's done already: DefaultChannelPromise@7f3386c4(failure(io.netty.util.IllegalReferenceCountException: refCnt: 0)
io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:111)
    at io.netty.util.ReferenceCountUtil.release(ReferenceCountUtil.java:84)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:791)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1278)
    at io.netty.channel.ChannelHandlerInvokerUtil.invokeWriteNow(ChannelHandlerInvokerUtil.java:158)
    at io.netty.channel.DefaultChannelHandlerInvoker.invokeWrite(DefaultChannelHandlerInvoker.java:337)
    at io.netty.channel.PausableChannelEventExecutor.invokeWrite(PausableChannelEventExecutor.java:132)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:539)
    at io.netty.handler.timeout.IdleStateHandler.write(IdleStateHandler.java:267)
    at io.netty.channel.ChannelHandlerInvokerUtil.invokeWriteNow(ChannelHandlerInvokerUtil.java:158)
    at io.netty.channel.DefaultChannelHandlerInvoker.invokeWrite(DefaultChannelHandlerInvoker.java:337)
    at io.netty.channel.PausableChannelEventExecutor.invokeWrite(PausableChannelEventExecutor.java:132)
    at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:539)
    at io.netty.channel.ChannelHandlerAdapter.write(ChannelHandlerAdapter.java:265)
    at io.netty.channel.ChannelHandlerInvokerUtil.invokeWriteNow(ChannelHandlerInvokerUtil.java:158)
    at io.netty.channel.DefaultChannelHandlerInvoker$WriteTask.run(DefaultChannelHandlerInvoker.java:440)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:328)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:116)
    at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
    at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
    at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
    at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
    at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
    at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)

这是我的 bootstrap:

EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .localAddress(43594)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast("timeout", new IdleStateHandler(15, 0, 0));
                        p.addLast("decoder", new RS2Decoder());
                        p.addLast("handler", new ChannelHandler());
                    }
                }
        ).bind();

第一个解码器:

http://pastebin.com/9FWzXSVE

第二个解码器:

http://pastebin.com/n2D8RDgn

我的频道处理程序:

http://pastebin.com/4mdXe5jV

解决方案是通过 @Sharable 注释将处理程序标记为可共享。