2024-10-08 09:38:54 +08:00
|
|
|
|
/**
|
2025-03-04 10:42:17 +08:00
|
|
|
|
* 开源代码,仅供学习和交流研究使用,商用请联系三丙
|
|
|
|
|
|
* 微信:mohan_88888
|
|
|
|
|
|
* 抖音:程序员三丙
|
2026-06-10 14:26:04 +08:00
|
|
|
|
* 付费课程:https://www.bilibili.com/cheese/play/ss942400790
|
2024-10-08 09:38:54 +08:00
|
|
|
|
*/
|
|
|
|
|
|
package sanbing.jcpp.protocol;
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import sanbing.jcpp.infrastructure.stats.MessagesStats;
|
|
|
|
|
|
import sanbing.jcpp.infrastructure.util.exception.DownlinkException;
|
|
|
|
|
|
import sanbing.jcpp.infrastructure.util.trace.TracerRunnable;
|
|
|
|
|
|
import sanbing.jcpp.protocol.domain.ListenerToHandlerMsg;
|
|
|
|
|
|
import sanbing.jcpp.protocol.domain.SessionToHandlerMsg;
|
|
|
|
|
|
import sanbing.jcpp.protocol.forwarder.Forwarder;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-09 08:23:59 +00:00
|
|
|
|
* @author 九筒
|
2024-10-08 09:38:54 +08:00
|
|
|
|
*/
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public abstract class ProtocolMessageProcessor {
|
|
|
|
|
|
protected final Forwarder forwarder;
|
|
|
|
|
|
protected final ProtocolContext protocolContext;
|
|
|
|
|
|
|
|
|
|
|
|
protected ProtocolMessageProcessor(Forwarder forwarder, ProtocolContext protocolContext) {
|
|
|
|
|
|
this.forwarder = forwarder;
|
|
|
|
|
|
this.protocolContext = protocolContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void uplinkHandleAsync(ListenerToHandlerMsg listenerToHandlerMsg, MessagesStats uplinkMsgStats) {
|
|
|
|
|
|
|
|
|
|
|
|
UUID id = listenerToHandlerMsg.session().getId();
|
|
|
|
|
|
|
|
|
|
|
|
protocolContext.getShardingThreadPool().execute(id, new TracerRunnable(() -> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
listenerToHandlerMsg.session().setForwarder(forwarder);
|
|
|
|
|
|
|
|
|
|
|
|
uplinkHandle(listenerToHandlerMsg);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
|
|
uplinkMsgStats.incrementFailed();
|
|
|
|
|
|
|
2025-09-09 08:23:59 +00:00
|
|
|
|
log.error("{} 上行消息处理器处理报文异常", listenerToHandlerMsg.session(), e);
|
2024-10-08 09:38:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-09 10:08:32 +08:00
|
|
|
|
protected abstract void uplinkHandle(ListenerToHandlerMsg listenerToHandlerMsg);
|
2024-10-08 09:38:54 +08:00
|
|
|
|
|
2025-09-09 08:23:59 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 下行消息处理入口
|
|
|
|
|
|
* 负责统一的异常处理和日志记录
|
|
|
|
|
|
*/
|
2024-10-08 09:38:54 +08:00
|
|
|
|
public void downlinkHandle(SessionToHandlerMsg sessionToHandlerMsg, MessagesStats downlinkMsgStats) throws DownlinkException {
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
2025-09-09 08:23:59 +00:00
|
|
|
|
doDownlinkHandle(sessionToHandlerMsg);
|
2024-10-08 09:38:54 +08:00
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
|
|
downlinkMsgStats.incrementFailed();
|
2025-09-09 08:23:59 +00:00
|
|
|
|
|
|
|
|
|
|
log.warn("下行消息处理失败,session: {}, 异常信息: {}", sessionToHandlerMsg.session(), e.getMessage(), e);
|
2024-10-08 09:38:54 +08:00
|
|
|
|
|
|
|
|
|
|
throw new DownlinkException(e.getMessage(), e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 08:23:59 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 下行消息具体处理逻辑
|
|
|
|
|
|
* 由各协议的具体实现类重写
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected abstract void doDownlinkHandle(SessionToHandlerMsg sessionToHandlerMsg);
|
2024-10-08 09:38:54 +08:00
|
|
|
|
}
|