From 78c05c1ed6249286d86899ca68650fd1bf016a2c Mon Sep 17 00:00:00 2001 From: ROOKIE Date: Thu, 28 Aug 2025 11:31:03 +0800 Subject: [PATCH] =?UTF-8?q?fix=20jt808:=20=E4=BC=98=E5=8C=96=20TCP=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=BC=82=E5=B8=B8=E5=85=B3=E9=97=AD=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 增加 SocketException 异常处理,确保异常情况下正确关闭连接 - 新增 doWithSessionManager 方法统一处理会话管理 - 实现 closeAndIgnoreException 方法安全关闭连接 --- ...808InstructionServerTcpHandlerAdapter.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ext/jt/jt-808-server-spring-boot-starter-reactive/src/main/java/io/github/hylexus/xtream/codec/ext/jt808/extensions/handler/Jt808InstructionServerTcpHandlerAdapter.java b/ext/jt/jt-808-server-spring-boot-starter-reactive/src/main/java/io/github/hylexus/xtream/codec/ext/jt808/extensions/handler/Jt808InstructionServerTcpHandlerAdapter.java index c75f8f10..c328f04c 100644 --- a/ext/jt/jt-808-server-spring-boot-starter-reactive/src/main/java/io/github/hylexus/xtream/codec/ext/jt808/extensions/handler/Jt808InstructionServerTcpHandlerAdapter.java +++ b/ext/jt/jt-808-server-spring-boot-starter-reactive/src/main/java/io/github/hylexus/xtream/codec/ext/jt808/extensions/handler/Jt808InstructionServerTcpHandlerAdapter.java @@ -33,6 +33,8 @@ import reactor.netty.NettyInbound; import reactor.netty.NettyOutbound; import java.net.InetSocketAddress; +import java.net.SocketException; +import java.util.function.Consumer; /** * @author hylexus @@ -60,11 +62,43 @@ public class Jt808InstructionServerTcpHandlerAdapter extends DefaultTcpXtreamNet log.error("Unexpected Exception", throwable); return Mono.empty(); }); + }).doOnError(SocketException.class, throwable -> { + this.doWithSessionManager(manager -> { + boolean closed = false; + Channel channel = inboundInfo.channel(); + if (channel == null) { + return; + } + try { + final String sessionId = sessionManager.sessionIdGenerator().generateTcpSessionId(channel); + closed = sessionManager.closeSessionById(sessionId, XtreamSessionEventListener.DefaultSessionCloseReason.CLOSED_BY_CLIENT); + } finally { + if (!closed) { + closeAndIgnoreException(channel, "Close channel because of [Socket Exception]: {}"); + } + } + }); }).onErrorResume(throwable -> { log.error("Unexpected Error", throwable); return Mono.empty(); }); } + protected void doWithSessionManager(Consumer> consumer) { + if (this.sessionManager != null) { + consumer.accept(this.sessionManager); + } else { + throw new IllegalStateException("No session manager found"); + } + } + + private static void closeAndIgnoreException(Channel channel, String message) { + try { + log.info(message, channel); + channel.close(); + } catch (Exception ignored) { + // ignored + } + } @Override protected Mono handleSingleRequest(NettyInbound nettyInbound, NettyOutbound nettyOutbound, ByteBuf payload, InboundInfo inboundInfo) { -- Gitee