Skip to content

Commit 5ad9e4c

Browse files
committed
fix(mqtt):1.10.4,实现了mqtt重连间隔的功能,并通过自测
1 parent aa4eeaa commit 5ad9e4c

File tree

3 files changed

+41
-32
lines changed

3 files changed

+41
-32
lines changed

examples/mqtt/conn/conn.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ int main(int argc, char **argv)
4141
mqtt::Client mqtt(sp_loop);
4242

4343
mqtt::Client::Config conf;
44+
conf.auto_reconnect_enable = true;
45+
conf.auto_reconnect_wait_sec = 5;
4446
#if 0
4547
conf.base.broker.domain = "cppmain.cpp";
4648
conf.base.broker.port = 1883;

modules/mqtt/client.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ void Client::cleanup()
207207
bool Client::start()
208208
{
209209
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");
212212
return false;
213213
}
214214

@@ -302,7 +302,9 @@ bool Client::start()
302302
[this, is_alive, ret] {
303303
if (!is_alive) //!< 判定this指针是否有效
304304
return;
305-
onTcpConnectDone(ret, true);
305+
306+
onTcpConnectDone(ret);
307+
enableTimer();
306308
},
307309
"mqtt::Client::start, connect done"
308310
);
@@ -315,7 +317,7 @@ bool Client::start()
315317
void Client::stop()
316318
{
317319
if (d_->state <= State::kInited ||
318-
d_->state == State::kDisconnected)
320+
d_->state == State::kEnd)
319321
return;
320322

321323
RECORD_SCOPE();
@@ -421,7 +423,7 @@ void Client::onTimerTick()
421423
[this, is_alive, ret] {
422424
if (!is_alive) //!< 判定this指针是否有效
423425
return;
424-
onTcpConnectDone(ret, false);
426+
onTcpConnectDone(ret);
425427
},
426428
"mqtt::Client::onTimerTick, reconnect done"
427429
);
@@ -438,6 +440,9 @@ void Client::onTimerTick()
438440
LogDbg("wait timeout, reconnect now");
439441
}
440442
}
443+
444+
} else if (d_->state == State::kEnd) {
445+
disableTimer();
441446
}
442447
}
443448

@@ -591,7 +596,7 @@ void Client::onLog(int level, const char *str)
591596
LogPrintfFunc("mosq", nullptr, nullptr, 0, new_level, 0, str);
592597
}
593598

594-
void Client::onTcpConnectDone(int ret, bool first_connect)
599+
void Client::onTcpConnectDone(int ret)
595600
{
596601
if (d_->sp_thread == nullptr)
597602
return;
@@ -604,13 +609,10 @@ void Client::onTcpConnectDone(int ret, bool first_connect)
604609
enableSocketRead();
605610
enableSocketWriteIfNeed();
606611
d_->state = State::kTcpConnected;
612+
607613
} else {
608-
LogWarn("connect fail, ret:%d", ret);
614+
tryReconnect();
609615
}
610-
611-
//! 如果是首次连接要启动定时器,重连的不需要
612-
if (first_connect)
613-
enableTimer();
614616
}
615617

616618
void Client::enableSocketRead()
@@ -663,6 +665,27 @@ void Client::disableTimer()
663665
d_->sp_timer_ev->disable();
664666
}
665667

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+
666689
void Client::handleDisconnectEvent()
667690
{
668691
disableSocketRead();
@@ -676,24 +699,7 @@ void Client::handleDisconnectEvent()
676699
--d_->cb_level;
677700
}
678701

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();
697703
}
698704
}
699705

modules/mqtt/client.h

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

77-
bool auto_reconnect_enabled = true; //! 是否自动重连
77+
bool auto_reconnect_enable = true; //! 是否自动重连
7878
int auto_reconnect_wait_sec = 0; //! 自动重连等待时长,秒
7979

8080
bool isValid() const;
@@ -122,8 +122,8 @@ class Client {
122122
kConnecting, //!< 正在连接
123123
kTcpConnected, //!< TCP已连接
124124
kMqttConnected, //!< MQTT已连接
125-
kDisconnected, //!< 已断连
126125
kReconnWaiting, //!< 断连等待中
126+
kEnd, //!< 终止,断连后又不需要重连的情况
127127
};
128128

129129
State getState() const;
@@ -149,7 +149,7 @@ class Client {
149149
void onMessage(const struct mosquitto_message *msg);
150150
void onLog(int level, const char *str);
151151

152-
void onTcpConnectDone(int ret, bool first_connect);
152+
void onTcpConnectDone(int ret);
153153

154154
void enableSocketRead();
155155
void enableSocketWrite();
@@ -160,6 +160,7 @@ class Client {
160160
void disableSocketWrite();
161161
void disableTimer();
162162

163+
void tryReconnect();
163164
void handleDisconnectEvent();
164165

165166
private:

0 commit comments

Comments
 (0)