Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica-mqtt
提交
54837baf
mica-mqtt
项目概览
mica
/
mica-mqtt
通知
71
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica-mqtt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
54837baf
编写于
8月 13, 2021
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
优化 mqtt 解码异常处理。
上级
c883282f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
32 addition
and
27 deletion
+32
-27
mica-mqtt-codec/src/main/java/net/dreamlu/iot/mqtt/codec/MqttDecoder.java
...src/main/java/net/dreamlu/iot/mqtt/codec/MqttDecoder.java
+17
-1
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/client/MqttClientAioHandler.java
...et/dreamlu/iot/mqtt/core/client/MqttClientAioHandler.java
+1
-10
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerAioHandler.java
...et/dreamlu/iot/mqtt/core/server/MqttServerAioHandler.java
+1
-10
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerAioListener.java
...t/dreamlu/iot/mqtt/core/server/MqttServerAioListener.java
+13
-6
未找到文件。
mica-mqtt-codec/src/main/java/net/dreamlu/iot/mqtt/codec/MqttDecoder.java
浏览文件 @
54837baf
...
...
@@ -17,6 +17,8 @@
package
net.dreamlu.iot.mqtt.codec
;
import
org.tio.core.ChannelContext
;
import
org.tio.core.exception.AioDecodeException
;
import
org.tio.core.exception.TioDecodeException
;
import
java.nio.ByteBuffer
;
import
java.nio.charset.StandardCharsets
;
...
...
@@ -54,7 +56,21 @@ public final class MqttDecoder {
this
.
maxClientIdLength
=
maxClientIdLength
;
}
public
MqttMessage
decode
(
ChannelContext
ctx
,
ByteBuffer
buffer
,
int
limit
,
int
position
,
int
readableLength
)
{
public
MqttMessage
doDecode
(
ChannelContext
ctx
,
ByteBuffer
buffer
,
int
limit
,
int
position
,
int
readableLength
)
throws
TioDecodeException
{
// 1. 半包
MqttMessage
message
=
decode
(
ctx
,
buffer
,
limit
,
position
,
readableLength
);
if
(
message
==
null
)
{
return
null
;
}
// 2. 解码异常
DecoderResult
decoderResult
=
message
.
decoderResult
();
if
(
decoderResult
.
isFailure
()
&&
decoderResult
.
getCause
()
instanceof
DecoderException
)
{
throw
new
AioDecodeException
(
decoderResult
.
getCause
());
}
return
message
;
}
private
MqttMessage
decode
(
ChannelContext
ctx
,
ByteBuffer
buffer
,
int
limit
,
int
position
,
int
readableLength
)
{
// 1. 首先判断缓存中协议头是否读完(MQTT协议头为2字节)
if
(
readableLength
<
MQTT_PROTOCOL_LENGTH
)
{
return
null
;
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/client/MqttClientAioHandler.java
浏览文件 @
54837baf
...
...
@@ -20,7 +20,6 @@ import net.dreamlu.iot.mqtt.codec.*;
import
org.tio.client.intf.ClientAioHandler
;
import
org.tio.core.ChannelContext
;
import
org.tio.core.TioConfig
;
import
org.tio.core.exception.AioDecodeException
;
import
org.tio.core.exception.TioDecodeException
;
import
org.tio.core.intf.Packet
;
...
...
@@ -52,15 +51,7 @@ public class MqttClientAioHandler implements ClientAioHandler {
@Override
public
Packet
decode
(
ByteBuffer
buffer
,
int
limit
,
int
position
,
int
readableLength
,
ChannelContext
context
)
throws
TioDecodeException
{
MqttMessage
message
=
mqttDecoder
.
decode
(
context
,
buffer
,
limit
,
position
,
readableLength
);
if
(
message
==
null
)
{
return
null
;
}
DecoderResult
decoderResult
=
message
.
decoderResult
();
if
(
decoderResult
.
isFailure
()
&&
decoderResult
.
getCause
()
instanceof
DecoderException
)
{
throw
new
AioDecodeException
(
decoderResult
.
getCause
());
}
return
message
;
return
mqttDecoder
.
doDecode
(
context
,
buffer
,
limit
,
position
,
readableLength
);
}
@Override
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerAioHandler.java
浏览文件 @
54837baf
...
...
@@ -22,7 +22,6 @@ import org.slf4j.LoggerFactory;
import
org.tio.core.ChannelContext
;
import
org.tio.core.Tio
;
import
org.tio.core.TioConfig
;
import
org.tio.core.exception.AioDecodeException
;
import
org.tio.core.exception.TioDecodeException
;
import
org.tio.core.intf.Packet
;
import
org.tio.server.AcceptCompletionHandler
;
...
...
@@ -61,15 +60,7 @@ public class MqttServerAioHandler implements ServerAioHandler {
*/
@Override
public
Packet
decode
(
ByteBuffer
buffer
,
int
limit
,
int
position
,
int
readableLength
,
ChannelContext
context
)
throws
TioDecodeException
{
MqttMessage
message
=
mqttDecoder
.
decode
(
context
,
buffer
,
limit
,
position
,
readableLength
);
if
(
message
==
null
)
{
return
null
;
}
DecoderResult
decoderResult
=
message
.
decoderResult
();
if
(
decoderResult
.
isFailure
()
&&
decoderResult
.
getCause
()
instanceof
DecoderException
)
{
throw
new
AioDecodeException
(
decoderResult
.
getCause
());
}
return
message
;
return
mqttDecoder
.
doDecode
(
context
,
buffer
,
limit
,
position
,
readableLength
);
}
/**
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServerAioListener.java
浏览文件 @
54837baf
...
...
@@ -56,19 +56,26 @@ public class MqttServerAioListener extends DefaultAioListener {
@Override
public
void
onBeforeClose
(
ChannelContext
context
,
Throwable
throwable
,
String
remark
,
boolean
isRemove
)
{
// 1. 业务 id
String
clientId
=
context
.
getBsId
();
// 2. 判断是否正常断开
boolean
isNotNormalDisconnect
=
context
.
get
(
MqttConst
.
DIS_CONNECTED
)
==
null
;
if
(
isNotNormalDisconnect
||
throwable
!=
null
)
{
logger
.
error
(
"Mqtt server close clientId isBlank, remark:{} isRemove:{}"
,
remark
,
isRemove
,
throwable
);
}
else
{
logger
.
info
(
"Mqtt server close clientId:{} remark:{} isRemove:{}"
,
clientId
,
remark
,
isRemove
);
}
// 3. 业务 id 不能为空
if
(
StrUtil
.
isBlank
(
clientId
))
{
logger
.
warn
(
"Mqtt server close clientId isBlank, remark:{} isRemove:{}"
,
remark
,
isRemove
);
return
;
}
logger
.
info
(
"Mqtt server close clientId:{} remark:{} isRemove:{}"
,
clientId
,
remark
,
isRemove
);
// 1. 对于异常断开连接,处理遗嘱消息
// 4. 对于异常断开连接,处理遗嘱消息
sendWillMessage
(
context
,
clientId
);
//
2
. 会话清理
//
5
. 会话清理
cleanSession
(
clientId
);
//
3
. 解绑 clientId
//
6
. 解绑 clientId
Tio
.
unbindBsId
(
context
);
//
4
. 下线事件
//
7
. 下线事件
notify
(
clientId
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录