java.lang.Исключение исключения IndexOutOfBoundsException с использованием Netty в пакетной системе

#java #networking #netty #packet

Вопрос:

В настоящее время я программирую систему, которая взаимодействует с помощью пакетов. Это тоже работает. У меня есть один сервер и теоретически бесконечное количество клиентов. Когда я запускаю только один клиент, он работает очень хорошо, но когда я запускаю несколько, я всегда получаю java.lang.IndexOutOfBoundsException исключение после того, как программа проработала 2-3 минуты.

Исключение

 WARNUNG: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(77)   length(36) exceeds writerIndex(112): PooledUnsafeDirectByteBuf(ridx: 77, widx: 112, cap: 112)
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:478)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
        at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
        at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:795)
        at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:480)
        at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.IndexOutOfBoundsException: readerIndex(77)   length(36) exceeds writerIndex(112): PooledUnsafeDirectByteBuf(ridx: 77, widx: 112, cap: 112)
        at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1442)
        at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1428)
        at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:895)
        at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:903)
        at eu.diamondcloudservice.wrapper.packets.incoming.PacketInCloudServerConnect.read(PacketInCloudServerConnect.java:79)
        at eu.diamondcloudservice.wrapper.network.PacketDecoder.decode(PacketDecoder.java:21)
        at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508)
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:447)
        ... 19 more
 

Сервер main(String[] args)

 EventLoopGroup eventLoopGroup = epoll ? (EventLoopGroup) new EpollEventLoopGroup() : (EventLoopGroup) new NioEventLoopGroup();
    try {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(eventLoopGroup);
        serverBootstrap.channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class);
        serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
            protected void initChannel(Channel channel) throws Exception {
                channel.pipeline().addLast(new NetworkHandler());
            }
        });
        channel = serverBootstrap.bind(networkConfiguration.getPort()).sync().channel();
        channel.closeFuture().syncUninterruptibly();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        eventLoopGroup.shutdownGracefully();
    }
 

Основной клиент(строка[] args):

 EventLoopGroup eventLoopGroup = epoll ? (EventLoopGroup) new EpollEventLoopGroup() : (EventLoopGroup) new NioEventLoopGroup();
    try {

        io.netty.bootstrap.Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventLoopGroup);
        bootstrap.channel(epoll ? EpollSocketChannel.class : NioSocketChannel.class);
        bootstrap.handler(new ChannelInitializer<Channel>() {
            protected void initChannel(Channel channel) throws Exception {
                channel.pipeline().addLast(new NetworkHandler());
            }
        });

        channel = bootstrap.connect(networkConfiguration.getMasterHostname(), networkConfiguration.getMasterPort()).sync().channel();
        packetCaller = new PacketCaller(channel);
        packetCaller.sendPacket(new PacketOutVerify(networkConfiguration.getWrapperName(), networkConfiguration.getKey()));
        channel.closeFuture().syncUninterruptibly();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        eventLoopGroup.shutdownGracefully();
    }
 

Канал активен в сетевом манипуляторе:

 public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.pipeline().addLast(new PacketEncoder());
    ctx.pipeline().addLast(new PacketDecoder());
    this.waitingForRegistration.add(ctx.channel());
}
 

Кодер пакетов

 @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> output) throws     Exception {
int id = byteBuf.readInt();
Class<? extends Packet> clazz = Wrapper.IN_PACKETS.get(id);
if (clazz == null) {
  throw new NullPointerException("Could not find packet by id "   id);
}

Packet packet = clazz.newInstance();
packet.read(byteBuf, ctx.channel());
}
 

Пакетный кодер

 public class PacketEncoder extends MessageToByteEncoder<Packet> {
@Override
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf output) throws Exception   {
  int id = Wrapper.OUT_PACKETS.indexOf(packet.getClass());
  if(id == -1)
    throw new NullPointerException("Couldn't find packet of "   packet.getClass().getName());

  output.writeInt(id);
  packet.write(output, ctx.channel());
}
}
 

Ответ №1:

Таким образом, существует множество проблем.

Прежде PacketDecoder всего, вам нужно сначала проверить, доступны ли для чтения 4 байта, прежде чем пытаться позвонить byteBuf.readInt() . Так что что-то вроде:

 protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf,      List<Object> output) throws Exception {
    if (byteBuf.readableBytes() < 4) {
        return;
    }
    int id = byteBuf.readInt();
    ...
}
 

Во-вторых, в вашем Packet.read() методе вам также необходимо убедиться, что для чтения достаточно байтов, и если не повторите попытку позже