Netty 客户端接收消息延迟

时间:2021-03-09 12:23:32

标签: java sockets tcp netty

1.场景描述: 设备将数据发送到 Netty 服务器(大约 ## 20ms 间隔 ##),Netty 服务器将 msg 转发到客户端 ## 立即 ##(IOS 或 Android)。 2.相关业务代码 ctx.writeAndFlush(msg)

protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    int writeSpinCount = -1;

    boolean setOpWrite = false;
    for (;;) {
        //Get the data of the first node that needs to be flushed
        Object msg = in.current();

        if (msg instanceof ByteBuf) {
            
            ByteBuf buf = (ByteBuf) msg;

            boolean done = false;
            long flushedAmount = 0;
            // Get the number of spin lock iterations
            if (writeSpinCount == -1) {
                writeSpinCount = config().getWriteSpinCount();
            }
            // Spin, write out the current node
            for (int i = writeSpinCount - 1; i >= 0; i --) {
                int localFlushedAmount = doWriteBytes(buf);
                if (localFlushedAmount == 0) {
                    setOpWrite = true;
                    break;
                }

                flushedAmount += localFlushedAmount;
                if (!buf.isReadable()) {
                    done = true;
                    break;
                }
            }

            in.progress(flushedAmount);

            // After writing, delete the current node
            if (done) {
                in.remove();
            } else {
                break;
            }
        } 
    }
}
protected int doWriteBytes(ByteBuf buf) throws Exception {
    final int expectedWrittenBytes = buf.readableBytes();
    return buf.readBytes(javaChannel(), expectedWrittenBytes);
}

3.问题 netty Server 可以及时接收到设备数据 netty Server 也可以及时将数据写入 Socket Buffer。 但是Netty客户端收到消息延迟!!!(比如5s延迟)

4.服务器带宽配置 入站每秒 100M/bps 位。 出站每秒 5M/bps 比特。

1 个答案:

答案 0 :(得分:0)

客户端长度打包问题引起的。

相关问题