【发布时间】:2018-08-04 23:22:45
【问题描述】:
解决方案:使用ChannelTrafficShapingHandler;
问题
看来我的实现 Netty(版本 4.1.25Final)平均每秒无法发送或接收超过 1024 字节的数据。
问题 是否可以使用 TCP 和 SSL 每秒发送/接收超过 1024 个字节?如果有,怎么做?
服务器实现
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SslHandler handler = context.newHandler(ch.alloc());
handler.setHandshakeTimeoutMillis(20_000);
handler.setCloseNotifyFlushTimeoutMillis(20_000);
handler.setCloseNotifyReadTimeoutMillis(20_000);
pipeline.addLast(handler);
pipeline.addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 8, 0, 8));
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast(new LengthFieldPrepender(8));
pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new ServerInboundHandler());
}
客户端实施
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(context.newHandler(ch.alloc(), "localhost", 1337));
pipeline.addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 8, 0, 8));
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast(new LengthFieldPrepender(8));
pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast(new LoggingHandler(LogLevel.INFO));
pipeline.addLast("handler", new ClientLoginChannelHandler(application, application.getPacketManager()));
}
值得注意的是,我启用了以下功能;
// Server
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true)
// Client
.option(ChannelOption.RCVBUF_ALLOCATOR, new DefaultMaxBytesRecvByteBufAllocator(1024 * 1024, 1024 * 1024))
.option(ChannelOption.SO_RCVBUF, 1024 * 1024)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
【问题讨论】:
-
那么使用ChannelTrafficShapingHandler的解决方案在哪里呢?我有一个类似的问题,我正在开发 TCP 客户端,来自服务器的 xml 在 1024 字节上被切断,我试图找出问题所在,并添加 .option(ChannelOption.RCVBUF_ALLOCATOR, new DefaultMaxBytesRecvByteBufAllocator(1024 * 1024 , 1024 * 1024)) 帮助了我,但我仍然对 ChannelTrafficShapingHandler 以及它在这种情况下如何帮助我感到好奇。谢谢。