mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-17 02:45:03 +08:00
【fix】 自定义netty 接收数据缓冲期控制接收数据大小
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
|
||||
|
||||
public class NettyClientRealFilter extends ChannelInitializer<SocketChannel> {
|
||||
/**
|
||||
@ -13,18 +14,13 @@ public class NettyClientRealFilter extends ChannelInitializer<SocketChannel> {
|
||||
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
|
||||
*
|
||||
* @param ch the {@link Channel} which was registered.
|
||||
* @throws Exception is thrown if an error occurs. In that case it will be handled by
|
||||
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default connectionClose
|
||||
* the {@link Channel}.
|
||||
*/
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
// 解码、编码
|
||||
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
|
||||
pipeline.addLast(new TransferEncoder());
|
||||
pipeline.addLast(new NettyClientRealHandler());
|
||||
// // 解码、编码
|
||||
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
// pipeline.addLast(new NettMsgEncoder());
|
||||
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
// pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
@ -15,14 +16,14 @@ import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeK
|
||||
* 来自客户端 真实服务器返回的数据请求
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyClientRealHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
||||
public class NettyClientRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
|
||||
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {
|
||||
public void channelRead0(ChannelHandlerContext ctx,NettyByteBuf nettyByteBuf) {
|
||||
|
||||
// 客户端发送真实数据到代理了
|
||||
byte[] bytes = new byte[buf.readableBytes()];
|
||||
buf.readBytes(bytes);
|
||||
byte[] bytes = nettyByteBuf.getData();
|
||||
log.debug("bytes.length:{}",bytes.length);
|
||||
log.debug("接收客户端真实服务数据:{}", new String(bytes));
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
Integer visitorPort = ChannelAttributeKeyUtils.getVisitorPort(ctx.channel());
|
||||
@ -37,10 +38,9 @@ public class NettyClientRealHandler extends SimpleChannelInboundHandler<ByteBuf>
|
||||
returnMessage.setData(bytes);
|
||||
|
||||
visitorChannel.writeAndFlush(returnMessage);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
|
@ -6,13 +6,13 @@ import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.filter.NettyClientVisitorRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.*;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.*;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.allocator.NettyRecvByteBufAllocator;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
@ -54,7 +54,19 @@ public class NettyClientRealSocket {
|
||||
String visitorId = internalNetworkPenetrationRealClient.getVisitorId();
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
|
||||
.handler(new NettyClientRealFilter());
|
||||
// 设置读缓冲区为2M
|
||||
// .option(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
// .option(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
.option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
|
||||
.option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
|
||||
.handler(new NettyClientRealFilter())
|
||||
|
||||
;
|
||||
|
||||
|
||||
bootstrap.connect(clientTargetIp, clientTargetPort).addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
|
||||
|
Reference in New Issue
Block a user