20260126:解决3帧内无注册包直接关闭设备端连接
This commit is contained in:
parent
692dcaab1f
commit
4ce8dce963
@ -92,6 +92,11 @@ public class Session {
|
||||
private String ip;
|
||||
/*服务协议类型 MQTT,TCP UDP COAP*/
|
||||
private ServerType serverType;
|
||||
// 帧计数器(未注册前计数)
|
||||
private AtomicInteger frameCountBeforeRegister = new AtomicInteger(0);
|
||||
// 未注册时最大允许帧数
|
||||
private static final int MAX_FRAMES_BEFORE_REGISTER = 3;
|
||||
|
||||
|
||||
public Session() {
|
||||
|
||||
@ -170,8 +175,24 @@ public class Session {
|
||||
return className;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加未注册前的帧计数
|
||||
*/
|
||||
public void incrementFrameCount() {
|
||||
frameCountBeforeRegister.incrementAndGet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否需要因未注册而关闭连接
|
||||
* @return true=应该关闭连接
|
||||
*/
|
||||
public boolean shouldCloseForNoRegister() {
|
||||
return !isRegistered() && frameCountBeforeRegister.get() > MAX_FRAMES_BEFORE_REGISTER;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断设备是否已经注册上线
|
||||
* @return true:已注册;false:未注册
|
||||
*/
|
||||
public boolean isRegistered() {
|
||||
return clientId != null;
|
||||
|
||||
@ -50,6 +50,11 @@ public class TCPMessageAdapter extends ChannelInboundHandlerAdapter {
|
||||
ByteBuf buf = (ByteBuf) msg;
|
||||
/*session存储*/
|
||||
Session session = getSession(ctx);
|
||||
session.incrementFrameCount();
|
||||
if (session.shouldCloseForNoRegister()) {
|
||||
log.info("=>设备未注册,关闭连接session=[{}],remoteAddress=[{}]", session.getClientId(),ctx.channel().remoteAddress());
|
||||
ctx.close();
|
||||
}
|
||||
session.access();
|
||||
/*将设备端消息传递给下一个处理器*/
|
||||
ctx.fireChannelRead(Packet.of(session, buf));
|
||||
|
||||
@ -516,8 +516,8 @@ public class DeviceServiceImpl implements IDeviceService {
|
||||
List<ThingsModelSimpleItem> thingsModelValueRemarkItem = input.getThingsModelValueRemarkItem();
|
||||
// 判断是否有纯计算物模型 有:需要获取设备级别物模型 无:直接使用产品级别物模型即可
|
||||
boolean isCalculated = thingsModelValueRemarkItem.stream().anyMatch(ThingsModelSimpleItem::isCalculated);
|
||||
Map<String, ThingsModelValueItem> calculatedModelMap = new HashMap<>();
|
||||
Map<String, String> thingsModelValueMap = new HashMap<>();
|
||||
Map<String, ThingsModelValueItem> calculatedModelMap = Collections.emptyMap();
|
||||
Map<String, String> thingsModelValueMap = Collections.emptyMap();
|
||||
if (isCalculated) {
|
||||
calculatedModelMap = itslCache.getDeviceCalculatedModelMap(serialNumber);
|
||||
thingsModelValueMap = thingsModelValueRemarkItem.stream().filter(things -> !things.isCalculated())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user