Skip to content

Commit aa4eeaa

Browse files
committed
feat(mqtt):1.10.4, 为mqtt添加自动重连设置项,可以选择不重连,以及重连间隔
1 parent 355c802 commit aa4eeaa

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed

modules/mqtt/client.cpp

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct Client::Data {
5151
State state = State::kNone;
5252

5353
int cb_level = 0; //! 回调层级
54+
int reconnect_wait_remain_sec = 0; //! 重连等待剩余时长
5455

5556
std::thread *sp_thread = nullptr;
5657

@@ -205,8 +206,9 @@ void Client::cleanup()
205206

206207
bool Client::start()
207208
{
208-
if (d_->state != State::kInited) {
209-
LogWarn("state != kInited");
209+
if (d_->state != State::kInited &&
210+
d_->state != State::kDisconnected) {
211+
LogWarn("state is not kInited or kDisconnected");
210212
return false;
211213
}
212214

@@ -312,7 +314,8 @@ bool Client::start()
312314

313315
void Client::stop()
314316
{
315-
if (d_->state <= State::kInited)
317+
if (d_->state <= State::kInited ||
318+
d_->state == State::kDisconnected)
316319
return;
317320

318321
RECORD_SCOPE();
@@ -400,14 +403,14 @@ void Client::onTimerTick()
400403
mosquitto_loop_misc(d_->sp_mosq);
401404

402405
if (mosquitto_socket(d_->sp_mosq) < 0) {
403-
LogInfo("disconnected with broker, retry.");
404-
d_->state = State::kConnecting;
406+
LogNotice("mosquitto_socket() < 0");
407+
handleDisconnectEvent();
408+
405409
} else {
406410
enableSocketWriteIfNeed();
407411
}
408-
}
409412

410-
if (d_->state == State::kConnecting) {
413+
} else if (d_->state == State::kConnecting) {
411414
if (d_->sp_thread == nullptr) {
412415
auto is_alive = d_->alive_tag.get(); //! 原理见Q1
413416
d_->sp_thread = new thread(
@@ -425,6 +428,16 @@ void Client::onTimerTick()
425428
}
426429
);
427430
}
431+
432+
} else if (d_->state == State::kReconnWaiting) {
433+
auto &remain_sec = d_->reconnect_wait_remain_sec;
434+
if (remain_sec > 0) {
435+
--remain_sec;
436+
if (remain_sec == 0) {
437+
d_->state = State::kConnecting;
438+
LogDbg("wait timeout, reconnect now");
439+
}
440+
}
428441
}
429442
}
430443

@@ -504,21 +517,8 @@ void Client::onConnected(int rc)
504517
void Client::onDisconnected(int rc)
505518
{
506519
RECORD_SCOPE();
507-
disableSocketRead();
508-
disableSocketWrite();
509-
510-
LogInfo("disconnected");
511-
512-
if (d_->state >= State::kTcpConnected) {
513-
if (d_->state == State::kMqttConnected) {
514-
++d_->cb_level;
515-
if (d_->callbacks.disconnected)
516-
d_->callbacks.disconnected();
517-
--d_->cb_level;
518-
}
519-
d_->state = State::kConnecting;
520-
}
521-
(void)rc;
520+
LogNotice("disconnected, rc:%d", rc);
521+
handleDisconnectEvent();
522522
}
523523

524524
void Client::onPublish(int mid)
@@ -663,5 +663,39 @@ void Client::disableTimer()
663663
d_->sp_timer_ev->disable();
664664
}
665665

666+
void Client::handleDisconnectEvent()
667+
{
668+
disableSocketRead();
669+
disableSocketWrite();
670+
671+
if (d_->state >= State::kTcpConnected) {
672+
if (d_->state == State::kMqttConnected) {
673+
++d_->cb_level;
674+
if (d_->callbacks.disconnected)
675+
d_->callbacks.disconnected();
676+
--d_->cb_level;
677+
}
678+
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+
}
697+
}
698+
}
699+
666700
}
667701
}

modules/mqtt/client.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class Client {
7474
Will will;
7575
TLS tls;
7676

77+
bool auto_reconnect_enabled = true; //! 是否自动重连
78+
int auto_reconnect_wait_sec = 0; //! 自动重连等待时长,秒
79+
7780
bool isValid() const;
7881
};
7982

@@ -119,6 +122,8 @@ class Client {
119122
kConnecting, //!< 正在连接
120123
kTcpConnected, //!< TCP已连接
121124
kMqttConnected, //!< MQTT已连接
125+
kDisconnected, //!< 已断连
126+
kReconnWaiting, //!< 断连等待中
122127
};
123128

124129
State getState() const;
@@ -155,6 +160,8 @@ class Client {
155160
void disableSocketWrite();
156161
void disableTimer();
157162

163+
void handleDisconnectEvent();
164+
158165
private:
159166
struct Data;
160167
Data *d_ = nullptr;

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 := 3
24+
TBOX_VERSION_REVISION := 4

0 commit comments

Comments
 (0)