Netty 5 MessagePackカスタムプロトコルの問題

6931 ワード

1カスタムプロトコル
Headers {
// 
private byte cls;
// 
private byte msgCrc = 0x01;

// id
private long msgSessionId;

// , 
private int msgLength;

// 
private byte msgType;
}
Messages {

    Headers ;
    Object ;

    Object getBody() {
        ;
    }
    }

2 encoder
Encoder MessageToByteEncoder<Messages> {

    MessagePack ;
    [] ;

    encode(ChannelHandlerContext ctx, Messages msgs, ByteBuf out) Exception {

        = MessagePack();

        (msgs == || msgs.getHeaders() == ) {
            Exception();
        }
        = .write(msgs.getBody());

        out.writeByte(msgs.getHeaders().getMsgCrc());
        out.writeByte(msgs.getHeaders().getCls());
        out.writeInt(.);
        out.writeByte(msgs.getHeaders().getMsgType());

        (msgs.getBody() != ) {
            out.writeBytes();
        }
    }
}

3 decoder
Decoder ByteToMessageDecoder {

    Logger = LoggerFactory.(.getClass().getName());

    MessagePack ;
    Messages ;
    Headers ;
    Class ;

    decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) Exception {


        = MessagePack();
        = Messages();
        = Headers();
        msgCrc = in.readByte();
        cl = in.readByte();
        lg = in.readInt();

        (msgCrc != Tools.) {
            .debug();
            ;
        }

        (in.readableBytes() < lg) {
            in.resetReaderIndex();
            ;
        }
        in.markReaderIndex();
        .setMsgCrc(msgCrc);
        .setCls(cl);
        .setMsgLength(lg);
        .setMsgType(in.readByte());


        (cl == ) {
            = DictClass.;
        }


        [] array;
        length = in.readableBytes();
        array = [length];
        in.getBytes(in.readerIndex(), array, , length);
        .setBody(.read(array, ));
        .setHeaders();

        out.add();
    }
}

4 server
ChildChannelHander ChannelInitializer<SocketChannel> {

    initChannel(SocketChannel ch) Exception {
        ch.pipeline().addLast(Encoder());
        ch.pipeline().addLast(Decoder());
        ch.pipeline().addLast(FlowerpotLoginServiceHandlers());
        .debug();
    }
}

5 client
(ChannelInitializer<SocketChannel>() {
    initChannel(SocketChannel ch) Exception {
        ch.pipeline().addLast(Encoder());
        ch.pipeline().addLast(Decoder());
        ch.pipeline().addLast(FlowerpotLoginClientHandlers());
        .debug();
    }
}

エラーメッセージ:
15:04:22.650 [nioEventLoopGroup-1-6] WARN  i.n.channel.DefaultChannelPipeline - 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(34) + length(1) exceeds writerIndex(34): PooledUnsafeDirectByteBuf(ridx: 34, widx: 34, cap: 34)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:347) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:272) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelInactiveNow(ChannelHandlerInvokerUtil.java:56) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelInactive(DefaultChannelHandlerInvoker.java:92) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.PausableChannelEventExecutor.invokeChannelInactive(PausableChannelEventExecutor.java:71) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:366) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:295) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelInactiveNow(ChannelHandlerInvokerUtil.java:56) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelInactive(DefaultChannelHandlerInvoker.java:92) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.PausableChannelEventExecutor.invokeChannelInactive(PausableChannelEventExecutor.java:71) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:366) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:938) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.AbstractChannel$AbstractUnsafe$7.run(AbstractChannel.java:680) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:328) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(34) + length(1) exceeds writerIndex(34): PooledUnsafeDirectByteBuf(ridx: 34, widx: 34, cap: 34)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1138) ~[netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.buffer.AbstractByteBuf.readByte(AbstractByteBuf.java:542) ~[netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at io.netty.buffer.WrappedByteBuf.readByte(WrappedByteBuf.java:423) ~[netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
at com.senhe.flowerpots.service.coder.Decoder.decode(Decoder.java:39) ~[classes/:na]
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:316) [netty-all-5.0.0.Alpha2.jar:5.0.0.Alpha2]
... 21 common frames omitted