mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-16 18:35:05 +08:00
【fix】 自定义netty 接收数据缓冲期控制接收数据大小
This commit is contained in:
@ -37,7 +37,7 @@ public class ServerHandleReportHandleChannelTransferTypeAdvanced extends Abstrac
|
||||
public void doHandler(Channel channel, NettyProxyMsg msg) {
|
||||
String clientId = new String(msg.getClientId());
|
||||
Integer visitorPort = Integer.valueOf(new String(msg.getVisitorPort()));
|
||||
// log.info("接收到客户端:[{}]内网穿透返回的数据:[{}]", clientId, msg.getData().length);
|
||||
log.info("访客端口:[{}] 接收到客户端:[{}]",visitorPort, clientId);
|
||||
log.debug("接收到客户端:[{}]内网穿透返回的数据:[{}]", clientId, new String(msg.getData()));
|
||||
// 将数据转发访客通道
|
||||
byte[] visitorId = msg.getVisitorId();
|
||||
@ -46,7 +46,7 @@ public class ServerHandleReportHandleChannelTransferTypeAdvanced extends Abstrac
|
||||
ByteBuf buf = visitor.config().getAllocator().buffer(msg.getData().length);
|
||||
buf.writeBytes(msg.getData());
|
||||
visitor.writeAndFlush(buf);
|
||||
|
||||
log.info("writeAndFlush");
|
||||
// 记录出口数据
|
||||
ServerChannelFlow serverChannelFlow = ServerChannelFlow
|
||||
.builder()
|
||||
|
@ -3,6 +3,10 @@ package org.framework.lazy.cloud.network.heartbeat.server.netty.filter;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.compression.JdkZlibDecoder;
|
||||
import io.netty.handler.codec.compression.JdkZlibEncoder;
|
||||
import io.netty.handler.codec.protobuf.ProtobufDecoder;
|
||||
import io.netty.handler.codec.protobuf.ProtobufEncoder;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
@ -47,5 +51,9 @@ public class NettyServerFilter extends ChannelInitializer<SocketChannel> {
|
||||
// 类型处理器适配器
|
||||
ChannelTypeAdapter channelTypeAdapter = new ChannelTypeAdapter(handleChannelTypeAdvancedList);
|
||||
pipeline.addLast("doHandler", new NettyServerHandler(channelTypeAdapter));// 服务端业务逻辑
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -158,6 +158,13 @@ public class VisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
||||
// if (clientChannel != null) {
|
||||
// clientChannel.config().setOption(ChannelOption.AUTO_READ, visitorChannel.isWritable());
|
||||
// }
|
||||
if (ctx.channel().isWritable()) {
|
||||
System.out.println("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
System.out.println("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged");
|
||||
super.channelWritabilityChanged(ctx);
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.allocator.NettyRecvByteBufAllocator;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.NettyServerFilter;
|
||||
|
||||
public class NettyOnCloudNettyServerSocket {
|
||||
@ -31,9 +32,20 @@ public class NettyOnCloudNettyServerSocket {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||
// 给服务端channel设置属性
|
||||
.option(ChannelOption.SO_BACKLOG, 128)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
|
||||
// 设置读缓冲区为2M
|
||||
.childOption(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
.childOption(ChannelOption.TCP_NODELAY, false)
|
||||
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
.childOption(ChannelOption.SO_BACKLOG, 512)//务端接受连接的队列长度 默认128
|
||||
.childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
|
||||
|
||||
.childHandler(nettyServerFilter);
|
||||
channelFuture = b.bind(serverPort).sync();
|
||||
|
||||
|
@ -10,6 +10,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrat
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyClientVisitorContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyVisitorPortContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.allocator.NettyRecvByteBufAllocator;
|
||||
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.VisitorFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -48,8 +49,22 @@ public class NettyVisitorSocket {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b
|
||||
.group(bossGroup, workerGroup)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
|
||||
|
||||
// 设置读缓冲区为2M
|
||||
.childOption(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
|
||||
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
.childOption(ChannelOption.TCP_NODELAY, false)
|
||||
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
.childOption(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
|
||||
.childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
|
||||
|
||||
.childHandler(visitorFilter);
|
||||
ChannelFuture sync = b.bind(visitorPort).sync();
|
||||
sync.addListener((ChannelFutureListener) future -> {
|
||||
|
Reference in New Issue
Block a user