套接字消息被netty拆分 [英] socket messages being split by netty

查看:170
本文介绍了套接字消息被netty拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个基于netty 4的REST服务器.客户端处理程序如下所示.

I wrote a REST server based on netty 4. The client handler looks something like the following.

netty在msg中提供的字节缓冲区容量有所不同.当客户端消息大于缓冲区时,消息将被拆分.我发现,每个片段都会调用channelRead和ChannelReadComplete.我通常看到的是ByteBuf大约为512,消息大约为600.我得到前512个字节的channelRead,之后是它们的ChannelReadComplete,然后是剩下的100个字节的另一个channelRead和它们的channelReadComplete- 2条消息,而不是1条消息.

The bytebuffer capacity in the msg provided by netty varies. When the client message is larger than the buffer the message gets split. What I find is that both channelRead and ChannelReadComplete get called for each fragment. What I usually see is that the ByteBuf is around 512, and the message around 600. I get a channelRead for the first 512 bytes, followed by a ChannelReadComplete for them, and then another channelRead for the remaining 100 bytes and a channelReadComplete for them - 2 messages instead of 1.

我在这里找到了一些相关的问题,但是我想知道channelReadComplete的意义是什么?在每个channelRead之后是否真的调用了它?只要有字节可用,是否不应该在调用channelReadComplete之前读取它们?

I found a few related questions here, but I am wondering what is the point of channelReadComplete? Is it really called after every channelRead? As long as there are bytes available, shouldn't they be read in before channelReadComplete is called?

public class ClientHandler extends ChannelInboundHandlerAdapter {
    ....
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Report.debug("Read from client");
        ByteBuf buf = (ByteBuf) msg;
        String contents = buf.toString(io.netty.util.CharsetUtil.US_ASCII);
        ReferenceCountUtil.release(msg);

        ClientConnection client = ClientConnection.get(ctx);
        if (client != null) {
            client.messageText(contents);   // adds text to buffer
            return;
        }
        ((parse serial number from contents, process registration))
        ClientConnection.online(serialNumber, ctx);     // register success, create the client object
    }

    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ClientConnection client = ClientConnection.get(ctx);
        if (client == null) 
            Report.debug("completed read of message from unregistered client");
        else {
            Report.debug("completed read of message from client " + client.serialNumber());
            String contents = client.messageText();
            ... ((process message))
        }
    }
 }

在每次channelRead之后都不会调用

推荐答案

channelReadComplete. netty事件循环将从NIO套接字读取并触发多个channelRead,直到没有更多数据要读取或应该放弃为止,然后触发channelReadComplete.

channelReadComplete is NOT called after each channelRead. The netty event loop will read from NIO socket and fire multiple channelRead until no more data to read or it should give up, then channelReadComplete is fired.

这篇关于套接字消息被netty拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆