ReplayingDecoder хочет собрать несколько входящих сообщений и передать их следующему обработчику - PullRequest
0 голосов
/ 25 января 2019

Ниже приведен код на стороне сервера.Реализовать простой replayingDecoder, чтобы разделить входящие сообщения на несколько частей. Я хочу использовать его в качестве препроцессора для бизнес-логики в следующем обработчике.

Однако после того, как декодирование выполнено в TestReplayingDecoder, метод channRead другого Хандлера не выполняется.Метод channelReadComplete выполняется.Почему?

Точно так же, когда вы реализуете ReplayingDecode с checkPoint и состоянием, я подтвердил, что следующий обработчик channelRead работает хорошо.

.childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new LoggingHandler(LogLevel.INFO));
                        pipeline.addLast(new TestReplayingDecoder());
                        pipeline.addLast(new AnotherHandler());
                    }
                });



@Slf4j
public class TestReplayingDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        out.add(in.readBytes(in.readInt()));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}



@Slf4j
@Sharable
public class AnotherHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String msgString = (String) msg;
        ctx.writeAndFlush(Unpooled.copiedBuffer(makeResponse(msg), CharsetUtil.US_ASCII));
    }
}

log ===================

nioEventLoopGroup-2-1 [-:-] [id: 0x2e31ca6f, L:/0:0:0:0:0:0:0:0:21103] READ: [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671]
nioEventLoopGroup-2-1 [-:-] [id: 0x2e31ca6f, L:/0:0:0:0:0:0:0:0:21103] READ COMPLETE
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] REGISTERED
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] ACTIVE
nioEventLoopGroup-3-2 [-:-] AnotherHandler.channelActive !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ: 1024B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 32 39 36 36 41 42 41 30 37 41 52 50 30 32 30 30 |.                |
~ ~
+--------+-------------------------------------------------+----------------+
nioEventLoopGroup-3-2 [-:-] TestReplayingDecoder.decode !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ: 1024B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |                |
~ ~ 
+--------+-------------------------------------------------+----------------+
nioEventLoopGroup-3-2 [-:-] TestReplayingDecoder.decode !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ: 922B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |                |
~ ~ 
+--------+-------------------------------------------------+----------------+
nioEventLoopGroup-3-2 [-:-] TestReplayingDecoder.decode !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] READ COMPLETE
nioEventLoopGroup-3-2 [-:-] AnotherHandler.channelReadComplete !!
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] WRITE: 0B
nioEventLoopGroup-3-2 [-:-] [id: 0x653410de, L:/127.0.0.1:21103 - R:/127.0.0.1:61671] FLUSH

1 Ответ

0 голосов
/ 30 января 2019

Я вполне уверен, что ваш channelRead(...) метод вызывается, но он вызовет ClassCastException, когда вы попытаетесь привести сообщение к String, тогда как это будет ByteBuf, так как вы производите ByteBuf с ваш TestReplayingDecoder.

...