【问题标题】:Can Netty send/receive more than 1024 bytes per frame over TCP with SSL?Netty 可以通过 TCP 使用 SSL 每帧发送/接收超过 1024 个字节吗?
【发布时间】: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 以及它在这种情况下如何帮助我感到好奇。谢谢。

标签: java netty


【解决方案1】:

可以,您需要在channelRead() 中累积数据并在从channelRead() 返回之前调用channel 上的read()。当没有更多数据时,您的 channelReadComplete() 将被调用,并且您将累积的数据发送到响应处理程序。

【讨论】:

  • 抱歉,解决方案是使用 ChannelTrafficShapingHandler,这是预期的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2018-06-13
  • 2020-09-22
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多