@@ -207,8 +207,8 @@ void Client::cleanup()
207
207
bool Client::start ()
208
208
{
209
209
if (d_->state != State::kInited &&
210
- d_->state != State::kDisconnected ) {
211
- LogWarn (" state is not kInited or kDisconnected " );
210
+ d_->state != State::kEnd ) {
211
+ LogWarn (" state is not kInited or kEnd " );
212
212
return false ;
213
213
}
214
214
@@ -302,7 +302,9 @@ bool Client::start()
302
302
[this , is_alive, ret] {
303
303
if (!is_alive) // !< 判定this指针是否有效
304
304
return ;
305
- onTcpConnectDone (ret, true );
305
+
306
+ onTcpConnectDone (ret);
307
+ enableTimer ();
306
308
},
307
309
" mqtt::Client::start, connect done"
308
310
);
@@ -315,7 +317,7 @@ bool Client::start()
315
317
void Client::stop ()
316
318
{
317
319
if (d_->state <= State::kInited ||
318
- d_->state == State::kDisconnected )
320
+ d_->state == State::kEnd )
319
321
return ;
320
322
321
323
RECORD_SCOPE ();
@@ -421,7 +423,7 @@ void Client::onTimerTick()
421
423
[this , is_alive, ret] {
422
424
if (!is_alive) // !< 判定this指针是否有效
423
425
return ;
424
- onTcpConnectDone (ret, false );
426
+ onTcpConnectDone (ret);
425
427
},
426
428
" mqtt::Client::onTimerTick, reconnect done"
427
429
);
@@ -438,6 +440,9 @@ void Client::onTimerTick()
438
440
LogDbg (" wait timeout, reconnect now" );
439
441
}
440
442
}
443
+
444
+ } else if (d_->state == State::kEnd ) {
445
+ disableTimer ();
441
446
}
442
447
}
443
448
@@ -591,7 +596,7 @@ void Client::onLog(int level, const char *str)
591
596
LogPrintfFunc (" mosq" , nullptr , nullptr , 0 , new_level, 0 , str);
592
597
}
593
598
594
- void Client::onTcpConnectDone (int ret, bool first_connect )
599
+ void Client::onTcpConnectDone (int ret)
595
600
{
596
601
if (d_->sp_thread == nullptr )
597
602
return ;
@@ -604,13 +609,10 @@ void Client::onTcpConnectDone(int ret, bool first_connect)
604
609
enableSocketRead ();
605
610
enableSocketWriteIfNeed ();
606
611
d_->state = State::kTcpConnected ;
612
+
607
613
} else {
608
- LogWarn ( " connect fail, ret:%d " , ret );
614
+ tryReconnect ( );
609
615
}
610
-
611
- // ! 如果是首次连接要启动定时器,重连的不需要
612
- if (first_connect)
613
- enableTimer ();
614
616
}
615
617
616
618
void Client::enableSocketRead ()
@@ -663,6 +665,27 @@ void Client::disableTimer()
663
665
d_->sp_timer_ev ->disable ();
664
666
}
665
667
668
+ void Client::tryReconnect ()
669
+ {
670
+ // ! 如果开启了自动重连
671
+ if (d_->config .auto_reconnect_enable ) {
672
+ if (d_->config .auto_reconnect_wait_sec > 0 ) {
673
+ LogDbg (" reconnect after %d sec" , d_->config .auto_reconnect_wait_sec );
674
+ d_->reconnect_wait_remain_sec = d_->config .auto_reconnect_wait_sec ;
675
+ d_->state = State::kReconnWaiting ;
676
+
677
+ } else {
678
+ LogDbg (" reconnect now" );
679
+ d_->reconnect_wait_remain_sec = 0 ;
680
+ d_->state = State::kConnecting ;
681
+ }
682
+
683
+ } else { // ! 如果不需要自动重连
684
+ LogDbg (" no need reconnect, end" );
685
+ d_->state = State::kEnd ;
686
+ }
687
+ }
688
+
666
689
void Client::handleDisconnectEvent ()
667
690
{
668
691
disableSocketRead ();
@@ -676,24 +699,7 @@ void Client::handleDisconnectEvent()
676
699
--d_->cb_level ;
677
700
}
678
701
679
- // ! 如果开启了自动重连
680
- if (d_->config .auto_reconnect_enabled ) {
681
- if (d_->config .auto_reconnect_wait_sec > 0 ) {
682
- LogDbg (" reconnect after %d sec" , d_->reconnect_wait_remain_sec );
683
- d_->reconnect_wait_remain_sec = d_->config .auto_reconnect_wait_sec ;
684
- d_->state = State::kReconnWaiting ;
685
-
686
- } else {
687
- LogDbg (" reconnect now" );
688
- d_->reconnect_wait_remain_sec = 0 ;
689
- d_->state = State::kConnecting ;
690
- }
691
-
692
- } else { // ! 如果不需要自动重连
693
- LogDbg (" no need reconnect" );
694
- d_->state = State::kDisconnected ;
695
- disableTimer ();
696
- }
702
+ tryReconnect ();
697
703
}
698
704
}
699
705
0 commit comments