Skip to content

Commit 3629442

Browse files
committed
fix(mqtt): 修复mqtt在tcp连接成功,而mqtt连接失败时没有回调connect_fail问题;修复重复调用disconnect回调的问题
1 parent 47c1075 commit 3629442

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

modules/mqtt/client.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,8 @@ void Client::onTimerTick()
407407
d_->state == State::kMqttConnected) {
408408
mosquitto_loop_misc(d_->sp_mosq);
409409

410-
if (mosquitto_socket(d_->sp_mosq) < 0) {
411-
LogNotice("mosquitto_socket() < 0");
412-
handleDisconnectEvent();
413-
414-
} else {
410+
if (mosquitto_socket(d_->sp_mosq) > 0)
415411
enableSocketWriteIfNeed();
416-
}
417412

418413
} else if (d_->state == State::kConnecting) {
419414
if (d_->sp_thread == nullptr) {
@@ -708,15 +703,18 @@ void Client::handleDisconnectEvent()
708703

709704
if (d_->state == State::kTcpConnected ||
710705
d_->state == State::kMqttConnected) {
706+
auto is_mqtt_disconnected = d_->state == State::kMqttConnected;
707+
//! 一定要先判定,因为 tryReconnect() 会改 d_->state
708+
711709
tryReconnect();
712710

713711
++d_->cb_level;
714-
if (d_->state == State::kTcpConnected) {
715-
if (d_->callbacks.connect_fail)
716-
d_->callbacks.connect_fail();
717-
} else {
712+
if (is_mqtt_disconnected) {
718713
if (d_->callbacks.disconnected)
719714
d_->callbacks.disconnected();
715+
} else {
716+
if (d_->callbacks.connect_fail)
717+
d_->callbacks.connect_fail();
720718
}
721719
--d_->cb_level;
722720
}
@@ -725,6 +723,7 @@ void Client::handleDisconnectEvent()
725723
void Client::updateStateTo(State new_state)
726724
{
727725
d_->state = new_state;
726+
LogDbg("new_state: %d", d_->state);
728727

729728
++d_->cb_level;
730729
if (d_->callbacks.state_changed)

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 10
24-
TBOX_VERSION_REVISION := 8
24+
TBOX_VERSION_REVISION := 9

0 commit comments

Comments
 (0)