解决方案:使用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)
答案 0 :(得分:1)
是的,您需要在channelRead()中累积数据并在从channelRead()返回之前在通道上调用read()。如果没有更多数据,则会调用您的channelReadComplete(),并且有一段时间将您累积的数据发送到响应处理程序。