mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2026-02-04 06:55:52 +08:00
[update] 显式指定注解处理器,4.x
This commit is contained in:
@@ -4,8 +4,12 @@ import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
@Slf4j
|
||||
public class ChannelUtils {
|
||||
/**
|
||||
@@ -74,5 +78,36 @@ public class ChannelUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制关闭Netty通道(跳过TCP优雅关闭,直接关闭套接字)
|
||||
* @param channel Netty通道
|
||||
*/
|
||||
public static void forceCloseChannel(Channel channel) {
|
||||
if (channel == null || !channel.isOpen()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 第一步:禁用通道读写,拒绝接收新数据
|
||||
channel.config().setAutoRead(false);
|
||||
channel.config().setAutoClose(true);
|
||||
|
||||
// 第二步:获取底层Socket,强制关闭(核心!抖音场景必须)
|
||||
if (channel instanceof NioSocketChannel) {
|
||||
NioSocketChannel nioChannel = (NioSocketChannel) channel;
|
||||
Socket socket = nioChannel.javaChannel().socket();
|
||||
if (socket != null && !socket.isClosed()) {
|
||||
socket.setSoLinger(true, 0); // 禁用SO_LINGER,立即关闭
|
||||
socket.close(); // 强制关闭Socket,切断TCP连接
|
||||
}
|
||||
}
|
||||
|
||||
// 第三步:关闭Netty通道(兜底)
|
||||
channel.close().sync();
|
||||
log.info("通道已强制关闭");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("强制关闭通道异常:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,13 @@ public class NettySocketProtocolHandleSocketLocalProxyTypeAdvanced
|
||||
|
||||
b.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.option(ChannelOption.SO_KEEPALIVE, true) // 开启TCP保活,检测无效连接
|
||||
.option(ChannelOption.TCP_NODELAY, true) // 关闭Nagle算法,适配直播流低延迟
|
||||
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)// 设置读缓冲区为2M
|
||||
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)// 设置写缓冲区为1M
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
|
||||
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
|
||||
@@ -83,6 +83,7 @@ public class NettySocketBackendHandler extends SimpleChannelInboundHandler<Netty
|
||||
if (nextChannel.isActive()) {
|
||||
nextChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.protocol.handler;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
@@ -107,6 +109,17 @@ public class NettySocks5CommandRequestHandler extends SimpleChannelInboundHandle
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
log.trace("代理通道与客户通道断开,即将断开客户端和代理服务器的连接");
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel.isActive()) {
|
||||
nextChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
cause.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user