在netty Java文档中,它表示so be sure to call flush() once you want to request to flush,但是在此示例中,它表示无需调用ctx.flush()。但是我没有找到flush(ChannelHandlerContext ctx)的名称。

package io.netty.example.time;

public class TimeEncoder extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        UnixTime m = (UnixTime) msg;
        ByteBuf encoded = ctx.alloc().buffer(4);
        encoded.writeInt((int)m.value());
        ctx.write(encoded, promise); // (1)
    }
}


Second, we did not call ctx.flush(). There is a separate handler method void flush(ChannelHandlerContext ctx) which is purposed to override the flush() operation.
https://netty.io/wiki/user-guide-for-4.x.html#wiki-h3-8

最佳答案

缓冲的要点是,您将“一堆”数据“聚集”在一起并一次发送全部,因为发送大量数据的速度和发送少量数据的速度大致一样快(因为IO速度很慢)。

这将分配一个4字节的缓冲区。如果每4个字节刷新一次,效率将不高。

仅在此特定方法中,某个地方会出现冲洗。

09-09 22:49
查看更多