@@ -407,13 +407,8 @@ void Client::onTimerTick()
407
407
d_->state == State::kMqttConnected ) {
408
408
mosquitto_loop_misc (d_->sp_mosq );
409
409
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 )
415
411
enableSocketWriteIfNeed ();
416
- }
417
412
418
413
} else if (d_->state == State::kConnecting ) {
419
414
if (d_->sp_thread == nullptr ) {
@@ -708,15 +703,18 @@ void Client::handleDisconnectEvent()
708
703
709
704
if (d_->state == State::kTcpConnected ||
710
705
d_->state == State::kMqttConnected ) {
706
+ auto is_mqtt_disconnected = d_->state == State::kMqttConnected ;
707
+ // ! 一定要先判定,因为 tryReconnect() 会改 d_->state
708
+
711
709
tryReconnect ();
712
710
713
711
++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) {
718
713
if (d_->callbacks .disconnected )
719
714
d_->callbacks .disconnected ();
715
+ } else {
716
+ if (d_->callbacks .connect_fail )
717
+ d_->callbacks .connect_fail ();
720
718
}
721
719
--d_->cb_level ;
722
720
}
@@ -725,6 +723,7 @@ void Client::handleDisconnectEvent()
725
723
void Client::updateStateTo (State new_state)
726
724
{
727
725
d_->state = new_state;
726
+ LogDbg (" new_state: %d" , d_->state );
728
727
729
728
++d_->cb_level ;
730
729
if (d_->callbacks .state_changed )
0 commit comments